Control Flow: Conditional Statements (if, else, switch)
Using `if`, `else`, and `switch` statements to control the flow of execution based on conditions.
C Programming: Control Flow - Conditional Statements (else)
Understanding Control Flow
In programming, control flow determines the order in which statements are executed. Conditional statements, like the if
and else
statements, allow your program to make decisions and execute different blocks of code based on whether a condition is true or false. This is crucial for creating dynamic and responsive programs.
The else
Statement
The else
statement is used in conjunction with the if
statement. It provides a block of code to be executed when the condition in the if
statement is false. Think of it as providing an "alternative" path for your program to follow.
Syntax:
if (condition) {
// Code to execute if the condition is true
} else {
// Code to execute if the condition is false
}
Explanation:
- The
condition
inside the parentheses()
of theif
statement is evaluated. - If the
condition
is true (non-zero), the code block enclosed in the curly braces{}
immediately following theif
statement is executed. - If the
condition
is false (zero), the code block enclosed in the curly braces{}
immediately following theelse
statement is executed.
Example in C:
#include <stdio.h>
int main() {
int age;
printf("Enter your age: ");
scanf("%d", &age);
if (age >= 18) {
printf("You are eligible to vote.\n");
} else {
printf("You are not eligible to vote.\n");
}
return 0;
}
Explanation of the Example:
- The program prompts the user to enter their age.
- The
scanf
function reads the age entered by the user and stores it in theage
variable. - The
if
statement checks ifage
is greater than or equal to 18. - If
age
is 18 or greater (the condition is true), the message "You are eligible to vote." is printed. - If
age
is less than 18 (the condition is false), the message "You are not eligible to vote." is printed.
Another Example: Checking if a number is even or odd
#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);
}
return 0;
}
Explanation of the Example:
- The program prompts the user to enter an integer.
- The
scanf
function reads the number entered by the user and stores it in thenumber
variable. - The
if
statement checks if the remainder ofnumber
divided by 2 is equal to 0 (number % 2 == 0
). This determines if the number is even. - If the remainder is 0 (the condition is true), the message that the number is even is printed.
- If the remainder is not 0 (the condition is false), the message that the number is odd is printed.
Key Takeaways:
- The
else
statement provides a way to execute code when theif
condition is false. - It improves the flexibility and decision-making capabilities of your programs.
- Using
if
andelse
allows your program to respond differently to various inputs and situations.