Loops Chapter-1 There may be a situation, when you need to execute a block of code several number of times. In general, statements are executed sequentially: The first statement in a function is executed first, followed by the second, and so on. A loop statement allows us to execute a statement or group of statements multiple times and following is the general from of a loop statement in most of the programming languages Loop Architecture programming language provides the following type of loops to handle looping requirements.
There are 3 types of loops in language 1) while loop 2) for loop 3) -while loop while loop while loop can be address as an entry control loop. It is completed in 3 steps. 1) Variable initialization (e.g. int x=0; ) 2) Condition( e.g while( x<=10) ) 3) Variable increment or decrement (x++ or x-- or x=x+2 ) Syntax : variable initialization ; while (condition) statements ; Example of While Loop variable increment or decrement ; #include <iostream.h> #include<conio.h> // Local variable declaration: int a = 10; // while loop execution while( a < 20 ) cout << "value of a: " << a << endl; a++;
Output value of a: 10 value of a: 11 value of a: 12 value of a: 13 value of a: 14 value of a: 15 value of a: 16 value of a: 17 value of a: 18 value of a: 19 Do While Loop Do While loop can be address as an exit control loop. In some situations it is necessary to execute body of the loop before testing the condition. Such situations can be handled with the help of -while loop. statement evaluates the body of the loop first and at the end, the condition is checked using while statement. In While loop if the condition is wrong still it will executed at once. General format of -while loop is, Syntax Example: Variable initialization; Increment/decrement; //optional Statement(s); while(condition); #include <iostream.h> #include<conio.h> // Local variable declaration: int a = 10; // while loop execution cout << "value of a: " << a << endl;
a++; while(a<20); Output value of a: 10 value of a: 11 value of a: 12 value of a: 13 value of a: 14 value of a: 15 value of a: 16 value of a: 17 value of a: 18 value of a: 19 For Loop for loop is used to execute a set of statement repeatedly until a particular condition is satisfied. we can say it an open ended loop. General format is, Syntax for(initialization; condition ; increment/decrement) statement-block; In for loop we have exactly two semicolons, one after initialization and second after condition. In this loop we can have more than one initialization or increment/decrement, separated using comma operator. for loop can have only one condition. Example #include<iostream.h> #include<conio.h> void main() clrscr(); cout<<"\n The first 10 integers are as follows :"<<endl; for(int i=1;i<=10;i++) cout<<" "<<i;
Output Jumping out of loop Sometimes, while executing a loop, it becomes necessary to skip a part of the loop or to leave the loop as soon as certain condition becomes true, that is jump out of loop. C language allows jumping from one statement to another within a loop as well as jumping out of the loop. 1) Break statement When break statement is encountered inside a loop, the loop is immediately exited and the program continues with the statement immediately following the loop. Example #include <iostream> // Declaring Local variable int count = 0; // loop execution cout << "Count : " << count << endl;
count++; if( count > 5) // Terminate the loop break; while( count < 20 ); 2) Continue statement The continue statement forces the next iteration of the loop to take place, skipping remaining code in between. 1. In the case of the for loop as soon as after the execution of continue statement, increment/decrement statement of the loop gets executed. After the execution of increment statement, condition will be checked. 2. In case of the while loop, continue statement will take control to the condition statement. 3. In case of the..while loop, continue statement will take control to the condition statement specified in the while loop. Example #include <iostream.h> #include <conio.h>
int count = 0; count++; if(count > 5 && count < 7) continue; cout << "Count : " << count << endl; while( count < 10 ); Output : Count : 1 Count : 2 Count : 3 Count : 4 Count : 5 Count : 7 Count : 8 Count : 9 Count : 10 In the above example, when count = 6 then both conditions become true and thus continue statement gets executed in that case.