Unit 2. Decision control structure with Pavan Chavhan.

 




Very happy to see you again, welcome! back to the "technical_spc". In the my last two articles of "C" programming we have completed unit 1 i.e. Introduction to "c". I hope all of you have understood the unit 1 very nicely. 





Now in this article let's start with unit 2 i.e. Decision control structure.

Today the topics that we will cover are :-


Let's move forward...


Decision making in"c".


(a). Decision making structures require that the programmer specify one or more conditions to be evaluated or tested by the program.

(b). Along with a statement or statements to be executed if the condition is determined to be true. 

(c). Optionally other statements to be executed if the condition is determined to be false.

(d). A decision control instruction can be implemented :


      1. The if statement.

      2. The if.....else statement.

      3. The if.....else if..... statement.

      4. Nested if statements.

      5. Switch statement.




 Algorithm & Flowchart.


(a). Algorithm is a step-by-step procedure, which defines a set of instructions to be executed in a certain order to get the desired outputs.

(b). Flowchart is a diagrammatic representation of sequence of logical steps of a program.

(c). Flowcharts use simple geometric shapes to depict processes and arrows to show relationships and process/data flow.







if statement.



(a). An if statement consists of a Boolean expression followed by one or more statements.

(b). Syntax :- 


         if(this condition is true)

         { 

         execute this statement;

         }


(c). Flowchart :-







 (d). Example :-


  • Program :-



  • Output :- 



  • Explanation :-
The condition (x<y) specified in the “if” returns true for the value of x and y, so the statement inside the body of if is executed. Otherwise a blank screen is executed if the condition is false.



if.....else statement.



(a). An if statement can be followed by an optional else statement, which executes when the boolean expression is false.

(b). Syntax :-


if(condition)

  statement will execute if the condition is true;

 }

else

 { 

  statement will execute if the condition is false;

 }


(c). Flowchart :-



(d). Example :-

  • Program :-


 


  • Output :-




The if.....else if.....else statement.




(a). An if statement can be followed by an optional else if.....else statement, which is very useful to test various conditions using single if.....else if statement.

(b). Syntax :-


if(condition 1)

  statement will execute if the condition 1 is true;

}

else if(condition 2)

   statement will execute if the condition 2 is true;

}

else if(condition 3)

   statement will execute if the condition 3 is true;

}

else

   statement will execute if the condition is false;

}


(c). Flowchart :-



(d). Example :-


  • Program :-



  • Output :-




  • Explanation :-
As you can see that only the statements inside the body of “if” are executed. This is because in this statement as soon as a condition is satisfied, the statements inside that block are executed and rest of the blocks are ignored.

Important Points :-

1. else and else..if are optional statements, a program having only “if” statement would run fine.

2. else and else..if cannot be used without the “if”.

3. There can be any number of else..if statement in a if else..if block.

4. If none of the conditions are met then the statements in else block gets executed.

5. Just like relational operators, we can also use logical operators such as AND (&&), OR(||) and NOT(!).



Nested if statements.


(a). It is always legal in "c" programming to nest if-else statements.

(b). Which means you can use one if or else if statement inside another if or else if statements.

(c). Syntax :-


if(condition 1)

   execute this statement if condition 1 is true;

    if(condition 2)

    { 

     execute this statement if condition 2 is true;

    }

    else

     {

     execute if condition 2 is false;

     }

     else

    {

     execute if condition 1 is false;

     }

}


(d). Example :-


  • Program :-



  • Output :-







  • Explanation :-

Within this example, If the age of a person is less than 18, he is not eligible to work. If the age is greater than or equal to 18, then the first condition fails, it will check the else statement. In the Else statement, there is another if condition called Nested If in C to check further.

  • In this example, the Nested IF Statement checks the person’s age greater than or equal to 18 and less than or equal to 60. When the condition is TRUE, then he can apply for the job.
  • If the condition is FALSE, then the statement – he is too old to work as per the government.


If you have any questions you may ask in comment below. If you enjoyed this article, share it with your friends and colleagues!




Post a Comment

2Comments

If you have any doubts, Please let me know

Post a Comment