Blog Archive

PG -TRB COMPUTER INSTRUCTOR C plus plus Class : 4 switch do while

 




C++ switch

The C++ switch statement executes one statement from multiple conditions. It is like if-else-if ladder statement in C++.

switch(expression)

{      

case value1:      

 //code to be executed;      

 break;    

case value2:      

 //code to be executed;      

 break;    

......      

      

default:       

 //code to be executed if all cases are not matched;      

 break;    

}    



C++ Switch Example

#include <iostream>  

using namespace std;  

int main () {  

       int num;  

       cout<<"Enter a number to check grade:";    

       cin>>num;  

           switch (num)    

          {    

              case 10: cout<<"It is 10"break;    

              case 20: cout<<"It is 20"break;    

              case 30: cout<<"It is 30"break;    

              default: cout<<"Not 10, 20 or 30"break;    

          }    

    }    

 

Output:

Enter a number:
10
It is 10

Output:

Enter a number:
55
Not 10, 20 or 30
 

C++ For Loop

The C++ for loop is used to iterate a part of the program several times. If the number of iteration is fixed, it is recommended to use for loop than while or do-while loops.

The C++ for loop is same as C/C#. We can initialize variable, check condition and increment/decrement value.

 

 

for(initialization; condition; incr/decr)

{    

//code to be executed    

}    



 

C++ For Loop Example

#include <iostream>  

using namespace std;  

int main() {  

         for(int i=1;i<=10;i++)

{      

            cout<<i <<"\n";      

          }       

    }   

 

Output:

1
2
3
4
5
6
7
8
9
10

 

C++ While loop

In C++, while loop is used to iterate a part of the program several times. If the number of iteration is not fixed, it is recommended to use while loop than for loop.

while(condition)

{    

//code to be executed    

}    



C++ While Loop Example

#include <iostream>  

using namespace std;  

int main() {         

 int i=1;      

         while(i<=10)   

       {      

            cout<<i <<"\n";    

            i++;  

          }       

    }  

 

 

Output:

1
2
3
4
5
6
7
8
9
10

 

C++ Do-While Loop

The C++ do-while loop is used to iterate a part of the program several times. If the number of iteration is not fixed and you must have to execute the loop at least once, it is recommended to use do-while loop.

The C++ do-while loop is executed at least once because condition is checked after loop body.

Do

{    

//code to be executed    

}while(condition);  



C++ do-while Loop Example

Let's see a simple example of C++ do-while loop to print the table of 1.

 

 

#include <iostream>  

using namespace std;  

int main() {  

     int i = 1;    

          do{    

              cout<<i<<"\n";    

              i++;    

          } while (i <= 10) ;    

 

 

Output:

1
2
3
4
5
6
7
8
9
10

PG -TRB COMPUTER INSTRUCTOR C plus plus Class : 4 switch do while

  C++ switch The C++ switch statement executes one statement from multiple conditions. It is like if-else-if ladder statement in C++. sw...