Você está na página 1de 5

LOOPS

A loop causes a section of the program to be repeated a certain number of times. The repetition continues while a condition is true. When the condition becomes false, the loop ends and the control is passed to the statement following the loop. while Loop The while construct repeats the body of the loop as long as the loop condition holds. The following program illustrates the usage of the while construct: //Program 2.2 //This program calculates the square of integer numbers #include<iostream.h> void main() { char cReply; int iNum, iSquare; cout<<"Do you want to find the square of a number (y/n)?"; cin>>cReply; while(cReply == 'y') { cout<<"Enter a number:"; cin>>iNum; iSqure = iNum * iNum; cout<<"The square of "<<iNum<<"is"<<iSquare<<endl; cout<<"Do you want to find the square of another number (y/n)?"; cin>>cReply; } } In Program 2.2 the body of the while loop is executed as long as cReply is 'y'. The sample output of Program 2.2 is: Do you want to find the square of a number (y/n)? y Enter a number: 9 The square of 9 is 81 Do you want to find the squre of another number (y/n)? n

do..while Loop In a while loop, the test expression is evaluated at the beginning of the loop. If the test expression is false, the loop body is not executed at all. If the loop body must be executed at least once, then the do..while construct should be used. The do..while construct places the test expression at the end of the loop. Program 2.2 can be modified using the do..while loop in the following way: //Program 2.3 //This program calculate the square of interger numbers #include<iostream.h> void main() { char cReply; int iNum, iSquare; do { cout<<"Enter a number:"; cin>>iNum; iSquare = iNum * iNum; cout<<"The square of "<<iNum<<"is"<<iSquare<<endl; cout<<"Do you want to input another number(y/n)?"; cin>>cReply; } while(cReply != 'n'); } used. The do..while construct places the test expression at the end of the loop. The keyword do marks the beginning of the loop. The braces delimit the body of the loop. Finally, a while statement provides the test expression and terminates the loop. The sample output of Program 2.3 is: Enter a number: 4 The square of 4 is 16 Do you want to input another number(y/n)?n If the user enters 'n', the test expression becomes false and the loop terminates.

for Loop The while and the do..while loops are used when the number of iteration(the number of times the loop body is executed) is not known. The for loop is used for fixed iterations. The following program illustrates the usage of the for loop: //Program 2.4 //This program calculates the square of the first ten natural numbers #include<iostream.h> void main() { int iVar; for(iVar = 1; iVar <= 10; iVar++) { cout<<iVar*iVar<<""; } } The output of Program 2.4 is: 1 4 9 16 25 36 49 64 81 100 The for statement consists of the keyword for, followed by parentheses containing three expressions, each separated by a semicolon. These three expressions are the initialization expression, test expression and the increment/decrement(re-initialization) expression. The general format of a for loop is: for(initialization; test; re-initialization) { body; } In the statement, for(iVar = 0; iVar <= 10; iVar++) iVar = 0 is the initialization expression, iVar <= 10 is the test expression, and iVar++ is the increment expression.

Initialization Expression The initialization expression is executed only once when the loop first starts. It gives the loop variable an initial value. Test Expression The test expression involves relational operators. It is executed each time through the loop before the body of the loop is executed. If the test expression is true, the loop is executed, and if it is false, the control passes to the statement following the for loop. Increment/Decrement(re-initialization) Expression The increment/decrement expression is always executed at the end of the loop, after the loop body. The body of the loop is enclosed within braces and is not executed if the for statement is followed by a semicolon. For example, in the following statement is followed by a semicolon. For example, in the following statement: for(iVar = 0; iVar <= 10; iVar++); { cout<<iVar*iVar<<""; } the body of the for loop is not executed.
break Statement

The break statement is used to terminate a case in a switch construct. The break statement is also for termination of a loop, bypassing the normal loop conditional test. When a break is encountered inside a loop, the loop is terminated and control passes to the statement following the loop body. The following program illustrates the usage of a break statement. In Program 2.5, if the user enters zero, the break statement causes immediate termination of the loop
//Program 2.5 //This program finds the inverse of a number #include<iostream.h> void main() { float fNum; char cReply; do { cout<<"Enter a number:"; cin>>fNum;

if(fNum == 0) break; cout<<"Inverse of the number is "<<1/fNum<<endl; cout<<"Do you want to input another number(y/n)?"; cin>>cReply; } while(cReply != 'n'); } continue Statment

The continue statement forces the next iteration of the loop to take place, skipping any code following the continue statement in the loop body. In the for loop, the continue statement causes the conditional test and then the re-initialization portion of the loop to be executed. In the while and do..while loops, program control passes to the coditional test. The following program illustrates teh usage of the continue statement:
//Program 2.6 //This program finds the square of the numbers less than 100 #include<iostream.h> void main() { int iNum; char cReply = 'y'; do { cout<<"Enter a number:"; cin>>iNum; if(iNum > 100) { cout<<"The number is greater than 100, enter another"<<endl; continue; } cout<<"The square of the number is: "<<iNum * iNum<<endl; cout<<"Do you want to enter another(y/n)?"; cin>>cReply; } while(cReply != 'n'); }

Você também pode gostar