Friday, 16 March 2012

Conditional Statement, Conditional Statement, if statement, If-else statement, If-else-if statement

Conditional Statement:-


          Some time while programming we want to execute some statement when some specified condition is true,for this type of condition we use conditional statement.
         conditional statements are following:-

  • The If Statement
  • The Switch statement



The if statement-


          If is not a command. It's another keyword in the C programming language, one that you can use in your program to make decisions- although it really makes comparisons, not decisions. It's the program that decides what to do based on the results of the comparison.
         The if() statement is a control statement that tests a particular condition. When ever the condition comes out to be true, then the action or the set of actions are carried out. Otherwise, the given set of action(s) are ignored.
        
        If statement is of following type-

  • Simple If statement
  • If-else statement
  • If-else-if statement

 If the contents of variable X are greater than variable Y, scream like

they’re twisting your nose.

If the contents of the variable calories are very high, it must taste very 
good. 
If it ain’t broke, don’t fix it. 
If Doug doesn’t ask me out to the prom, I’ll have to go with Charley.

      Syntax:-    (i)-    if (expressions)
                                          statement(s);
                                          
                            (ii)-   if (expression)
                                           {    
                                               statement;
                                               statement;
                                               -----------
                                               -----------
                                             }
                                       else
                                             {
                                                  statement;
                                                  statement;
                                                   ----------
                                                  ----------
                                              }

                             (iii)-   if (expression)
                                              {
                                                  statement;
                                                 statement;
                                                  ----------
                                                  ----------
                                                 }
                                            else
                                                   if (expression)
                                                         {
                                                              statement;
                                                              statement;
                                                             ------------
                                                             ------------
                                                         }
                                                     else
                                                             {         
                                                                   statement;
                                                                   statement;
                                                                  -----------
                                                                 -------------
                                                               }
Example of Program:-

                          
                            #include <stdio.h> 
                            #include <stdlib.h> 
                                      
                                int main() 
                                { 
                                     
                                      char num[2]; 
                                      int number; 

                                      printf(“I am your computer genie!\n”); 
                                      
                                      printf(“Enter a number from 0 to 9:”); 
                                      gets(num); 
                                      number=atoi(num); 

                                      if(number<5) 
                                      { 
      
                                                 printf(“That number is less than 5!\n”); 

                                       } 

                                       printf(“The genie knows all, sees all!\n”); 
                                       return(0); 
                                }


1 comment: