For example: In school we have different activities from which we opt for one depending upon choice of our activity. Similarly, at college level we have different colleges and universities to choose from. Further, we have to opt for the career most suited to us in life, depending on our priorities.
Be it any situation, wherever there are options, we need to make choices. For this we use conditional checking i.e. when we need to ascertain a final output we make a conscious decision of choosing a specific option. This decision is based on some checks that we make.
What are these checks? Different conditions, different checks.
In programming there are different ways of checking conditions. Here we are learning about If-else ladder. Using if-else ladder we check three types of flow of conditions- single condition, double condition and multiple conditions.
When we have a single condition to be verified, we use if condition.
E.g.:- If book name=”Hunger Games”, print the genre as “Fiction”
BEGIN
READ: CHOICE;
IF CHOICE==”Hunger Games”
PRINT:”Fiction”;
END
When we have to choose from two options, we use if-else.
E.g.:-If the library deals in only two genres of books, i.e. fiction and comedy, we would have an algorithm similar to:
BEGIN
READ: CHOICE;
IF CHOICE==”Hunger Games”
PRINT: “Fiction”;
ELSE
PRINT: “Comedy”;
END
When we have to choose from two options, we use if-elseif-else.
Whereas if we have a Library which deals in three genres of books, we would have an algorithm similar to:
BEGIN
READ: CHOICE;
IF CHOICE==”Hunger Games”
PRINT: “Fiction”;
ELSE IF CHOICE==”Pride & Prejudice”
PRINT: “Classic”;
ELSE
PRINT: “Comedy”;
END
Similarly, we can keep adding else if condition and its value to check, more conditions, genre in this case.