Skip to main content

8. Looping Statement

 ➢ Looping Statement :-

            In computer programming, loop cause a certain piece of program to be executed a certain number of times. Consider these scenarios: 

            • You want to execute some code/s certain number of time. 

            • You want to execute some code/s certain number of times depending upon input from user. 

            These types of task can be solved in programming using loops. 

            There are 3 types of loops in C++ Programming: 

                        • for Loop 

                        • while Loop 

                        • do...while Loop 


1) while Loop :-

    Syntax of while Loop :-

    while (test expression) 

   { 

             statement/s to be executed. 

   } 

                 The while loop checks whether the test expression is true or not. If it is true, code/s inside the body of while loop is executed, that is, code/s inside the braces { } are executed. Then again the test expression is checked whether test expression is true or not. This process continues until the test expression becomes false.


Flowchart of while Loop in C++ :- 




Example Program:-

//C++ program to find factorial of a positive integer entered by user.
#include <iostream.h>
#include <conio.h>
int main()
{
        int n, i = 1, fact = 1;
        cout<< "Enter a positive integer: ";
        cin >> n;
        while ( i <= n)
        {
                fact=fact*i;
                i++;
        }
        cout<<"Factorial of "<<n<<" = "<<fact;
        getch();
}

Output:-

Enter a positive integer:5
Factorial of 5 : 120


2) do...while Loop :-

                The do...while Loop is similar to while loop with one very important difference. In 
while loop, check expression is checked at first before body of loop but in case of do...while loop, 
body of loop is executed first then only test expression is checked. That's why the body of 
do...while loop is executed at least once.

Syntax of do...while Loop :-

do 
{
         statement/s;
}
while (test expression); 

               The statement/s inside body of loop is executed at least once, that is, the statement/s inside braces { } is executed at least once. Then the test expression is checked. If the test expression is true, the body of loop is executed. This process continues until the test expression becomes false. Since the body of loop is placed before the test expression in do...while loop, the body of loop is executed at least once.




Example Program :-

//C++ program to print the numbers from 1 to 5
#include <iostream.h>
#include <conio.h>
int main()
{
         int a=1;
         do
         {
                 cout<<a<<"\t";
                 a++;
         }
         while(a<6);
         getch();
}

Output:-

1 2 3 4 5


3) for Loop:-

Syntax:-

for(initialization statement; test expression; update statement) 
{
         code/s to be executed; 
}


                The initialization statement is executed only once at the beginning of the for loop. Then the test expression is checked by the program. If the test expression is false, for loop is terminated. But if test expression is true then the code/s inside body of for loop is executed and then update expression is updated. This process repeats until test expression is false. 

Flowchart of for Loop in C++  :-




Example Program:-

//C++ Program to find factorial of a number
#include <iostream.h>
#include <conio.h>
int main()
{
             int i, n, fact = 1;
             cout<<"Enter a positive integer: ";
             cin>>n;
             for (i = 1; i <= n; ++i)
             {
                         fact=fact*i;
             }
             cout<< "Factorial of "<<n<<" = "<<fact;
             getch();
}


Output :-

Enter a positive integer: 5
Factorial of 5 = 120

Comments

Popular posts from this blog

C++ programming language

                c++ is a general programming language and is widely used nowadays for competitive programming. It has imperative, object-oriented and generic programming features.C++ runs on lots of platforms like Windows, Linux, Unix, Mac etc.   ❖❖  C++ with oops ❖❖

7. Switch Statement in c++

 ❖ C++ switch Statement :-                Consider a situation in which, only one block of code needs to be executed among many blocks. This type of situation can be handled using nested if...else statement but, the better way of handling this type of problem is using switch...case statement.  Syntax of switch:-      Switch(expression) {            Case value1 : statement1;                       Break;             Case value2 : statement2;                      Break;             Default : default statements;  }               The expression is either an integer or a character in above syntax. If the expression matches constant in case, the relevant codes a...

3. Input /output in c++

  ➢   Input/ Output in C++ :-        C++ comes with libraries which provides us many ways for performing input and output. In C++.        input and output is performed in the form of sequence of bytes or more commonly known as  streams. ● Input Stream: If the direction of flow of bytes is from device(for example: Keyboard) to the main memory then this process is called input. ● Output Stream: If the direction of flow of bytes is opposite, i.e. from main memory to device(display screen ) then this process is called output. ● Header files available in C++ for Input – Output operation are: • iostream: iostream stands for standard input output stream. This header file contains definitions to objects like cin, cout, cerr etc. • fstream: This header file mainly describes the file stream. This header file is used to handle the data being read from a file as input or data being written into the file as output.       ...