Conditional Statement | If statement | If -else statement | If-else if ladder | C Programming | Programming tutorials | programming concepts


                                

                        CONDITIONAL STATEMENTS IN C



CONDITIONAL STATEMENT are used to change the normal flow of the

program ,they represent certain condition like true and false depending upon whom the other part of the program gets executed.


                          

                          if Statement


It is also known as decision making statement. If condition is always used

with a condition or expression. If statements always evaluate the condition

inside the parenthesis and generate ‘true’ or ‘false’ as the result. If the

result after the evaluation of the condition is ‘True’, then the statements

inside the opening and closing braces will be evaluated. Otherwise, if the

result is ‘False’, the condition inside the opening and curly braces will not

be evaluated.


Points to take care of:-


  • Every statement or expression inside the opening and curly braces should end with a semicolon ( ; ).

  • If the if-block contains only one expression, there is no need of putting the opening and closing curly braces.

  • The opening and closing curly braces are only needed when there are multiple expressions or statements near the if-block.


Syntax for if:-


if (condition/expression/statement)

{

…………………………….

…………………………….

…………………………….

…………………………….

}



Example:


  
 
  
Output:
  
                                                                            



                          If-else Statement


If-else statement is a two way decision process. In these statements, first of

all the condition inside the if statement will be evaluated, if the result comes

out to be ‘True’ then the expressions inside the If-block will be executed.

Otherwise, if the condition inside the if statement comes out to be ‘False’,

then the expressions inside the else block will be executed.


Note:- In one if-else statements, only one block will be executed at a time

i.e. either If- block or else-block will be executed at a time.


Syntax for if-else:-


if (condition/expression/statement)

{

…………………………….

…………………………….

}


else

{

…………………………….

…………………………….

}


Example:



  Output:
  





                            If-else if- else ladder


It is a multiple way decision process.If statement is executed from top to bottom but as the statement gets executed and its gets true the next statement associated with it also gets executed. And if none of the if statement gets true then final else statement gets executed.

Syntax for if-else if-else :-


if (condition/expression/statement)

{

…………………………….

…………………………….

}


else if (condition/expression/statement)

{

…………………………….

…………………………….

}


else

{

…………………………….

…………………………….

}



Example:

       
 

output:




So friends, these are the basics of conditional statements in C including the if statement,

if-else statement and if-else-if ladder. Go through the programs and their outputs and

try to understand the working of these statements thoroughly. If you have any doubts

related to conditional statements in C, feel free to ask it in the comments section.












































































































































































































































































































































Comments