Вы находитесь на странице: 1из 7

C++ Loop Statements C++ has three distinct loop statements and also uses the break statement

to make other combinations. Consider the following menu items: a) The for loop. b) The while and do while loops. c) The break statement used with loops or x) Exit to the Looping exhibit Links: The for statement The while statement The break statement with loops Looping Statements The for statement The for loop is the most general of the C++ loops. It is the usual counting loop found in many other programming languages, but is much stronger than most of those. The loop header contains the initialization, the test and the advancement, making the loop header more complicated to code and the loop body less complicated to code. It is a leading decision loop. A simple for that reads in 10 integers and sums them would look like this: int i, total = 0, num = 0; for(i=0;i<10;i++) { cin >> num; total += num; } This lesson is divided into the following pieces. Use the menu to observe. a) The syntax and semantics of the for b) The scope of the for c) Some examples or x) Exit to the loop exhibit The while statement The while loop is a simplification of the for loop. A simple while, similar to the for example, that reads in 10 integers and sums them would look like this: int i=0, total = 0, num = 0; while(i<10) {

cin >> num; total += num; i++; } This lesson is divided into the following pieces, use the menu to observe. a) The syntax and semantics of the while b) When to use the while, with examples. c) The do - while is a variation or x) Exit to the loop exhibit The break statement with loops The break statement should have already been seen in the switch case lesson. In that statement it is used to exit from the case when it is complete. Its general purpose is to exit a construct. This construct may be either switch-case or any type of loop. Since it is not a conditional statement it is usually preceded by an if or case. This opens two interesting possibilities, which you should consider from the following menu. a) The N and a half loop b) The multi-exit loop or x) Return to the Loop statements Exhibit Looping Statements Loops are one of the more complicated things to code in any programming language. Often we build them inside out, unlike most other pieces of code. This exhibit consists of several pieces, each of which are menu entries. Normally you should process them in order, though the menu item d) could be viewed before c). a) A general introduction to loops, including some terminology. b) The various kinds of looping statements, including syntax. c) Some loop construction tips. d) Loop coding examples e) Disk files, an approach needing looping or x) Return to Flow of Control Statements

C++ Looping
Author: Sripriya R Published on: 4th Sep 2007 | Last Updated on: 25th Jul 2011

In object-oriented programming language, whenever a block of statements has to be repeated a certain number of times or repeated until a condition becomes satisfied, the concept of looping is used. The following commands used in C++ for achieving looping:

for loop while loop do-while loop

for loop:
The syntax of for loop is Sample Code
1. 2. 3. 4. for (initialization; condition; increase or decrease) { statement block; }
Copyright exforsys.com

Initialization is the primary value set for a variable. Initialization is only executed one time. After initialization is performed, it is not executed again. The condition specified is checked and if the condition returns a value of true, (if the condition is satisfied), the loop is continued for execution. If the condition returns a value of false, (the condition is not satisfied), then the statements following the loop block are not executed and the control is transferred to the end of the loop. The statement specified in the for loop can be a statement block or more than one statement. These statements must be enclosed within flower brace symbols{ }. The statement block can also be a single statement. For a single statement, the flower brace symbols are not needed and may be specified as: Sample Code
1. for (initialization; condition; increase or decrease) 2. statement;
Copyright exforsys.com

If the condition becomes satisfied, all the statements in the for block are executed, the increase or decrease is executed on the variable and again the condition is checked, so the loop continues with these commands until the condition becomes false. It is important for the programmer to remember that the optional attributes are initialization and increase/decrease where the for loop must be written as: Sample Code
1. for (;condition;)
Copyright exforsys.com

or as Sample Code
1. for(initialization ; condition;)
Copyright exforsys.com

It is also possible to initialize more than one variable in a for loop and to perform this the programmer uses the comma operator denoted as ",". For example: To initialize two variables namely a and b with value 5 and 10 respectively and increment a and decrement b this can be done as follows; Sample Code
1. for (a=5,b=10;a!=b; a++, b--)
Copyright exforsys.com

Let us see a small example using for loop: Sample Code


1. 2. 3. 4. #include <iostream> using namespace std; void main() {

5. 6. 7. 8. 9. 10.

for (int x=5; x>0; x--) { cout << x << "; "; } cout << "EXFORSYS"; }
Copyright exforsys.com

The output of this program is

while loop:
Statement of while statement is: Sample Code
1. while (expression) 2. { 3. statement block; 4. }
Copyright exforsys.com

Until the condition represented in the expression is true (satisfied), the set of statements in while block is executed. As in for loop, when the number of statements to be executed is more than one (if the condition is satisfied), then it is placed inside the flower braces as shown in the example above. If the number of statements to be executed is one, if the condition is satisfied, then the flower braces may be removed. For Example: Sample Code

1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14.

#include <iostream> using namespace std; void main () { int x; cout << "Input the number:"; cin >> x; while (x>0) { cout << x << "; "; --x; } cout << "EXFORSYS"; }
Copyright exforsys.com

Here the output of the above program is

In this example, the input number is 5 and the while loop is executed starting from 5 until the number reaches a value <0.Whilethe value of x>0 the statement inside the while block prints the value of x. When this is completed, the numbers 5, 4, 3, 2, 1 are printed. After the value of x reaches less than zero, the control passes to outside the while block and EXFORSYS is printed.

do-while loop:
The syntax of do-while loop is Sample Code
1. do 2. { 3. statement block; 4. } while (condition);
Copyright exforsys.com

The do while loop functionality is similar to while loop. The main difference in do-while loop is that the condition is checked after the statement is executed. In do-while loop, even if the condition is not satisfied the statement block is executed once. For Example: Suppose a programmer wishes to output the input number until the user types the number 5. This can be done using a do..while loop structure as follows: Sample Code
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. #include <iostream> using namespace std; void main () { int x; do { cout << "Input the number:"; cin >> x; cout << "The number is:" << x << "n"; } while (x != 5); cout << "End of program"; }
Copyright exforsys.com

The output of the above program is:

Вам также может понравиться