ELSE IF Ladder
ELSE IF ladder
|
if statement evaluates the condition inside the paranthesis and if it is true, then it executes one line followed by if statement or the sequence of steps bound of { }.
if statement can be combined with else part which is explained in if else statement.
Nested if statement is explained with finding the biggest of 3 numbers example.
Else if ladder is explained with an example given below:
Source Code
#include <stdio.h>
void main()
{
int choice;
printf("[1] Add\n");
printf("[2] Edit\n");
printf("[3] Delete\n");
printf("[4] View\n");
printf("[5] Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
if(choice == 1)
printf("Add option selected");
else if(choice == 2)
printf("Edit option selected");
else if(choice == 3)
printf("Delete option selected");
else if(choice == 4)
printf("View option selected");
else if(choice == 5)
printf("Exit option selected");
else
printf("Invalid option selected");
Output
[1] Add
[2] Edit
[3] Delete
[4] View
[5] Exit
Enter your choice: 4
View option selected
### simple else if ladder ###
ReplyDelete#include
main()
{
int wish;
printf("[a] info\n");
printf("[b] details\n");
printf("[c] setting\n");
printf("[d] address\n");
printf("[e] complete\n");
printf("[x] exit\n");
printf("enter your wish: ");
scanf("%d" & wish);
if(wish==a)
printf("info option selected");
else if(wish==b)
printf("details option selected");
else if(wish==c)
printf("setting option selected");
else if(wish==d)
printf("address option selected");
else if(wish==e)
printf("complete option selected");
else if(wish==x)
printf("exit option selected");
else
printf("wrong option selected");
}