do while loop in C C++

do while loop in C C++

Loops are used to repeat some steps until the desired condition is satisfied. Loops basically execute certain piece of code which programmer does not want to write again and again. For e.g. if I want print numbers from 1 to 100, I have to declare 100 variables assign values to them and print them. It will definitely make my program large and will consume a lot of memory. Thus to avoid this problems looping structure provides easiest way by re-executing everything written inside it. do – while loop in C and C++ is a post-tested loop. Which means irrespective of the condition the loop will execute atleast once. Whereas while loop is pre-tested loop. It’s general syntax is

Syntax -
do
{
  task1;
  task2;
  task3;
}while(condition);

Let’s see an example to understand the concept more clearly.

Q. Write a program in C and C++ to show use of do..while loop.

1. Program

C Code :

//do...while program to create a simple calculator
#include<stdio.h>

int main()
{
    int a, b, opt;

    do
    {
        printf("\n\n Simple Clculator");
        printf("\n 1. Addition.");
        printf("\n 2. Subtraction.");
        printf("\n 3. Multiplication.");
        printf("\n 4. Division.");
        printf("\n 5. Exit.");

        printf("\n\n Select operation :");
	    scanf("%d", &opt);

        switch(opt)
        {
            case 1 : printf("\n Enter two nos:");
                     scanf("%d%d", &a, &b);
                     printf("\n Addition is : %d",(a+b));
                     break;

            case 2 : printf("\n Enter two nos:");
                     scanf("%d%d", &a, &b);
                     printf("\n Subtraction is : %d",(a-b));
                     break;

            case 3 : printf("\n Enter two nos:");
                     scanf("%d%d", &a, &b);
                     printf("\n Multition is : %d",(a*b));
                     break;

            case 4 : printf("\n Enter two nos:");
                     scanf("%d%d", &a, &b);
                     printf("\n Division is : %d",(a/b));
                     break;

            case 5 : printf("\n Thank You!");
			         break;

            default : printf("\n You entered wrong number");
                      break;
        }
    } while (opt != 5);
    
    return 0;
}

C++ Code :

//do...while program to create a simple calculator
#include<iostream>

using namespace std;

int main()
{
    int a, b, opt;

    do
    {
        cout<<"\n\n Simple Clculator";
        cout<<"\n 1. Addition.";
        cout<<"\n 2. Subtraction.";
        cout<<"\n 3. Multiplication.";
        cout<<"\n 4. Division.";
        cout<<"\n 5. Exit.";

        cout<<"\n\n Select operation :";
	    cin>>opt;

        switch(opt)
        {
            case 1 : cout<<"\n Enter two nos:";
                     cin>>a>>b;
                     cout<<"\n Addition is : "<<(a+b);
                     break;
                     
            case 2 : cout<<"\n Enter two nos:";
                     cin>>a>>b;
                     cout<<"\n Subtraction is : "<<(a-b);
                     break;
                     
            case 3 : cout<<"\n Enter two nos:";
                     cin>>a>>b;
                     cout<<"\n Multition is : "<<(a*b);
                     break;
                     
            case 4 : cout<<"\n Enter two nos:";
                     cin>>a>>b;
                     cout<<"\n Division is : "<<(a/b);
                     break;

            case 5 : cout<<"\n Thank You!";
			         break;
                     
            default : cout<<"\n You entered wrong number";
                      break;
        }
    } while (opt != 5);
    
    return 0;
}

2. Output :

do while loop in C and C++

3. Program Parts :

1. #include is a pre-processor directive. It is used to include header files.
2. stdio/iostream is header file which has certain commands that c/c++ supports. E.g. return, main, etc.
3. .h is an extension for header file.
4. int is data type. It indicates that the program returns a value.
5. main() is main function that indicates the compiler that the user written programs starts from here.
6. do … while() is a looping structure.
7. print/cout is the keyword use to print a message.
8. scanf/cin is the keyword use to store values in variables.
9. switch is keyword in C/C++. It will take value stored in opt and will check with every case inside the switch block.
10. return statement is use to return a value. If any error occurs the program will return 0.

4. Explanation :

In this program we are going to create a simple calculator using do…while loop. First without checking any condition the compiler will enter the loop. Because it is post tested loop. So it will ask user which operation it wants to perform. It will store the input in opt variable and enter the switch case. In switch case the value of opt will be compared with each case and respective case will be executed. After first iteration (i.e. successfully executing loop once) the value of opt variable is checked. If it is not equal to 5 then the loop continues to execute. As soon as it’s value becomes 5 the compiler comes out of loop. And the program execution stops.

Conclusion

So this was the concept of do…while loop in C and C++. Hope we made it very vlear to use. If you have any doubt regarding any concept or step just do mention it in the comment section below. Do share this with your friends and programming buddies.

For daily updates do follow us on Instagram! Visit again.

ErrorFreeProgram.

4 COMMENTS

Leave a Reply

Your email address will not be published. Required fields are marked *