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 ❖❖

Pratical -206 paper solution -3

Full course of c++ programming language Click here > C++ programming language Q-1 [A] Write a c program to create structure of employee with members Empid, EmpName, Qualification and EmpSalary by taking input of 5 employees display the employee whose qualification is "MBA" and salary greater than 20000 . #include <stdio.h> #include <conio.h> #include <string.h> struct Employee {     int Empid, EmpSalary;     char EmpName[50];     char Qualification[50]; }; Void main() {     struct Employee emp[5];     int i;     for (i = 0; i < 5; i++)     {         printf("Enter employee ID: ");         scanf("%d", &emp[i].Empid);         printf("Enter employee name: ");         scanf("%s", emp[i].EmpName);         printf("Enter employee qualification: ");         scanf("%s", emp[i].Quali...

9. Jumps Statement in c++

➢ Jumps in Loops:- ➢ C++ break and continue Statement:-                     There are two statements (break; and continue ;) built in C++ programming to alter the normal flow of program.                     Loops are used to perform repetitive task until test expression is false but sometimes it is desirable to skip some statement/s inside loop or terminate the loop immediately with checking test condition. On these type of scenarios, continue; statement and break; statement is used respectively. The break; statement is also used to terminate switch statement. 1) break Statement :-                       The break; statement terminates the loop(for, while and do..while loop) and switch statement immediately when it appears . Syntax of break :- break;              ...