else- if ladder statement

 

Control Statements

 else- if ladder statement:

When to choose one alternative out of many if-else statement is used.

Syntax:

If(test Condition-1)

            {

                        Block of statements-1;

             }

else if(test Condition-2)

            {

                        Block of statements-2;

             }

else if(test Condition-3)

            {

                        Block of statements-3;

             }

 

else if(test Condition-n)

            {

                        Block of statements-n;

             }

else

            {

                        default statements;

             }

 

Statement X;

§  It is used to choose one alternative out of many.

§  The conditions are evaluated from top of if condition to downwards up to else. As soon as a true condition is found, the statement associated with it is executed and then statement x is executed.

Flowchart:



Program:

            Write a program to check enter number is +ve, -ve or Zero.

 

 

/* Prog no-11 Program to check enter number is +ve, -ve or Zero */

 

#include <stdio.h>

#include <conio.h>

 

void main()

{

                int num;

 

                clrscr();

 

                printf("\nEnter one integer number  ");

                scanf("%d",&num);

 

                if(num>0)

                {

                        printf("\n %d is +ve number ",num);

                }

                else if(num<0)

                {

                        printf("\n %d is -ve number ",num);

                }

                else

                {

                        printf("\n %d is Zero number",num);

                }

 

                getch();

}

 

Output



Program:

            Write a program to print grade of student based on percentage.

 

/* Prog no-12 Program to print grade of student based on percentage */

 

#include <stdio.h>

#include <conio.h>

 

void main()

{

                float per;

 

                clrscr();

 

                printf("\nEnter one percentage of student  ");

                scanf("%f",&per);

 

                if(per>=90)

                {

                        printf("\n\n Student percentage=  %f \nThe Student has A+ Grade ",per);

                }

                else if(per>=80 && per<=89)

                {

                        printf("\n\n Student percentage=  %f \nThe Student has A Grade ",per);

                }

                else if(per>=70 && per<=79)

                {

                        printf("\n\n Student percentage=  %f \nThe Student has B Grade ",per);

                }

                else if(per>=60 && per<=69)

                {

                        printf("\n\n Student percentage=  %f \nThe Student has C Grade ",per);

                }

                else

                {

                        printf("\nStudent percentage=  %f \nThe Student has D Grade ",per);

                }

 

                getch();

}

 

Output:




Post a Comment

0 Comments