C : CONDITIONAL STATEMENTS AND LOOPS

CONDITIONAL STATEMENTS AND LOOPS

CONDITIONAL STATEMENTS AND LOOPS




DECISION MAKING WITHIN A PROGRAM :

  1. Decision making structure of a program is done when the programmer specifies more than one condition.

  2. It is tested with the statements provided for the decision which can be true or false.

  3. C programming language assumes any non-zero and non-null values as true, and if it is either zero or null, then it is assumed as false value.

IF AND IF-ELSE STATEMENTS :

  1. IF STATEMENT

    1. IF statement is used for checking condition and executing the statements accordingly.

    2. If the condition is TRUE then the statement which we have written in block will be executed.

    3. The statement will be ignored if the condition is FALSE.

    4. SYNTAX :
          
          if(condition)
          {
             
            //STATEMENTS;
            
          }
          
          


  2. IF-ELSE STATEMENT

    1. IF-ELSE statement is similar to IF statement only with one difference.

    2. The differnce is that it will also execute the statement if the condition false .(here : STATEMENT 2)

    3. ELSE block is used with IF to executed the statements according to the condition.

    4. SYNTAX :
          
          if(condition)
          {
            
            //STATEMENT 1;
            
          }
          else
          {
             
            //STATEMENT 2;
            
          } 
          
          


  3. IF-ELSE-IF STATEMENT

    1. IF-ELSE-IF statements are used to test the multiple condition and execute accordingly.

    2. It have more than one condition to be checked.

    3. ELSE IF statement is used to take multiple conditions.

    4. SYNTAX :
          
          if(condition 1)
          {
             
            //STATEMENT 1 ;
            
          }
         else if(condition 2)
          {
             
            //STATEMENT 2;
            
          }
          else
          {
             
            //STATEMENT 3;
            
          }
          
          

LOOPS IN C :

  1. FOR LOOP

    1. FOR loop is used to execute a block of code repeatedly until a specified condition is met. 

    2. INITILIZATION is done only one time and then the condition is checked.
      If the CONDITION is FALSE then the loop will be terminated.

    3. If the CONDITION is TRUE, statements inside the body of for loop are executed, and the increment/decrement is done until CONDITION get FALSE.

    4. SYNTAX :
          
          for(initilization;condition;increment/decrement)
          {
             
            //STATEMENTS;
            
          }
          
          



  2. WHILE LOOP

    1. The WHILE loop test the CONDITION inside the parenthesis ().

    2. If the CONDITION is TRUE, statements inside the body of for loop are executed, and the increment/decrement is done until CONDITION get FALSE.

    3. INITILIZATION is done before executing WHILE loop.

    4. SYNTAX :
          
           initilization;
           while(condition)
          {
             
            //STATEMENTS;
            increment/decrement;
            
          }
          
          



  3. DO-WHILE LOOP

    1. The DO-WHILE loop is similar to the WHILE loop with only difference that the body of DO-WHILE loop is executed at least once.

    2. After executing at least one time the condition is checked.

    3. SYNTAX :
          
          initilization;
          do
          {
             
            //STATEMENTS;
            increment/decrement;
            
          }while(condition);
          
          


SWITCH STATEMENTS :

  1. A SWITCH statement allows a variable to be tested for various condition against a list of expression.

  2. Each expression is known as a CASE.

  3. When the variable is equal to a case, the statements following that case will execute until a break statement is reached.

  4. SYNTAX :
        
        switch(expression)
        {
          case expression1:
          statement;
          break;
          
          case expression2:
          statement;
          break;
          
          case expression3:
          statement;
          break;
          
          default:
          statement;
        }
        
        


CONTROL/JUMP STATEMENTS :

  1. BREAK STATEMENT :

    1. It can be used to terminate a CASE in the SWITCH statement

    2. If used in NESTED LOOPS (one loop inside another), the BREAK statement will stop the execution of the innermost loop and start executing the next line of code after the block.

    3. SYNTAX : break;



  2. CONTINUE STATEMENT :

    1. The CONTINUE statement works somewhat like the BREAK statement.
      Instead of forcing termination, it forces the next statements of the loop to take place, skipping any code in between.

    2. For the WHILE and DO-WHILE loops, CONTINUE statement causes the program control to pass to the conditional tests.

    3. SYNTAX : continue;

Comments