INC 161 , CPE 100 Computer Programming Lecture 5 Flow Control (Cont.)
the controlling expression. For Loop The syntax of a for statement is as follows: for(expression1; expression2; expression3) statement The expression expression1 is evaluated as a void expression before the first evaluation of the controlling expression. The expression expression2 is the controlling expression that is evaluated before each execution of the loop body. The expression expression3 is evaluated as a void expression after each execution of the loop body. Both expression1 and expression3 can be omitted. An omitted expression2 is replaced by a nonzero constant. The for-loop is semantically equivalent to the following while-loop: expression1; while(expression2) { expression3; }
Flowchart of a for Loop 2 semi-colons inside The syntax of a for loop is as follows: for(expression1; expression2; expression3) statement
0 1 2 3 4 Output: for-loop is suitable for counter control Example: int i; for(i = 0; i < 5; i++) { printf(“%d ”, i); } Example: Output: 0 1 2 3 4 for-loop is suitable for counter control
Calculating a factorial 5!. The factorial n! is defined as n*(n-1)! Example: Calculating a factorial 5!. The factorial n! is defined as n*(n-1)! /* File: forloop.c */ #include <stdio.h> main() { unsigned int i, f, n; printf(“Please input a number\n”); scanf(“%d”, &n); for(i=1, f=1; i<=n; i++) { f = f*i; } printf(“factorial %d! = %d\n", n, f); Execution and Output: > forloop.c 5 factorial 5! = 120
Find factorial n= 4 f = 1 i = 2 f = 2 i = 3 f = 6 i = 4 f = 24 i = 5
Jump Statements (break, continue) Break Statements The break statement provides an early exit from the for, while, do-while, and for each loops as well as switch statement. A break causes the innermost enclosing loop or switch to be exited immediately. Example: int i; for(i=0; i<5; i++) { if(i == 3) { break; } printf("%d", i); Output: 0 1 2
int i; Continue Statements and do-while loop to begin. Example: The continue statement causes the next iteration of the enclosing for, while, and do-while loop to begin. A continue statement should only appear in a loop body. Example: int i; for(i=0; i<5; i++) { if(i == 3) { continue; } printf("%d", i); Output: 0 1 2 4
switch..case The switch command is used to decide the choices that the program will jump to. switch(p) { case 1: commands // if p==1 case 2: commands // if p==2 case 3: commands // if p==3 }
Example #include <stdio.h> main() { int p; printf("Please enter the choice: "); scanf("%d",&p); switch(p) { case 1: printf("Case 1\n"); case 2: printf("Case 2\n"); case 3: printf("Case 3\n"); }
Normally, we will use the break command to run only the case we want. #include <stdio.h> main() { int p; printf("Please enter the choice: "); scanf("%d",&p); switch(p) { case 1: printf("Case 1\n"); break; case 2: printf("Case 2\n"); case 3: printf("Case 3\n"); }
Default label is used when we want t handle other cases. #include <stdio.h> main() { int p; printf("Please enter the choice: "); scanf("%d",&p); switch(p) { case 1: printf("Case 1\n"); break; case 2: printf("Case 2\n"); default: printf("Case 3\n"); }
switch-case is the same as else-if The syntax for an else-if statement is as follows: if(expression1) statement1 else if(expression2) statement2 else if(expression3) statement3 else statement4
Nested Loop Nested loop = loop inside loop Program flow The inner loops must be finished before the outer loop resumes iteration.
Write a program to print a multiplication table. 1 2 3 4 5 6 7 8 9 10 ------------------------------------------ 1| 1 2 3 4 5 6 7 8 9 10 2| 2 4 6 8 10 12 14 16 18 20 3| 3 6 9 12 15 18 21 24 27 30 4| 4 8 12 16 20 24 28 32 36 40 5| 5 10 15 20 25 30 35 40 45 50 6| 6 12 18 24 30 36 42 48 54 60 7| 7 14 21 28 35 42 49 56 63 70 8| 8 16 24 32 40 48 56 64 72 80 9| 9 18 27 36 45 54 63 72 81 90 10| 10 20 30 40 50 60 70 80 90 100
A program to print a multiplication table. Example: A program to print a multiplication table. #include <stdio.h> main() { int i, j; printf(" 1 2 3 4 5 6 7 8 9 10\n"); printf(" ------------------------------------------\n"); for(i=1; i<= 10; i++) { /* outer loop */ printf("%4d|", i); for(j=1; j<= 10; j++) { /* inner loop */ printf("%4d", i*j); } printf("\n");
Small loop