CPP02 – Write a CPP program to explain the use of for loop, while loop, switch-case, break and continue statements.

By | October 15, 2018

CPP02 – Write a C++ program to explain the use of for loop, while loop, switch-case, break and continue statements.

CODE:-

#include<iostream.h>
#include<conio.h>
void main()
{
int i;
clrscr();
cout<<“FOR LOOP”<<endl;
for(i=1;i<6;i++)
cout<<i<<endl;
cout<<“WHILE LOOP”<<endl;
while(i>1)
cout<<–i<<endl;
cout<<“DO WHILE LOOP”<<endl;
do
{
cout<<i++<<endl;
}while(i<6);
getch();
}

#include<iostream.h>
#include<conio.h>
void main()
{
 int i;
 clrscr();
 cout<<"FOR LOOP"<<endl;
 for(i=1;i<6;i++)
   cout<<i<<endl;
 cout<<"WHILE LOOP"<<endl;
 while(i>1)
  cout<<--i<<endl;
 cout<<"DO WHILE LOOP"<<endl;
 do
 {
   cout<<i++<<endl;
 }while(i<6);
 getch();
}

SCREEN SHOTS:-

CPP_program_Loops

OUTPUT:-

CPP_program_Loops_Output

 

CODE:-

#include<iostream.h>
#include<conio.h>
void main()
{
int i=0;
clrscr();
cout<<“Odd Numbers with CONTINUE statement”<<endl;
do
{
if(i%2==0)
{
i++;
continue;
}
cout<<i++<<endl;
}while(i<10);
i=0;
cout<<“Odd Numbers with CONTINUE statement”<<endl;
do
{
if(!(i%2==0))
{
i++;
continue;
}
cout<<i++<<endl;
}while(i<10);
cout<<“Printing Odd or Even Number with SWITCH CASE BREAK statements”<<endl;
i=0;
do
{
cout<<i++;
switch(i%2)
{
case 0:  cout<<” : Even Number”<<endl;
break;
case 1:  cout<<” : Odd  Number”<<endl;
break;
default: cout<<“Invalid Data”<<endl;
break;
}
}while(i<10);
getch();
}

#include<iostream.h>
#include<conio.h>
void main()
{
 int i=0;
 clrscr();
 cout<<"Odd Numbers with CONTINUE statement"<<endl;
 do
 {
   if(i%2==0)
   {
    i++;
    continue;
   }
   cout<<i++<<endl;
 }while(i<10);
 i=0;
 cout<<"Odd Numbers with CONTINUE statement"<<endl;
 do
 {
   if(!(i%2==0))
   {
    i++;
    continue;
   }
   cout<<i++<<endl;
 }while(i<10);
 cout<<"Printing Odd or Even Number with SWITCH CASE BREAK statements"<<endl;
 i=0;
 do
 {
   cout<<i++;
   switch(i%2)
   {
    case 0:  cout<<" : Even Number"<<endl;
         break;
    case 1:  cout<<" : Odd  Number"<<endl;
         break;
    default: cout<<"Invalid Data"<<endl;
         break;
   }
 }while(i<10);
 getch();
}

SCREEN SHOTS:-

CPP_program_Continue_Switch_Break

OUTPUT:-

CPP_program_Continue_Switch_Break_Out