-->

Learn to code the on-demand coding Languages in an easy way. We help you understand programming concepts and coding Techniques Better.

Wednesday, January 27, 2021

Control Statements in C | if, if-else, and switch statements in C

 Control Statements in C :

    
In the most general sense, a statement is a part of our program that can be executed. That is, a statement specifies an action. C  categorizes control statements into these groups.

1. Selection Statements

Selection/Conditional Statement : 
        The selection statements are used to perform certain operations based on the condition. The operations specified in the block are executed only if the condition is true. C supports selection statements if-else and switch. 

The following are the different variants in control statements.
    1. if 
    2. if else 
    3. if-else-if ladder
    4. switch

    if: The if block is executed whenever the condition evaluated to true.
   
    syntax:  

               if (condition)
               {
                   //code to be executed.
               }
        
    Flow Diagram:  A  Flow chart is a step-by-step procedure in order to solve a particular task.

                        

       Example: 
        
    

      Output:

   

if-else : The if block is executed when the condition is true otherwise else block is executed.

syntax:   

             if (condition)

             {

                       //code to be executed.
             }
             else
             {
                       //code to be executed
             }

 Flow Diagram :
        
                           
    Example:


     Output:



if-else-if Lader:  It is used in the scenario where there are multiple cases to be performed for different conditions. In the if-else-if ladder statement, if a condition is true then the statements defined in the if block will be executed, otherwise if some other condition is true then the statements defined in the else-if block will be executed, at the last if none of the condition is true then the statements defined in the else block will be executed. There are multiple else-if blocks possible.

syntax: 

                    if( condition)

                            // statements

                    else if( condition)

                            //statements

                    else if(condition)

                            //statements

                    else

                            //statements

   Example:

   Output:



switch  :  It is similar to the switch case statement where the default is executed instead of else block if none of the cases is matched.

syntax : 

                switch (value)

                 {

                    case 0 : // statement 1

                                             break;

                    case 1 : // statement 2

                                     break; 

                         case2 : // statement 3

                                      break;

                    default : //stattement 4

                                       break;

                    }

    Flow Diagram: 

                       

    Example:

   Output:

   Explanation: The value 10 matches at the 2nd case. The value doesn't matches with the 3rd case and default case because of the break statement in the second case. If there is no break statement in 2nd case the following cases will execute until the break statement found.


prev<---operators                                                           next topic --->Iterators in c

                

No comments:

Post a Comment