C++ Control flow

Control flow is a process which are manage the execution of instructions, statement and function calling in a runtime of program. That is very important to control execution flow and achieve the desired output. In C++ programming are provide certain decision making statement to control execution flow of program. there are as follows.

If statements

If is a conditional statement that are check given expression (conditions). if this expression are true (valid) then inside this condition statement executes of all given statement one by one.

if(expression){
  //code-statements
}

note that this expression are true (condition valid or 1). then executes inside this block of statement and instruction. for example.

//Example of if statements
#include<iostream>
using namespace std;
int main(){
   int age=21;
   if(age>18){
    // if age is big then 18
    //then execute this block statements
     cout<<" Age is greater than 18"<<endl;

   }
   return 0;
}
Output
 Age is greater than 18

That is also reliable to check exception and valid input value of program.

//Example of if statements
#include<iostream>
using namespace std;

void divide(int value,int divider){

  if(divider==0){
    cout<<"Opps divide by zero try again!"<<endl;
    return; //back to main
  }
  cout<<"Result is :"<<(value/divider)<<endl;
}
int main(){
   
   divide(10,0);
   divide(10,2);
   return 0;
}
Output
Opps divide by zero try again!
Result is :5

else if statements

When we need to check multiple different condition in same expression then else if statement are useful. this are executed when previously defined (if) condition is not valid.

if(expresion1){
  //logical code
}else if(expression2){
  //logical code
  //group of statements
}else if(expression3){
  //logical code
  //group of statements
}

For example

//Example of else if statements
#include<iostream>
using namespace std;
int main(){
  int result=94;
  
  if(result<33){
    cout<<"Opps you are fail"<<endl;
  }else if(result>33 && result <60){
    cout<<"Pass are Second division"<<endl;
  }
  else if(result>60 && result<80){
    cout<<"Pass are First division and Average result"<<endl;
  }
  else if(result>80){
    cout<<"Pass are First division and Excellent result"<<endl;
  }
  return 0;
}
Output
Pass are First division and Excellent result

Observe that if else is an channing form. if one is valid then not execute or check another related conditional statements. and without (if statement) cannot possible (else if statement).

else statement

else is default statement which are executed when not satisfied any (if, and else if) condition.

//Example of else if statements
#include<iostream>
using namespace std;
int main(){
  int score=410;
  if(score<250){
    cout<<"Good defends of Bowling Team";
  }else if(score<320){
    cout<<"Good total score";
  }else{
    cout<<"Big total score";
  }
  return 0;
}
Output
Big total score

Switch cases

Switch case are suitable for when we are check multiple condition on a single variable.They are normally used in programming. see the syntax.

switch(expression){
  case constant-condition1:
    //statements
  break;
  case constant-condition2:
    //statements
  break;
  case constant-condition3:
    //statements
  break;
  //cases
  ..
  ..
  ..
  ..
  ..
  ..
  default:
  //statements
}

switch are accept one argument this is can be integer or character. switch are accepts one argument value. this is can be integer or character. inside of switch there are multiple test case statement this are check relative argument.and execute those cases which are related to given argument of switch.

Note that break are used in end of case condition. and this is normally used to terminate switch statements. and if not match any case then executes default statement. example of switch case statements

//Example of switch statements
#include<iostream>
using namespace std;
//display message 
int choice(){
  int value;
  cout<<"Choose Your Best Programming Language :"<<endl;
  cout<<"1 : C Programming"<<endl;
  cout<<"2 : C++ Programming"<<endl;
  cout<<"3 : Java"<<endl;
  cout<<"4 : Python"<<endl;
  cout<<" Enter Choose :";
  cin>>value;
  return value;
}
int main(){

  int language=choice();
  /*
    switch : keyword
    expression : langugae
  */
  switch(language){
    case 1:
      cout<<"C Programming";
    break;
    case 2:
      cout<<"C++ Programming";
    break;
    case 3:
      cout<<"Java Programming";
    break;
    case 4:
      cout<<"Python Programming";
    break;
    default:
      cout<<"Opps invaild option";
  }
  return 0;
}
Output
Choose Your Best Programming Language :
1 : C Programming
2 : C++ Programming
3 : Java
4 : Python
 Enter Choose :2
C++ Programming

If case are not terminate by break statement then there are executes of next case statement until not find a break statements.

#include<iostream>
using namespace std;

int main(){
  
  int status=2;
  switch(status){

    case 1:
      cout<<" case 1"<<endl;
      //no break statements
    case 2:
      cout<<" case 2"<<endl;
      //no break statements
    case 3:
      cout<<" case 3"<<endl;
    break;
    case 4:
      cout<<" case 4"<<endl;
    default:
      cout<<"default option";
  }
  return 0; 
}
Output
 case 2
 case 3

Note that in this program not given break condition of case 1 and case 2. of given program status value equal to two. so case 2 are satisfied valid true condition. and there are not an break condition. then this situation it will executing another next case statement. and case 3 are break condition then stops the execution of this switch case.

© 2019, regularcodes.com, All rights reserved