IF - ELSE statements in C

  In this article we will talk about statements in C.



SIMPLE IF :-   The ' statement block ' may be a single statements or a group of statements . If the test expression is true , the statement - block will be executed ; otherwise the statements - block will be skipped .                                                                                                                                                                            The general form of using IF statement is :-

                                             if(test expression)                                                    {                                                                     Statement - block                                                    } 

Example :-          

                              #include <stdio.h>                                                       int main( )                                                           {                                                                      int num;                                                              printf("Enter a number ");                                            scanf("%d",&num);                                                     if ( num <= 10 ){                                                      printf(" num is valid ");                                           }                                                                       return 0;                                                          }      

The IF - ELSE Statements :-   The expression written in if block will execute when the expression is true , if the expression is false , then the set of statements in else block will execute.

Syntax :-                       if ( condition )                                                       {                                                                          statement                                                           }                                                                        else {                                                                    statements                                                             }    

Example :-    

                #include <stdio.h>                                                     int main( )                                                          {                                                                     int num;                                                             printf("Enter a number ");                                           scanf("%d",&num);                                                     if ( num <= 10 ){                                                        printf(" num is valid ");                                         } else { printf("num is invalid ");}                                  return 0;                                                           }      

Nested IF - ELSE  Statements :-   We can write an entire If- else statements within either the body of the if statement or the body of an Else - Statement. 

Syntax :-           if ( condition )                                                       {                                                                        statement                                                         }                                                                      else {                                                               if ( condition ){                                                      statements                                                           }                                                                     else { statement }                                                   } 

Example :-

                   #include <stdio.h>                                                     int main( )                                                          {                                                                     int num;                                                             printf("Enter a number ");                                           scanf("%d",&num);                                                     if ( num <= 10 ){                                                     printf(" num is valid ");                                             } else if ( num >= 10 ){                                                     printf(" num is in-valid ");                                      else { printf("num is equal ");}                                    }                                                                     return 0;                                                            }     

Conditions and it's meaning :-

     a==b                             a is equal to b

     a!=b                             a is not equal to b

     a > b                            a is greater than b

     a < b                            a is less than b  

     a >= b                           a is greater than equal to b

     a <= b                           a is less than equal to b 



 in next arictle we will dicuss about SWITCH-CASE statements in C.






                    

Code with AD

Hii connection! This is Adarsh Kumar_ From Patna , Bihar(India)_ I am pursing graduation from AN college, Patna.

Post a Comment (0)
Previous Post Next Post