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

Remove specific item from Recyclerview in android studio

For Full video click on below link :- https://youtu.be/sUz4fqeanjI?si=A8AqBWCgu-5NnBOJ Working with the activity_main.xml file. Navigate to the   app > res > layout > activity_main.xml  and add the below code to that file. Below is the code for the   activity_main.xml   file.  <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" android:orientation="vertical" android:background="@color/white"> <EditText android:layout_width="350dp" android:layout_height="50dp" android:inputType="text" android:id="@+id/coursenam...

Fingerprint Authentication in Android Studio Project

Implement the implementation : - implementation 'androidx.biometric:biometric:1.0.1' Working with the activity_main.xml file. Navigate to the   app > res > layout > activity_main.xml  and add the below code to that file. Below is the code for the   activity_main.xml   file. <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:orientation="vertical" tools:context=".MainActivity" android:background="@color/white"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Welcome to the Boxcode!!" android:textSize=...

programming skills imp programs

Full course of c++ programming language Click here >  C++ programming language ===================================== 1. Write a program to display an average of 3 inputted numbers. a=int(input("Enter a : ")) b=int(input("Enter a : ")) c=int(input("Enter a : ")) avg=(a+b+c)/3 print(avg) ====================================== 2. Write a program to calculate simple interest for a given principle amount, rate of interest and no of years. p=float(input("Enter a P : ")) r=float(input("Enter a R : ")) n=float(input("Enter a N : ")) si=(p*r*n)/100 print("simple interest is ",si) ====================================== 3. Write a program to display area of triangle. base=float(input("Enter a base : ")) height=float(input("Enter a height : ")) area=base*height/2 print("Area of triangle is ",area) ====================================== 4. Write a program to calculate area of circle. r=float(input...