if-else Selection Statement in C

For Complete YouTube Video: Click Here

In this class, we will try to understand the if-else Selection Statement in C.

If-else statements are selection statements.

Selection statements mean we can choose from two options based upon the evaluation of the expression.

Do we use selection statements in the day to day life?

Yes, knowingly or unknowingly, consciously or unconsciously, we use selection statements in our day to day life.

For example, if we want to buy a movie ticket for an A-Rated movie, he will ask for ID proof, or just by seeing the face, he will confirm and issue the ticket.

Here the ticket is issued under the condition of age greater than 18 or not.

Similarly, C Programming language provides an essential feature to select an option from two choices.

if-else Selection Statement

The syntax for the if-else statement is as shown in the image below.

if-else Selection statement in c
if-else Selection Statement Syntax

In the above image, if and else are keywords.

After if we have to provide open and closed parentheses.

Within those parentheses, we can provide logical expressions.

Below the if we have open and closed curly braces.

If the output generated by the expression is one or any other integer, then the statements in the curly braces below the if will be executed.

Below the curly braces is the ‘else’ statement and its body.

If the output generated is zero, then the compiler will execute statements below the else.

Below the curly braces are the ‘else’ statement and its body.

If the output generated is zero, then the compiler will execute statements below the else.

Example if-else Statements

The if-else statements below are how the movie tickets are issued by checking the age of the customer.

if-else Selection statement in c 2
if-else Selection Statement Example

If the condition in the parentheses is true, then eligible in the if will be printed.

If the condition is false, Not Eligible in the ‘else’ part will be printed.