Tuesday, 30 January 2018

Decision Statements

Decision Statements

           Using decision statements, we can make our programs execute certain statements depending on some conditions. Next we illustrate the decision statements supported by C.

' if ' statement
 
          It consist of a boolean expression and we execute the set of statements in the ' if ' block depending on the value (True/False) of the boolean expression.

Following program demonstrates the use of ' if ' statement : 


----------------------------------------------------------------------------------------------------------------------------



----------------------------------------------------------------------------------------------------------------------------

Output : 

Enter Marks :  20

Final Marks :   25

----------------------------------------------------------------------------------------------------------------------------


' if ... else ' statement
 
          This statement is used when there are multiple conditions depending on which different block of statements are to be executed. If the condition is true, then statements in the if block is executed, otherwise statements in the else block is executed.

See the following program to understand the use of ' if ... else ' construct :

 ----------------------------------------------------------------------------------------------------------------------------


 ----------------------------------------------------------------------------------------------------------------------------

Output :

Greater no. is : 9

----------------------------------------------------------------------------------------------------------------------------
 
         When there are more than one if condition are to be evaluated, then we use a chain of
  if ... else statements. Following program illustrates the use case :

----------------------------------------------------------------------------------------------------------------------------

 

----------------------------------------------------------------------------------------------------------------------------

Output :
Grade Assigned is C

----------------------------------------------------------------------------------------------------------------------------


' switch ' statement
 
           switch statement is used when a condition or a variable has to be tested for equality against multiple values.

Following program illustrates the use of switch construct :
 
----------------------------------------------------------------------------------------------------------------------------

 

----------------------------------------------------------------------------------------------------------------------------

Output :
Enter your GRADE :  A

Outstanding


----------------------------------------------------------------------------------------------------------------------------

Nesting of Decision statements
 
           We can use a decision statement inside the block of another decision statement. This is known as nesting.

See the following program to understand nesting : 

----------------------------------------------------------------------------------------------------------------------------

 

----------------------------------------------------------------------------------------------------------------------------

Output :
Enter Employee Type (R/r for regular) : R

Enter no. of years served : 12

Enter salary : 10000

Bonus :  15000.000000
----------------------------------------------------------------------------------------------------------------------------

No comments:

Post a Comment