Loading...
C++

Condition Or Branching Statements

Decision making is about deciding the order of execution of statements based on certain conditions. C++ handles decision making by supporting the following statements.
1. Simple if statement
2. if…..else statement
3. Nested if…..else statement
4. else…..if statement
5. switch-case

  1. Simple if Statement:
    It is a conditional branching statement. In conditional branching statement a condition is evaluated, if it is true a group of statements are executed.
    Syntax:
    if(condition)
    {
    //Statements
    }
    If the expression is true, then the statements in side the block are executed, if the expression is false, then the statements in the block are skipped and statements outside the block are executed.
    Example:
    #include<iostream>
    using namespace std;
    void main()
    {
    int number;
    cout<<“Enter a integer:”;
    cin>>number;
    if(number>0)
    {
    cout<<“The given number is positive integer:”<<number<<endl;
    }
    cout<<“The given number is negative”;
    }
  2. if…..else statement:
    In if….else statement is one among the conditional statement. If the given condition is true then the if block statements are executed if the condition is false then the else block statements are executed.
    Syntax:
    if(condition)
    {
    if block statements;
    }
    else
    {
    else block statements;
    }

    Example:
    #include<iostream>
    using namespace std;
    void main()
    {
      int number;
      cout<<“Enter a integer:”;
      cin>>number;
      if(number>=0)
      {
        cout<<“The given number is a positive integer:”<<number<<endl;
      }
      else
      {
         cout<<“The given number is a negative integer:”<<number<<endl;
      }
      cout<<“The program is executed”  ;
    }

  3. Nested if else:
    It is same as if else statement, where a new block of if else statement is defined in existing if or else block statements.
    Syntax:
    if(condition)
    {  
      if(condition)
    {
       statement;
     }
     else
     { 
       statement;
     }
    }
    else

      statement;
    }
  4. Else if Statement:
    As if else statement are too complicated to write when they are nested more deeply. To reduce this complication else if clause can be used.
    Syntax:
    if (expression1)
    {
     Statements;
    }
    else if(expression2)
    {
       Statements;
    }
    else
    {
      Statements;
    }

    Example:
    #include<iostream>
    using namespace std;
    void main()
    {
      int number;
      cout<<“Enter an integer:”;
      cin>>number;
      if(number>0)
      {
        cout<<“The entered number is positive integer:”<<number<<endl;
      }
      else if(number<0)
      {
        cout<<“The entered number is negative integer:”<<number<<endl;
      }
      else
      {
        cout<<“The entered number is : 0”;
      }
    }
  5. Switch Case Statement:
    Switch case statement is used when user has multiple alternatives of codes to chose, depending upon the value of single variable.
    Syntax
    switch(Expression)
    {
      case 1:
           statement;
           break;
      case 2:
            statement;
            break;
      case 3:
            statement;
            break;
       default:
            statement;
    }
    Example:
    #include<iostream>  
    using namespace std;
    void main()
    {
      char c;
      float num1, num2;
      cout<<“Enter and operator(+, -, *, /): “;
      cin>>c;
      cout<<“Enter two operands:”;
      cin>>num1>>num2;
      switch(c)
      {
         case ‘+’:
             cout<<num1<<“+”<<num2<<“=”<<num1+num2;
             break;
         case ‘-‘:
              cout<<num1<<“-“<<num2<<“=”<<num1-num2;
              break;
          case ‘*’:
              cout<<num1<<“*”<<num2<<“=”<<num1*num2;
              break;
          case ‘/’:
               cout<num1<<“/”<<num2<<“=”num1/num2;
               break;
           default;
                cout<<“Error! operator is not correct”;
                break;
       }
    }

          
       

Leave a Reply

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