C Programming Iterative Statement while loop for loop
















- Slides: 16

C Programming

Iterative Statement • while loop • for loop • do-while loop

Branching Condition F T Loops Statement list Condition T Statement list F 3

The for Statement • General Form of the for Statement for (expr 1; expr 2; expr 3) statement next statement • First expr 1 is evaluated; typically expr 1 is used to initialize the loop. • Then expr 2 is evaluated; if it is nonzero (true) then statement is executed, expr 3 is evaluated, and control passes back to the beginning of the for loop again, except that evaluation of expr 1 is skipped. The process continues until expr 2 is zero (false), at which point control passes to next statement.

For loop flow diagram • The following flow diagram describes the execution of a for loop: execute initialization check the test is true execute statements in body of loop test is false skip to 1 st statement after body of loop execute update

Typical Roles of for Loop expr 1, expr 2, & expr 3 • General Form of the for Statement for (expr 1; expr 2; expr 3) statement next statement • expr 1 is used to initialize the loop. • expr 2 is a logical expression controlling the iteration. The loop exits when expr 2 becomes false. • expr 3 typically modifies a variable in expr 2 eventually causing expr 2 to become false.

The most common kind of for-loop int i; for (i = 0; i < limit; i++) {. . . ; /* do something with entity */. . . ; Loop “bod y” is a com } typic poun a lly d stat emen t

The most common kind of for-loop int i; for (i = 0; i < limit; i++) { It i. . . ; s loo trad itio entity */ /* do something with tes ps sith nal t a t tha rt c i t h an t th oun n C t. . . ; the e c tin hat up oun g at fo } per ter rze lim is l ro a ess nd it

Example of for-loop #include <stdio. h> int main() { int i; for (i=1; i<=3; i++) { printf("%dn", i); } return 0; } Output: 1 2 3

Nested for-loops int i, j; for (i = 0; i < limit; i++) {. . . ; /*prepare subgroup i*/ for (j=0; j < limit 2; j++) {. . . ; /* do something with item j of subgroup i*/. . . ; }

Example of Nested for-loop #include <stdio. h> int main() { for (int i=0; i<2; i++) { for (int j=0; j<4; j++) { printf("%d, %dn", i , j); } } return 0; } Output 0, 0 0, 1 0, 2 0, 3 1, 0 1, 1 1, 2 1, 3

Equivalence of for and while Loops for (expr 1; expr 2; expr 3) statement Is Equivalent to: expr 1; while (expr 2) { statement expr 3; } next statement The for loop has the advantage of keeping both the control (expr 2) and the indexing (expr 3) all in one place at the top of the loop.

Missing Expressions in the for Loop • Any or all of the expressions in a for statement can be missing, but the two semicolons must remain. i = 1; sum = 0; for ( ; i <= 10; ) sum += i++; printf(“%dn”, sum); }

Use of the comma Operator in a for Statement • You can use the comma operator in a for statement to do multiple initializations and multiple processing of indices. • Examples for (sum = 0, i = 1; i <= n; ++i) sum += i;

Example: for loop // Program to calculate the sum of first n natural numbers // Positive integers 1, 2, 3. . . n are known as natural numbers #include <stdio. h> int main() { int num, count, sum = 0; printf("Enter a positive integer: "); scanf("%d", &num); // for loop terminates when n is less than count for(count = 1; count <= num; ++count) { sum += count; } printf("Sum = %d", sum); return 0; } Output Enter a positive integer: 10 Sum = 55

Thank You!!!