Thursday, 2 March 2017

Elements of programming-6




If-else if ladder.

Checking more than one else if condition is known as if-else if ladder. First a condition is checked if it is true then a block of statements are executed. If it is false an another condition is checked with the help else if construct. If it also false then another condition is checked. Checking more than one else if construct is known as else if ladder.


Example:

#include <stdio.h>

int main()
{
    int dayno;
    printf("enter day no:");
    scanf("%d",&dayno);
    if(dayno==1)
    {
        printf("sunday");
    }
   else  if(dayno==2)
    {
        printf("monday");
    }
   else if(dayno==3)
    {
        printf("tuesday");
    }
   else if(dayno==4)
    {
        printf("wednesday");
    }
    else if(dayno==5)
    {
        printf("thursday");
    }
   else if(dayno==6)
    {
        printf("friday");
    }
   else if(dayno==7)
    {
        printf("saturday");
    }
    else
    {
        printf("invalid day number");
    }


    return 0;
}

                                       In the above program day number is get as input . the block statements which will be executed is depending upon the variable dayno we entered. If all the condition is false then  else part will be executed.

                                       Thanking you,
                                                                    Muthu karthikeyan,Madurai.
contact:919629329142
email:muthu.vaelai@gmail.com

Wednesday, 1 March 2017

Elements of programming -5.




Nested if statement.

First a condition is checked if it is true then another condition checked. If it is true another condition is checked. If it also true then a block of statement is executed. If the first condition is true, then else part will be executed. we can also check another condition in else part.

Example.

We are going to find big number among three numbers. The program for it.
#include <stdio.h> 
int main()
{
     int a,b,c;
     printf("enter a:");
     scanf("%d",&a);
     printf("enter b");
     scanf("%d",&b);
     printf("enter c");
     scanf("%d",&c);
     if(a>b)
     {
         if(a>c)
         {
             printf("a is big");

         }
         else
         {
             printf("c is big");

         }
     }
     else
     {
         if(b>c)
         {
             printf("b is big");
         }
         else
         {
             printf("c is big");
         }
     }
    return 0;
}
In the above program has been written in c language. It uses the method of nested if statement.  First a is greater than b is executed if it is true then it check for the second condition whether the second condition is true or not false. If it also true a is declared as big number. If the second condition is false then c is declared as big number .if the first condition is false then it comes to else part.  It also checks another condition. If it is true then b is declared as big number. If it is false then c is declared as big number.
                    Will continue
                                                                                Muthu karthikeyan,Madurai.
                                                     contact:919629329142
                                                          Email:muthu.vaelai@gmail.com.