Prepared by: Shraddha Modi
Introduction In looping, a sequence of statements are executed until some conditions for termination of the loop are satisfied. A program loop consist of two segments Body of the loop Control statement Two types of control structure: Entry-controlled loop Exit-controlled loop
For loop Entry-controlled loop Used when the number of iterations is predetermined. Syntax : for (initialization; test condition; increment/decrement) block of code next statement;
INITIALIZATION CONDITION FALSE True LOOP_BODY INCREMENT/ DECREMENT NEXT STATEMENT 4
Example: Output : void main() int i, n=5; for (i=0;i<n; i=i+1) printf ( The numbers are %d \n",i); The numbers are 0 The numbers are 1 The numbers are 2 The numbers are 3 The numbers are 4 5
Nested loop If a for loop is defined in another for loop, it is called nested for loop. The outer loop is executed first of all. The general format of nested for loop is: for (initialization; test condition; increment/decrement) for (initialization; test condition; increment/decrement) block of code block of code next statement;
Write a program to display the stars as shown below * * * * * * * * * * * * * * * * * * * * * * * * * void main( ) int x, i, j ; * * * * * printf( How many lines stars (*) should be printed? : ); scanf( %d, &x); for(i=1; i<=x; i++) for (j=1; j < =i; j++) printf( * ); printf( \n );
While Loop Entry-controlled loop Syntax : while(condition 1) body of the loop next statement; CONDITION True Body of Loop False NEXT STATEMENT
Example: void main() Output : int i = 0; while (i<5) printf(" the value of i is %d\n", i); i=i+1 ; the value of i is 0 the value of i is 1 the value of i is 2 the value of i is 3 the value of i is 4
Program to calculate factorial of a given number using while loop. void main ( ) int n, fact =1; clrscr( ) ; printf( \n Enter the Number: ); scanf( %d, &n); while(n>=1) fact = fact*n; n - - ; printf( \n factorial of given number is %d, fact); getch( );
Do-While Loop It tests the condition at the bottom of the loop after executing the body of the loop. we can be assured that the body of the loop is executed at least once. It is mainly used in menu like programs where all the available choices are printed at least once. Syntax : do statement; while(condition); Statement CONDITION False True
Example: void main() Output : int i,n = 5; i = 0; do printf("the numbers are %d \n",i); i=i +1; while( i<n) ; the numbers are 0 the numbers are 1 the numbers are 2 the numbers are 3 the numbers are 4
Program to calculate factorial of a given number using do while loop. void main ( ) int n, fact =1; clrscr( ) ; printf( \n Enter the Number: ); scanf( %d, &n); do fact = fact * n; n- -; while (n >= 1); printf( \n factorial of given number is %d, fact); getch( );
for loop while statement do-while statement Condition is evaluated at the beginning of the loop. Condition is evaluated at the beginning of the loop. Condition is evaluated at the end of the loop. A variable value is initialized at the beginning of the loop. Increment/Decrement is given at the beginning of the loop. The statement block will not be executed when the value of the condition is false. A variable value is initialized at the beginning or before the loop. Increment/Decrement is given inside the loop. The statement block will not be executed when the value of the condition is false. A variable value is initialized before the loop or assigned inside the loop. Increment/Decrement is given inside the loop. The statement block will executed at least once the value of the condition is false.
Example: for(i=1;i<=10;i++) s = s + i; p = p * i; Example: i =1; while(i<=10) s = s + i; p = p * i; i++; Example: i =1; do s = s + i; p = p * i; i++; while(i<=10);
Continue statement The continue statement is used to bypass the remainder of the current pass through a loop. The loop does not terminate when a continue statement is encountered, instead, the remaining loop statements are skipped and the computation proceeds directly to the next pass through the loop. The continue statement can be included within a while, a do-while, a for statement. It is simply written as continue. The continue statement tells the compiler Skip the following Statements and continue with the next Iteration. In while and do loops continue causes the control to go directly to the test condition and then to continue the iteration process. In the case of for loop, the updation section of the loop is executed before test-condition, is evaluated.
(1) while (Test condition) - - - - - - - - if ( - - - - - - -) continue; -------------------- -------------------- (2) do - - - - - - - - if ( - - - - - - -) continue; -------------------- -------------------- while (Test condition); (3) for(initialization; test condition; increment) - - - - - - - - if ( - - - - - - -) continue; -------------------- --------------------
No. break statement continue statement 1. A break statement is used to terminate the execution of a statement block. The keyword is break; A continue statement is used to transfer the control to the beginning of a statement block. The keyword is continue; 2. When a break statement is executed in a loop, a repetition of the loop will be terminated. When a continue statement is executed in a loop, the execution is transferred to the beginning of the loop.
Example: #include<stdio.h> void main() int i = 1, j = 0; clrscr(); while(i<=5) i=i+1; if(i== 2) break; j=j+1; printf("value of i=%d and j=%d",i,j); getch(); OUTPUT: value of i=2 and j=0 Example: #include<stdio.h> void main() int i = 1, j = 0; clrscr(); while(i<=5) i=i+1; if(i== 2) continue; j=j+1; printf("value of i=%d and j=%d",i,j); getch(); OUTPUT: value of i=6 and j=4