C tutorial - Conditional Statements in C programming language

Programming languages or concepts
0

Conditional Statements in C programming language

In this tutorial, we learn about Conditional Statements. Conditional Statements in C programming are used to make decisions based on the conditions.If you put some condition for a block of statements, the execution flow may change based on the result evaluated by the condition. Conditional statements execute sequentially when there is no condition around the statements

Conditional Statements in C programming language:-

   Conditional Statements helps you to make decisions based on certain condition.
   The conditional Statements in C are the decision making statement they are the branching statement.

   These are conditional Statements in  C Language :-

1) if statement.
2) if....else statement.
3) if_else ladder.
4) Nested if...else statement.
5) switch statement.

1) if...else statement:-

    Syntax:-
      if(Condition)
      {
          // Statements;
           ---
           ----
       }

Working of if...else statement:-
    If the condition is evaluated to false, statement inside the body of if are executed.
     If the condition is evaluated to true, Statement inside the body of if are not executed.

Ex.
#include<studio.h>
int main()
{
   int X=20;
   int Y=22;
   if(X<Y)
   {
       printf("X is less than Y.");
    }
      return 0;
 }


OUTPUT:
           X is less than Y.

2) if... else statement:-

   In if statement may have an optional else block.

Syntax:-
if(Condition)
{
    Statement;
     ------
}
else {
             Statement;
          }
Working of if...else statement:
    If the condition is evaluated to true.
    Statements inside the body of if are executed.
   Statements inside the body of else are skipped from execution.
  If the condition is evaluated to false else block is executed.
Statements inside the body of if are skipped from execution.

Ex.  Consider odd & even

#include<stdio.h>
int main( )
{
  int num;
  printf(" Enter an integer :");
  scanf("%d", &num);
  if(num%2 = =0)
   {
      printf("%d is an even number ",num);
    }
     else{
               printf("%d is an odd number",num);
     }
     return 0;
}

OUTPUT :

    Enter an integer : 8
    8 is an even number

    When the user enter 8 , the test expression num%2° = =0 is evaluated to true. Hence the statement inside the body of if loop is executed and print output 8 is an even number.

3) if...else ladder :-

   The if...else statement is useful to check multiple condition within the program.
  If none of the condition evaluates to be true then the last else block is executed.

Syntax :-
if (Condition)
{
   // Statement 1;
}
else if(Condition 2)
{
     //Statement 2;
}
else if (Condition 3)
  {
      //Statement 3;
   }
else
  {
     //Statement 4;
   }

Ex.

#include<studio.h>
int main()
{
  int i=30;
if( i>=0 && i<=5 )
{
  printf("i is between 0 and 10");
}
else if (i>=11 && i<=15 )
{
   printf("i is between 11 and 25");
}
else if ( i>=16 && i<=29)
{
   printf ("i is between 16 and 29");
}
else
{
   printf( "i is greater than 29");
}

OUTPUT:

i is greater than 29

4) Nested if...else statement

         Nested if statement that is the target another if statement Nested if statements means an if statement inside another if statement means we can place the if statement inside the another if statement.

Syntax:-
if ( Condition )
{
  // block of statements;
      if( condition 2)
     {
        // Block of statements;
     }
}

Ex.
#include<stdio.h>
int main ( )
{
  if ( i= = 10 )
  {
     if ( i<15 )
printf ("i is smaller than 15");
     if ( i<12 )
      printf (" i is smaller than 12");
else
printf (" i is greater than 15");
}
return 0;
}


5). Switch statement

           The switch statement allow us to execute one code block among many alternatives.

Syntax:-
switch ( Condition )
{
  Case constant 1;
     //Statement;
       break;
   Case constant 2;
      //Statement;
       break;
      ----
      ----
default:
    // default statement;
}

How it works?
  - The expression is evaluated onces and compared with the values of each case label.
   If there is match, the different / corresponding statement after the matching label are executed.
  If there is not match, the default statement is executed.
   
Ex.
#include<studio.h>
int main ( )
{
   int a,b,c, choice;
   while (choice!=3)
   {
      printf ("\n 1. Addition");
      printf ("\n 2. Subtraction");
      printf(" \n Enter your choice");
      scanf("%d", & choice);
      switch (choice)
      {
         case 1:
                      printf("Enter 2 numbers");
                      scanf("%d%d",&a,&b);
                       c=a+b;
                      printf("%d",c);
                      break;
         case 3:
                     printf("Enter 2numbers");
                     scanf("%d%d",c);
                      c=a+b;
                     printf("%d",c);
                     break;
         default:
                       printf("wrong choice");
       }
   }
}



Post a Comment

0Comments

Post a Comment (0)
close