knowledgediarybd.com

Your Virtual Knowledge Campus

For Loop, While Loop And Do while Loop In OOP

For Loop, While Loop And  Do while Loop In OOP

 

for loop while loop & do while loop

 

C++ has very effective & flexible three looping mechanisms like Java/C languages  or other languages. For Loop,While Loop and  Do while Loop are the most important statement all of them in c++ programming.They are used in c++ programming for doing a program very fast & effective.

You can use one of the following three loops in c++:

  1. while Loop
  2. do…while Loop
  3. for Loop

1. while loop:

The  while loop is a control structure that allows you to repeat a task a certain number of times. The while loop you can use it  when you don’t know in advance when the loop will terminate or when you may not want to execute the loop body even once.

Syntax of while loop:

while(Condition) { //Statements } More details see the figure: while loop When executing, if the Condition result is true then the actions inside the loop will be executed. This will continue as long as the expression result is true. Here key point of the while loop is that the loop might not ever run. When the expression is tested and the result is false, the loop body will be skipped and the first statement after the while loop will be executed.

Example with code:

#include<iostream>
using namespace std;
int main()
{
int n=2;
while(n!=5)
{
cout<<"\nValue of n:"<<n<<endl;
n++;
}
}

This would produce following result:

Value of n = 2
Value of n = 3
Value of n = 4
Value of n = 45

2.do…while Loop:

do…while loop is similar to a while loop, except that a do…while loop is guaranteed to execute at least one time that means you can use do loop when you’re sure you want to execute the loop body at least once.

Syntax:

do { //Statements }while(Condition);

More detail syntax of do..while loop:

do while loop Notice that the Condition appears at the end of the loop, so the statements in the loop execute once before the Boolean is tested. If the Boolean expression is true, the flow of control jumps back up to do, and the statements in the loop execute again. This process repeats until the Boolean expression is false.

Example with c code:

#include<iostream>
using namespace std;
int main()
{
int n=1;
do
{
cout<<"Value of n:"<<n<<endl;
n++;
}while(n!=4);
}

This would produce following result:

Value of n = 1 Value of n = 2 Value of n = 3

3. for Loop:

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

Syntax:

for(initialization; Condition; update) { //Statements } for loop

Here is the flow of control in a for loop:

  1. The initialization 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 past the for loop.
  3. After the body of the for loop executes, the flow of control jumps back up to the update 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 Boolean expression.
  4. The Condition is now evaluated again. If it is true, the loop executes and the process repeats itself (body of loop, then update step,then Boolean expression). After the Boolean expression is
  5. false, the for loop terminates.

Example:

#include<iostream>
using namespace std;
int main()
{
int n=1;
for(n=1;n<=5;n++)
{
cout<<"\nValue of n:"<<n<<endl;

}
}

This would produce following result:

Value of n = 1 Value of n = 2 Value of n = 3 Value of n = 4 Value of n = 5
Note:More interested things are waiting to publish..Stay connected to us..
knowledgediarybd.com © 2017
shares