Branching Control Statement in C++

 Control Statement in C++

Control statements are that change the flow of execution of statements. To control the sequence of operations in a program Control Statements are used.


Control Statements are of 3 Types which are furthur classified:


  • Branching 
  • Looping
  • Jumping

Branching statement also known as conditional statement which execute a program until the condition is satisfied.

Branching Include: 

  1. if statement(simple if)
  2. if else statement
  3. else if statement
  4. switch statement


if statement or simple if it is used to test the condition. This statement is execute if the condition is                                                 True.
Syntax:-
               if(expression){
                         //code;
                          }
Example:-
                   if(marks>=80){
                      cout<<"Admission is Possible"<<endl;
                    } 

if else statement In this statement the program is executed in both the codition wheather the given                                    codition is True or False. In the Simple words, Program will be executed in both                                      condition.
Syntax:-
               if(expression){
                         //code;
                          }
                 else{
                      //code;
                       }
Example:-
                   if(age>=18){
                      cout<<"candidate is eligible for voting"<<endl;
                    } 
                   else{
                       cout<<"age is less"<<endl;
                        }
            
else if statement In this statement it executes one condition from multiple statements. It is also known                                 as else if ladder.

 Syntax:-
               if(expression){
                         //code;
                          }
                 else if(expression){
                      //code;
                       }
                else if(expression){
                      //code;
                       }
                else{
                     //code;
                     }

Example:-
                   if(age>=21){
                      cout<<"Person is eligible for marriage"<<endl;
                    } 
                   else if(age>=18){
                       cout<<"Person is eligible for voting"<<endl;
                        }
                   else{
                       cout<<"Age is minimum"<<endl;
                       } 




Switch Statement or Switch Case
     If you want to select one statement out of many statements then                                                                  switch statement is used.

Syntax:-
                 switch(expression){
                  case a:
                          //set of instruction;
                    break;
                   case b:
                          //set of instruction;
                    break;
                  default:
                  //set of instruction;
                    }

Example:-        
                   char car;
                   cin>>car;
                   switch(car){
                   case a:
                         cout<<"maruti suzuki"<<endl;
                       break;
                     case b:
                         cout<<"Tata Motors"<<endl;
                       break;
                     case c:
                         cout<<"Mahindra Motors"<<endl;
                       break;
                    default:
                             cout<<"No more options"<<endl;
                      }


Comments

Popular posts from this blog

C++ Tutorial

My Social Media Account