Skip to main content

Conditional statements in C Language

                                                       C Language Beginner level

Conditional Statements in C :

The concept of Conditional statement also can be reffered as Dacision Making . We can understand the Decision Making concept and working using the below situation  -

If The value of a variable x is greater than 5 , Program should print " Hello " and if the value of x is lower than 5 , program should print "World " . We use conditional statement as -

if(x >5)
{
printf(" Hello ") ;
}
else
{
printf(" World ") ;


There are various kind of Conditional statements , we can use in C -

  • If Statement
  • If-Else Statement 
  • Nested If Statement 
  • Switch 
If Statement : 
Simply If a condition inside if statement is True , The If block executes otherwise ignore the If Block and compiler moves to the next lines of code . 

if( x < 10 )
{
printf(" Hello World ") ;
}


Note : 

  • If compiler gets the value of x greater than 10 in the programe code , It executes the If Block and prints the " Hello World " , Otherwise compiler ignores the entire If Block .
If-Else Statement :
It is exactly same as the If statement but It does not ignore the entire If block for wrong condition . If the condition inside if block is wrong then compiler works on the Else block . 


if(x >5)
{
printf(" Hello ") ;
}
else
{
printf(" World ") ;


Nested If Statement : 
We use Nested-If , when we need to work with the more conditions inside a if block .

if(x >5)
{
if(x >10) { printf(" Hello ") ; }
else { printf(" Hello  World ") ;}
}
else
{
printf(" World ") ;

Note : 

  • Here we see an another If Else Statement inside an If Block . If x>5 , Compiler executes first If block and Second if Block Otherwise directly it prints " World " . 
  • If x > 5 , compiler again checks for the x value . If x is greater than 10 , It prints " Hello " otherwise " Hello World " . 

Switch Statement : 
The syntex  of Switch Stement is different from the If-Else Statements and can understand with the below example  -

#include <stdio.h>

int main () {

   /* local variable definition */
   char rank= 'A';

   switch(rank) {
      case 'A' :
         printf("Gold Medal!" );
         break;
      case 'B' :
         printf("Silver Medal!" );
         break;
      case 'C' :
         printf(" Bronze Medal! " );
         break;
     
      default :
         printf(" Goodluck for Next Time ! " );
   }
   
   printf("Your rank is  %c\n", rank);
   return 0;
}

Note :

  • If the variable rank contains ' A ' in our code and when Switch statement executes , It gets the rank variable as a condition . 
  • Switch statement contains multiple cases in its block like here , case 'A' , case 'B' , case 'C' .
  • Switch statement check for its condition for all the cases one by one .
  • So condition saisfies the case ' A ' because , we initiated rank variable with A . 
  • case 'A' executes and prints Gold Medal and then break statement used to come out from the entire switch block . 
  • So the output on the screen is - Gold Medal ! Your rank is A : 

Let's see Keywords in C in the next Page .


                                                           << Back            >>Next

Comments