Difference between While and Do-While Loop in C++

Master C++ with Real-time Projects and Kickstart Your Career Start Now!!

Program 1

#include<iostream>
using namespace std;
int main()
{
    system("cls");
    int i=11;
    cout<<"\n-------While-----------\n";
   while(i<=10)
    {
      cout<<i<<endl;
      i++;
  }

  i=11;
  cout<<"\n-------Do While----------\n";
  do
  {
      cout<<i<<endl;
      i++;
  }while(i<=10);

    return 0;
}

Program 2

#include<iostream>
using namespace std;
int main()
{
    int choice,a,b;

    system("cls");
   do
   {
      cout<<"\n---------Menu----------"<<endl;
      cout<<"1. Add\n 2.Swap \n 3. Exit"<<endl;
      cout<<"\n-------------------------"<<endl;
      cout<<"\nEnter your choice: ";
      cin>>choice;
      switch(choice)
     {
      case 1: 
      {
         cout<<"\nEnter two number";
         cin>>a>>b;
         cout<<"\nAddition is: "<<a+b;
         break;
      }
    case 2: 
    {
         cout<<"\nEnter two number";
         cin>>a>>b;
         cout<<"\nBefore Swaping: "<<a <<"  "<<b;
         a=a+b;
         b=a-b;
         a=a-b;
         cout<<"\nAfter Swaping: "<<a <<"  "<<b;
         break;
    }
    case 3:break;
    default:cout<<"\nInvalid choice";
  }  

 }while(choice!=3);   

    return 0;
}

Program 3

#include<iostream>
using namespace std;
int main()
{
    system("cls");

for (int i = 1; i <= 2; i++)     
{
    for (int j = 1; j <= 3; j++) 
    {     
             continue;
             cout<<i <<"   "<<j<<endl;  
    }
    cout<<"Hello";
}

   
    return 0;
}

Did you like our efforts? If Yes, please give DataFlair 5 Stars on Google

courses

DataFlair Team

DataFlair Team provides high-impact content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. We make complex concepts easy to grasp, helping learners of all levels succeed in their tech careers.

Leave a Reply

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