Download presentation
Presentation is loading. Please wait.
1
INC 161 , CPE 100 Computer Programming
Lecture 5 Flow Control (Cont.)
2
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; }
3
Flowchart of a for Loop 2 semi-colons inside
The syntax of a for loop is as follows: for(expression1; expression2; expression3) statement
4
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: for-loop is suitable for counter control
5
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
6
Find factorial n= 4 f = 1 i = 2 f = 2 i = 3 f = 6 i = 4 f = 24 i = 5
7
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
8
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:
9
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 }
10
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"); }
11
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"); }
12
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"); }
13
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
14
Nested Loop Nested loop = loop inside loop Program flow
The inner loops must be finished before the outer loop resumes iteration.
15
Write a program to print a multiplication table.
1| 2| 3| 4| 5| 6| 7| 8| 9| 10|
16
A program to print a multiplication table.
Example: A program to print a multiplication table. #include <stdio.h> main() { int i, j; printf(" \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");
17
Small loop
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.