Looping Statements in C++

In a programming often a situation may comes where we want to  execute the set of code again and again.

Loop has a ability to repeat set of statement until a condition to be satisfied or a particular number of times.

There are following types of  loop in C++  language:

In a C++ programming language we can also define a loop within another loop.

  • while loop
  • do…while loop
  • for loop
  • nested loops

while loop

“while loop” repeats a set of statements if condition is true.

If condition is false the flow of control do not enter inside of while loop.

It first tests the condition then execute the body of loop.

Syntax of a while loop −

Example: Write a program to print 1 to 10 using while loop.

Output:

Description: In the above program, line “while( a <= 10 )” will check if condition is true or false.

In the above case when a=1, condition is true and flow of control move inside of while and print 1 after that increment occurs.

Now a=2, again condition is true and flow of control move inside of while and print 2 after that increment occurs.

Now a=3, again condition is true and flow of control move inside of while and print 3 after that increment occurs.

This process is continue until condition is false(in this case condition false occur when a=11).

Example: Write a program to print 10 to 1 using while loop.

Output

do-while loop 

like a while loop,  “ do while loop” repeats a set of statements if condition is true.

If condition is false then the flow of control do not enter inside the do-while loop.

“do-while” first execute the body of loop even if the condition is false than tests the condition.

Means even if the condition if false do-while executes at least ones.

The syntax of a do…while loop in C programming language is −

Example: Write a program to print 1 to 10 using while loop.

Output

Even if the condition if false do-while executes at least ones.

Output

Description: in the above program do-while executes ones even condition of is false.

Because do-while first executes the statement then check the condition. 

For Looping Statement

“for-loop” repeat the set of statement until a particular condition to be satisfied or a particular number of times.

Syntax of for-loop:

“for-loop” has a initialization , condition and iteration (increment/decrement) part separated by semicolon.

Initialization : This phase allows you to initialize loop control variables.

Condition : If condition of for-loop is true, then the body of the loop will execute.

And the body of the loop does not execute if the condition is false, and the flow of control jumps to the next statement just after the ‘for’ loop

Iteration phase: This statement allows you to update(increment/decrement any loop control variables..

Example: Write a program to print 1 to 10 using for-loop.

Output

In a for loop either initialization or iteration , or both may be absent but condition must be present in a for loop.

Output

Example: Write a program to take a number from user and find its factorial.

Output

Example: Write a program to take a number from user and check whether a number (entered by the user) is a prime number or not.

  Prime Number : prime number is a number which  satisfies the following conditions.

  • It should be whole number
  • It should be greated than 1
  • It should have only 2 factors. They are, 1 and the number itself.

 Example for prime numbers: 2, 3, 5, 7, 11, 13, 17, 19, 23,29 etc. because this numbers is only divided by 1 and the number itself.

Number  4, 6, 8, 9, 10, 12, 14, 15, 16 etc are not prime numbers. Because, the number 4 can be divided by 2. 

As per the rule of prime number,  should be divide 2 numbers only.

They are 1 and the number itself.  But, number 4 is also divided by 2. Similarly , all remaining numbers 6, 8, 9, 10, 12, 14……..  also divided by a number other than 1 and the number itself.

Therefore these are not a  prime numbers.

Number 1 is neither a prime nor a composite number.

Output