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)
    {  

Leave a Reply

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