You are on page 1of 3

Practical session - 3

if-else statement is a conditional control statement used in C programming to execute different


blocks of code based on certain conditions.

Syntax:

Explanation:

The if statement evaluates the given condition inside the parentheses ( ). If the condition is true, the
block of code within the curly braces { } following the if statement is executed.

If the condition provided with the if statement is false, the block of code within the else block (denoted
by the else keyword followed by another block in curly braces) will be executed.

Example:
Explanation of the Example:

In this example, the program initializes a variable num with the value 10.

The if statement checks if num is greater than 5. In this case, num is indeed greater than 5, so the code
inside the { } block following the if statement is executed.

Therefore, the output of this program will be: The number is greater than 5.

Nested if-else:

if-else statements can also be nested, meaning an if or else block can contain another if-else construct
within it.

Example of Nested if-else:


Explanation of Nested if-else Example:

In this example, if num is greater than 10, it enters the first if block and prints Number is greater than 10.

Inside this block, there's another if-else construct: if num is less than or equal to 15, it prints Number is
less than or equal to 15. Otherwise, it prints Number is greater than 15.

If num is not greater than 10, the program jumps to the else block and prints Number is less than or
equal to 10.

The if-else statement is fundamental in controlling the flow of a C program based on conditions, allowing
different paths of execution depending on the evaluated conditions

You might also like