Assoc. Prof. Dr. Tansu FİLİK

Similar documents
Assoc. Prof. Dr. Tansu FİLİK

Computer Programming 5th Week loops (do-while, for), Arrays, array operations, C libraries

Assoc. Prof. Dr. Tansu FİLİK

Assoc. Prof. Dr. Tansu FİLİK

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

Computer Programming: 7th Week Functions, Recursive Functions, Introduction to Pointers

Assoc. Prof. Dr. Tansu FİLİK

More examples for Control statements

Chapter 2. Section 2.5 while Loop. CS 50 Hathairat Rattanasook

Decision Making and Loops

Chapter 5: Control Structures

Computer Programming 3 th Week Variables, constant, and expressions

INTRODUCTION TO C++ PROGRAM CONTROL. Dept. of Electronic Engineering, NCHU. Original slides are from

Control Structure: Loop

PDS Lab Section 16 Autumn Tutorial 3. C Programming Constructs

Introduction to C Programming

Day06 A. Young W. Lim Wed. Young W. Lim Day06 A Wed 1 / 26

University of California San Diego Department of Electrical and Computer Engineering. ECE 15 Midterm Exam

Problem Solving through Programming In C Prof. Anupam Basu Department of Computer Science & Engineering Indian Institute of Technology, Kharagpur

CS102: Standard I/O. %<flag(s)><width><precision><size>conversion-code

Tail recursion. Decision. Assignment. Iteration

Computer Programming: 8th Week Pointers

Computer Programming 6th Week Functions (Function definition, function calls),

Flow of Control. Selection. if statement. True and False in C False is represented by any zero value. switch

Computer Programming: Skills & Concepts (CP) Files in C

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

Computer Programming: Skills & Concepts (CP1) Files in C. 18th November, 2010

There are algorithms, however, that need to execute statements in some other kind of ordering depending on certain conditions.

Control Structure: Selection

LESSON 4. The DATA TYPE char

CpSc 1111 Lab 4 Part a Flow Control, Branching, and Formatting

BSM540 Basics of C Language

Multiple Choice Questions ( 1 mark)

Rule 1-3: Use white space to break a function into paragraphs. Rule 1-5: Avoid very long statements. Use multiple shorter statements instead.

Module 4: Decision-making and forming loops

Advanced Computer Programming

Chapter 4 C Program Control

Lecture 6. Statements

بسم اهلل الرمحن الرحيم

Introduction. C provides two styles of flow control:

Iteration. Side effects

Unit 5. Decision Making and Looping. School of Science and Technology INTRODUCTION

n Group of statements that are executed repeatedly while some condition remains true

Theory of control structures

Laboratory 2: Programming Basics and Variables. Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information

Structured programming

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath

Data Type Fall 2014 Jinkyu Jeong

Programming for Engineers Introduction to C

COMP1917 Computing 1 Written Exam Sample Questions

CS Introduction to Programming Midterm Exam #2 - Prof. Reed Fall 2015

ROCK PAPER SCISSORS Rock Paper Scissors Lab Project Using C or C++

Example: Monte Carlo Simulation 1

SECTION A TRUE / FALSE QUESTIONS (10 MARKS) (INSTRUCTION: Please answer all 10 questions)

Lecture 12 CSE July Today we ll cover the things that you still don t know that you need to know in order to do the assignment.

3/12/2018. Structures. Programming in C++ Sequential Branching Repeating. Loops (Repetition)

C OVERVIEW. C Overview. Goals speed portability allow access to features of the architecture speed

15 FUNCTIONS IN C 15.1 INTRODUCTION

Comments. Comments: /* This is a comment */

COP 2000 Introduction to Computer Programming Mid-Term Exam Review

Operators And Expressions

The C Programming Language Part 2. (with material from Dr. Bin Ren, William & Mary Computer Science)

Flow Control. CSC215 Lecture

Other Loop Options EXAMPLE

Data Types. Data Types. Integer Types. Signed Integers

CHAPTER 2.2 CONTROL STRUCTURES (ITERATION) Dr. Shady Yehia Elmashad

8. Characters and Arrays

printf( Please enter another number: ); scanf( %d, &num2);

Conditional Statement

Dept. of CSE, IIT KGP

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University

Midterm Exam. CSCI 2132: Software Development. March 4, Marks. Question 1 (10) Question 2 (10) Question 3 (10) Question 4 (10) Question 5 (5)

C Functions. CS 2060 Week 4. Prof. Jonathan Ventura

C Programming Basics

Slide Set 1. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary

DECISION MAKING STATEMENTS

UNIT IV 2 MARKS. ( Word to PDF Converter - Unregistered ) FUNDAMENTALS OF COMPUTING & COMPUTER PROGRAMMING

COMP 208 Computers in Engineering

Subject: PROBLEM SOLVING THROUGH C Time: 3 Hours Max. Marks: 100

CSE 5A Introduction to Programming I (C) Homework 4

2/5/2018. Learn Four More Kinds of C Statements. ECE 220: Computer Systems & Programming. C s if Statement Enables Conditional Execution

Fundamentals of Programming Session 8

Fundamentals of Programming. Lecture 3: Introduction to C Programming

Chapter 7 Functions. Now consider a more advanced example:

C Language Part 2 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

Prepared by: Shraddha Modi

Sudeshna Sarkar Dept. of Computer Science & Engineering. Indian Institute of Technology Kharagpur

공학프로그래밍언어 (PROGRAMMING LANGUAGE FOR ENGINEERS) -CONTROL FLOW : LOOP- SPRING 2015, SEON-JU AHN, CNU EE

Problem Solving and 'C' Programming

C OVERVIEW BASIC C PROGRAM STRUCTURE. C Overview. Basic C Program Structure

Chapter 4 Lab. Loops and Files. Objectives. Introduction

8. Characters, Strings and Files

Computer Science & Engineering 150A Problem Solving Using Computers

Programming for Electrical and Computer Engineers. Loops

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

Programming Language A

Data types, variables, constants

M.CS201 Programming language

Government Polytechnic Muzaffarpur.

CSE 1001 Fundamentals of Software Development 1. Identifiers, Variables, and Data Types Dr. H. Crawford Fall 2018

Transcription:

Assoc. Prof. Dr. Tansu FİLİK

Previously on Bil 200 Low-Level I/O getchar, putchar, getc, putc Control Structures if-else, switch-case, loops (while, dowhile, for loops)

for loop (generic) A disturbingly general loop structure. statement and expression can be anything. The whole program, no matter how ridiculous, can be written inside a single for loop.

for loop for( initial statements;logical expression;updates) statements; initial statements can be used as the first commands and assignments before running the loop. These statements work only for once. You can use several statements separated by,. initializes the for loop. It may be a mathematical assignment, or several commands. It runs only once.

for loop for( initial statements;logical expression;updates) statements; updates get executed after the statements in the block are executed. There may be several updates separated by,. makes the final changes to the control variables. These changes and variables can be completely different than the ones in statement1, or the body statements.

for loop for( initial statements;logical expression;updates) statements; After the updates, the logical expression is checked. If it is true, the loop continues. checks for the truth of an expression. That expression can be anything that determines the continuity or stop condition.

int gun, saat, dakika; for(gun=1; gun<=3; gun++) printf( gun=%d\n,gun); for(saat=5; saat>2; saat--) 1 2 4 dakika=60*saat; printf( S:%d D:%d\n,saat,dakika); 3 1 2 3 4 2 3 4

for loop : Notes for is an extremely flexible structure. The statement 1-3 parts may be completely empty, leaving everything inside the body. Yet, do not forget to use ;. Skipping the first and third statements actually turn the for loop into a while loop. Without the second statement (in the middle), you cannot determine when to terminate the loop. In this case, you must use break somewhere inside the body to check for a termination situation. The first and third statements can contain multiple commands. Using such a style, you can write single line for loops, even without a body part.

for loop : Example for(;;) < - > printf( Bir sayi girin (çıkış için 0): ); scanf( %d,&number); if(number==0) break; for(number=1;number!=0;) printf( Bir sayi girin (çıkış için 0): ); scanf( %d,&number); < - > for(number=1;number!=0; printf( Bir sayi girin (çıkış için 0): ),scanf( %d,&number));

for while statement1 ; while ( expression2 ) statements ; statement3 ; for ( statement1;expression2;statement3 ) statements ;

for loops : Example #include <stdio.h> void main ( ) int k, n ; for( k = 1, n = 12 ; k<9 && n>6 ; k++, n--) printf ("k=%d, n=%d\n", k, n ) ;

Exercises: What is the value of x when the following statement is complete? for (x = 0 ; x < 100 ; x++) ; What is the value of ctr when the following statement is complete? for (ctr = 2 ; ctr < 10 ; ctr += 3) ; How many X s does the following print? for (x = 0; x < 10; x++) for (y = 5; y > 0; y--) printf("x");

Loop termination The keyword break can be used for terminating the loop. If the loop is nested, it terminates the current block. The keyword continue is used for skipping until the end of the loop block, but the block does not terminate. Depending on the algorithm, you may use combinations of these keywords.

Loop termination: break: exit loop In the following program, break causes the loop to finish when value of x becomes 5. void main() int x = 0; for( ; ; ) /* infinite loop */ if(x == 5) break; printf("x = %d\n", x); x++;

Example: Nested for loops Consider formula: x 2 + y 2 = z 2. Here, x, y, and z are integers. As an example, for 3,4,5 triangle, 3 2 + 4 2 = 5 2. We wish to obtain others. Brute force method: We may check all integer x-y couples and see if an integer z can be obtained.

Example: Nested for loops #include <stdio.h> #define N 20 void main(void) int i,j,k, karetoplam; for (i=1; i <= N; i++) for (j=1; j <= N; j++) karetoplam = i * i + j * j; /* x2 + y2 */ /* is there a suitable z for the x and y? */ for(k = 1 ; k <= N; k++ ) if (karetoplam == k * k) printf("%5d %5d %5d\n", i, j, k);

Example: Nested for loops 3 4 5 4 3 5 5 12 13 6 8 10 8 6 10 8 15 17 9 12 15 12 5 13 12 9 15 12 16 20 15 8 17 16 12 20

Exercises: Can you write the previous program using only two nested loops? Think of a way to eliminate repetitions in the loop. In this way, once 3 4 5 is determined, the program will not output 4 3 5.

Exercises: Write a program in which you can enter a string into a string variable using for loop and getchar. Whenever an enter is pressed, the new string must be available for printing with printf( %s,..). Find a simple program that converts lowercase letters to uppercase ones inside a string. Do not use long lists of if-else or switch-case. Do not use ready C functions either. You do not need to know ASCII codes of letter, either. Be smart.

Exam Examples #include <stdio.h> main(void) int i = 0, x = 0; for (i = 1; i < 10; ++i) if( i % 2 == 1) x += i; else x--; printf("%d-", x); printf("\nx = %d", x); Write output of the program 1-0-3-2-7-6-13-12-21 x = 21 Rewrite the equivalent of the following for loop using while loop i = 1; while (i < 10) if( i % 2 == 1) x += i; else x--; printf("%d ", x); ++i;

Exam Examples #include <stdio.h> main(void) int i, j, k, x=0, y=0; for (i = 0; i < 5; ++i) for (j = 0; j<i; ++j) switch (i + j - 1) case -1: case 0: y +=++x; x += y; break; case 1: case 2: case 3: x += 2; default: x += 3; printf("%d ", x); printf("\nx = %d\n", x); Write output of the program #include <stdio.h> main(void) int i, j, k, x=0, y=0; 1-0-3-2-7-6-13-12-21 x = 21 Rewrite the program by using if-else statement instead of switch statement which will give the same output. for (i = 0; i < 5; ++i) for (j = 0; j<i; ++j) if ((i + j - 1) <= 0 ) y += ++x; x += y; else if ((i + j - 1) > 0 && (i + j - 1) <= 3) x += 2; x += 3; else x += 3; printf("%d ", x); printf("\nx = %d\n", x);

Exam Examples