Thursday, May 2, 2019

Loop Statement

Loop statement
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:
C++ programming language provides the following type of loops to handle looping requirements

While Loop :

A while loop statement repeatedly executes a target statement as long as a given condition is true.

Syntax

 The syntax of a while loop in C++ is:


while(condition) {
statement(s); }
Here statement(s) may be a single statement or a block of statements. The condition may be any expression, and true is any non-zero value. The loop iterates while the condition is true.
When the condition becomes false, program control passes to the line immediately following the loop.
Example:
#include <iostream>
 using namespace std;
int main () 
{
int n; 
cout << "Enter the starting number > ";
 cin >> n;
while (n>0)
 {
 cout << n << ", "; --n;
}
cout << "FIRE!\n"; 
 return0;
}

for Loop

A for loop is a repetition control structure that allows you to efficiently write a loop that needs to execute a specific number of times.

Syntax 

For (init;condition;incr/decrd)
{
   Statement(s);
}

 Here is the flow of control in a for loop:
1. The init step is executed first, and only once. This step allows you to declare and initialize any loop control variables. You are not required to put a statement here, as long as a semicolon appears.
2. Next, the condition is evaluated. If it is true, the body of the loop is executed. If it is false, the body of the loop does not execute and flow of control jumps to the next statement just after the for loop.
3. After the body of the for loop executes, the flow of control jumps back up to the increment statement. This statement allows you to update any loop control variables. This statement can be left blank, as long as a semicolon appears after the condition.
4. The condition is now evaluated again. If it is true, the loop executes and the process repeats itself (body of loop, then increment step, and then again condition). After the condition becomes false, the for loop terminates.

Example:
#include <iostream> 
using namespace std; 
int main ()
 {
for (int n=10; n>0; n--) 
{ cout << n << ", ";
} cout << "FIRE!\n";
 return 0;
}

do…while Loop

Unlike for and while loops, which test the loop condition at the top of the loop, the do...while loop checks its condition at the bottom of the loop. A do...while loop is similar to a while loop, except that a do...while loop is guaranteed to execute at least one time.

Syntax

 The syntax of a do...while loop in C++ is:
do
{
statement(s);
}
while(condition);
Notice that the conditional expression appears at the end of the loop, so the statement(s) in the loop execute once before the condition is tested.

Example:
#include <iostream> 
using namespace std;
int main ()
 {
unsigned long n;
 do
 { cout << "Enter number (0 to end): "; 
cin >> n;
 cout << "You entered: " << n << "\n";

while (n != 0);
 return 0;
}

No comments:

Post a Comment

Array

Arrays  An array is a series of elements of the same type placed in contiguous memory locations that can be individually referenced by ad...