0% found this document useful (0 votes)
0 views2 pages

Explain if Else Statement in c Programming

The if-else statement in C programming allows for executing one block of code if a condition is true and another if it is false, providing two distinct execution paths. The syntax includes the 'if' keyword, a condition, and an 'else' keyword with corresponding code blocks. This control flow mechanism enhances decision-making in programs, ensuring that one of the two code blocks is executed based on the evaluated condition.

Uploaded by

kk1982johnson
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views2 pages

Explain if Else Statement in c Programming

The if-else statement in C programming allows for executing one block of code if a condition is true and another if it is false, providing two distinct execution paths. The syntax includes the 'if' keyword, a condition, and an 'else' keyword with corresponding code blocks. This control flow mechanism enhances decision-making in programs, ensuring that one of the two code blocks is executed based on the evaluated condition.

Uploaded by

kk1982johnson
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

The if-else statement in C programming is an extension of the simple if statement.

It provides a
way to execute one block of code if a specified condition is true and another block of code if the
condition is false. This allows for two distinct paths of execution based on the evaluation of a
single condition.
Syntax:
if (condition) {​
// Code to be executed if the condition is true​
} else {​
// Code to be executed if the condition is false​
}​

Explanation:
1.​ if keyword: Marks the beginning of the conditional statement.
2.​ condition: An expression enclosed in parentheses (). This condition is evaluated to
determine its truthiness (non-zero value) or falsity (zero value).
3.​ if block (True block): The code enclosed within the curly braces {} immediately following
the if condition is executed only if the condition evaluates to true.
4.​ else keyword: This keyword follows the closing curly brace of the if block. It signifies the
alternative block of code to be executed when the if condition is false.
5.​ else block (False block): The code enclosed within the curly braces {} immediately
following the else keyword is executed only if the condition in the if statement evaluates
to false.
How it Works (Flow of Execution):
1.​ The program encounters the if-else statement.
2.​ The condition inside the parentheses is evaluated.
3.​ If the condition is true (evaluates to a non-zero value):
○​ The code block within the if block is executed.
○​ The code block within the else block is skipped.
○​ The program then proceeds to the statement immediately following the closing curly
brace of the else block.
4.​ If the condition is false (evaluates to zero):
○​ The code block within the if block is skipped.
○​ The code block within the else block is executed.
○​ The program then proceeds to the statement immediately following the closing curly
brace of the else block.
Example:
#include <stdio.h>​

int main() {​
int number;​

printf("Enter an integer: ");​
scanf("%d", &number);​

if (number % 2 == 0) {​
printf("%d is an even number.\n", number);​
} else {​
printf("%d is an odd number.\n", number);​
}​

printf("End of the program.\n");​

return 0;​
}​

Output (for even input, e.g., 10):


Enter an integer: 10​
10 is an even number.​
End of the program.​

Output (for odd input, e.g., 7):


Enter an integer: 7​
7 is an odd number.​
End of the program.​

Explanation of the Example:


1.​ The program prompts the user to enter an integer and stores it in the number variable.
2.​ The if statement checks if the remainder of number divided by 2 is equal to 0 (number %
2 == 0).
3.​ If the condition is true (the number is even):
○​ The statement printf("%d is an even number.\n", number); inside the if block is
executed.
○​ The else block is skipped.
4.​ If the condition is false (the number is odd):
○​ The if block is skipped.
○​ The statement printf("%d is an odd number.\n", number); inside the else block is
executed.
5.​ Finally, the statement printf("End of the program.\n"); is always executed.
Key Differences from the if Statement:
●​ The if statement executes a block of code only when the condition is true. If the condition
is false, nothing happens (unless there are subsequent statements).
●​ The if-else statement provides an alternative block of code to execute when the condition
is false, ensuring that one of the two blocks of code is always executed based on the
truthiness of the condition.
Omitting Curly Braces (for single statements):
Similar to the if statement, if the if block or the else block contains only a single statement, you
can omit the curly braces. However, it's strongly recommended to always use curly braces for
better readability and to prevent potential errors when modifying the code later.
The if-else statement is a fundamental control flow mechanism in C, enabling programs to
handle different scenarios and execute different code paths based on specific conditions. It adds
more flexibility and decision-making capabilities to your programs compared to the simple if
statement.

You might also like