Condition Statements

Condition Statements Dec. 5, 2020

Conditional Statements

Conditional Statements are the decision making statements. if, if...else..., switch are the examples of conditional statements. They uses the condition to decide before executing the statements inside the conditional statements.

Mainly there are two type of conditional statements:

  1. if
  2. if...else...
  3. nested if...else...
  4. if...else...ladder
  5. switch

1. if statement

        if statements takes a condition. The condition can be either true or false. if statements has one block. It is for if statement. If the condition is true then if block would be executed. In this statement we don't think about the action for false statement.

Example:

A teacher asks a question to a student.

If the student answers true then

       he would be awarded else nothing will do. 

if ( answer is true ) :
        award
/*
C, C++, PHP, JAVA and JavaScript
if(condition)
{
      statements;
}

python
if(condition):
      statements
*/

if( x % 2 == 0)
{
       printf("x is even")                          //C and C++
       cout<<"x is even";                        //C++
       echo "x is even";                           //PHP
       System.out.println("x is even");     //JAVA
       console.log("x is even");                //JavaScript
}

//python
if(x%2==0):
      print("x is even")

 

2. if...else...statement

        if-else statements takes a condition. if-else statements has two blocks. One  block is for if statement and another block is for else statement. If the condition is true then if block would be executed else else block would be executed.

Example:

If a teacher asks a question to a student.

If the student answers true then

       he would be awarded

else

        he would be punished. 

if ( answer is true ) :
        award
else:
        punishment
/*
C, C++, PHP, JAVA and JavaScript
if(condition)
{
      statements;
}
else
{
       statements;
}

python
if(condition):
      statements
*/

if( x % 2 == 0)
{
       printf("x is even")                          //C and C++
       cout<<"x is even";                        //C++
       echo "x is even";                           //PHP
       System.out.println("x is even");     //JAVA
       console.log("x is even");                //JavaScript
}
else{
       printf("x is odd")                          //C and C++
       cout<<"x is odd";                        //C++
       echo "x is odd";                           //PHP
       System.out.println("x is odd");     //JAVA
       console.log("x is odd");                //JavaScript
}

//python
if(x%2==0):
      print("x is even")
else:
      print("x is odd")

 

3. nested if...else...statement

        nested if-else statements takes multiple conditions. It is nothing but using the if-else again inside the block.

Example:

A teacher asks a question to a student.

If the student answers true then

       if the answer is partially true then

he would be rewarded by 50 Rs.

else

he will get 100 Rs.

else he would be awarded else he would be punished. 

if ( answer is true ) :
    if ( partially true):
        award of 50 Rs.
    else:
        award of 100 Rs.
else:
    punishment
/*
C, C++, PHP, JAVA and JavaScript
if(condition)
{
      if(condition)
      {
             statements;
      } 
      else
      {
              statements;
       }
}
else
{
      statements;
}

python
if(condition):
     if(condition):
         statements
     else:
         statements
else:
      statements
*/

if( x % 2 == 0)
{
    if(x % 3 == 0){
       printf("x is multiple of 6")                          //C and C++
       cout<<"x is multiple of 6";                        //C++
       echo "x is  multiple of 6";                          //PHP
       System.out.println("x is multiple of 6");     //JAVA
       console.log("x is multiple of 6");                //JavaScript
     }
     else{
       printf("x is even but not divisible by 3")                          //C and C++
       cout<<"x is even but not divisible by 3";                        //C++
       echo "x is even but not divisible by 3";                           //PHP
       System.out.println("x is even but not divisible by 3");     //JAVA
       console.log("x is even but not divisible by 3");                //JavaScript
     }
}
else{
       printf("x is odd")                          //C and C++
       cout<<"x is odd";                        //C++
       System.out.println("x is odd");     //JAVA
       console.log("x is odd");                //JavaScript
}

//python
if(x%2==0):
    if(x%3==0):
         print("x is multiple of 6")
    else:
         print("x is even but not divisible by 3")
else:
     print("x is odd")

 

4. if...else...ladder statement

        If we use if-else statements again in else statement. So it would become similar to a ladder.

Example: If a teacher asks three questions to a student.

If the student answers all questions true then

        he would be awarded 50 Rs.

else if the student answers two question true then

        he would be awarded 20 Rs.

else if the student answers one question true then

        he would be awarded 10 Rs.

else if the student answers all questions false then

        he would not be awarded.

if ( all answers are true ) :
        award of 50 Rs.
else if ( 2 answers are true ) :
        award of 20 Rs.
else if ( 1 answers are true ) :
        award of 10 Rs.
else if ( all answers are false ) :
        no award
/*
C, C++, PHP, JAVA and JavaScript
if(condition)
{
      statements;
}
else if{
       statements;
}
else{
       statements;
}

python
if(condition):
      statements
elif(condition):
      statements
else:
       statements
*/

eli

 

5. switch statement

        It is a better way for deciding among the multiple blocks than if-else ladder. If else ladder checks one by one but it directly goes to the correct block. Break statement is required at the end of each case otherwise it will execute all the following cases. Default case can be defined if there is no correct case.

Example: If a teacher asks three questions to a student.

The cases can be

     case 'If the student answers all questions true then':

              he would be awarded 50 Rs.

     case 'If the student answers 2 questions true then':

              he would be awarded 20 Rs.

     case 'If the student answers 1 question true then':

              he would be awarded 10 Rs.

     default:

              he would not be awarded.

switch(true answers):
   case (3) :
        award of 50 Rs.
        break
   case (2) :
        award of 20 Rs.
        break
   case (1) :
        award of 10 Rs.
        break
   default :
        no award
/*
C, C++, JAVA and JavaScript
switch(condition)
{
      case 'option' : 

python doesn't have switch statement
*/

switch(n)
{
    case 0:
       printf("Zero")                          //C and C++
       cout<<"Zero";                        //C++
       System.out.println("zero");     //JAVA
       console.log("Zero");                //JavaScript
       break;
    case 1:
       printf("one")                          //C and C++
       cout<<"one";                        //C++
       System.out.println("one");     //JAVA
       console.log("one");                //JavaScript
       break;
    default:
       printf("Neither zero nor one")                          //C and C++
       cout<<"Neither zero nor one";                        //C++
       System.out.println("Neither zero nor one");     //JAVA
       console.log("Neither zero nor one");                //JavaScript
}

 


Rate this post

Comments