Saturday 1 August 2015

if-else Statements :


if else statement is another type of if statement. It executes one block of statements when the condition is true and the other when it is false. When the if condition becomes false then else block gets executed.

Conditions :


  • Both blocks can never be executed.
  • Both blocks can never be skipped.


Syntax 


if-else syntax is as follows :

             if (condition)
                 statement;
             else
                 statement;

Two or more statements are written in curly braces { }. The syntax is as follows :

             if (condition) {
                statements 1;
                statements 2;
                .
                .
               statements n;
                                    }
            else {
               statement 1;
               statement 2;
               .
               .
               statement n;
                    }

if else Flowchart :



if else in cpp


Example :

#include <iostream>
using namespace std;
 
int main ()
{
   // declare local variable
   int marks = 30;
 
   // check the condition
   if( marks > 40 )
   {
       // if condition is true
       cout << "You are pass !!" << endl;
   }
   else
   {
       // if condition is false
       cout << "You are fail !!" << endl;
   }
 
   return 0;
}

Output :

You are fail !!

No comments:

Post a Comment