Thursday, 17 November 2016

Looping Structure in C++

Loops: 

A type of control structure that repeats a statement or set of statements is known as looping structure. It is also known as iterative or repetitive structure.  In some situations,  it is required to repeat a statement or set of statements for a specified number of times, Looping structures are used for this purpose.

Uses of Loops: 

Loops are basically used for two purpose: 

  • To execute a statement or number of statements for a specified number of times.For example, a user want to display a specific statement on screen for 10 times. 
  • To use a sequence of values. For example, a user may display a set of whole numbers from 0 to 10.

Types of  Loops:

Counter-Controlled Loop: 

This type of  loop depends on the value of variable known as counter variable.The value of this counter variable increments or decrements each time the body of the loop is executed. The loop terminates when  the value of counter variable reaches  a that particular values set as limit. In counter-control loop the number of iterations are known in advance. The iteration of this loop depends on the following factors: 
  • Initial value of Counter variable
  • Termination condition
  • Increment  or decrement value. 

Sentinel-Controlled Loop: 

This type of loop depends on special value known as sentinel value. This loop terminates when that specific sentinel value encountered. These types of loops are also known as conditional loop.For example,  a loop may continue its execution until user entered -1. Here -1 is known as sentinel value that is used for terminate the loop. The number of iteration in sentinel-controlled loop is unknown, it terminates only when user enters the sentinel value. In while and do-while loops a sentinel value is used. 

There are three types of loops available in C++. These are as follows:
  • while loop
  • do-while loop
  • for loop

for  loop: 

for loop  executes one or more statements for a specific number of times. This loop is also called counter-controlled loop. It is most flexible loop. It is most frequently used loop by the programmers.

Syntax:

The syntax for loop is as follow:

    for(initialization; condition; increment/decrement)
    {
              body of the loop
     }

   for ( i =1; i<=10; i++){ loop body }

Flowchart :


Write a program that display counting from 1 to 5 using for loop.

        
        #include <iostream.h>
        #include <conio.h>
        int main ()
        {
           int i; 

           for(i =1; i <= 5; i++)
           {
                cout<< << endl;
           }
            return 0
        }


while loop: 

while loop is the simplest loop in C++ language. This loop executes one or more statements while the given condition remain true. It is useful when the number of the iterations is known in advance.

Syntax:

The syntax while loop is as follow:

while (condition)
    {

              body of the loop

              increment/decrement
     }

Flowchart :


Write a program that display counting from 1 to 5 using while loop.

        
        #include <iostream.h>
        #include <conio.h>
        int main ()
        {
           int i; 

           while( i <= 5)
           {
                cout<< << endl;
                i++
           }
            return 
        }

do-while loop: 

do-while loop is an iterative control in C++ language. This is executes one or more statements while the given condition  is true. In this loop, the condition comes after the body of the loop. It is an important loop in a situation when the loop body must be executed at least one. 

Syntax:

The syntax do-while loop is as follow:

do 
    {

              body of the loop

              increment/decrement
     }while (condition);

Flowchart :


Write a program that display counting from 1 to 5 using do-while loop.

        
        #include <iostream.h>
        #include <conio.h>
        int main ()
        {
           int i; 

          do
           {
                cout<< << endl;
                i++
           } while( i <= 5);

            return 
        }


Programs:

1 comment:

If you have any doubt please let me know

Methods in JAVA Language

 In general, a way may be thanks to perform some task. Similarly, the tactic in Java may be a collection of instructions that performs a sel...