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