Conditional Statements
Fri, 08 Nov 2024
In programming, we often need to perform different sets of actions depending on certain conditions. For example:
In C, decision-making is done using conditional statements, which enable the program to decide which path of execution to follow. These include:
if
statement,if-else
statement,ternary
) operator,switch
statementLet's dive into the C version of decision control instructions and how we can use them in different scenarios.
if
statement to make decisions. The general form of the if
statement looks like this:if
statement:if(conditional_statement){
// execute is condition is true
}
conditional_statement
:i < 10
) or combination of multiple condition (i>5 && i < 10
), or any function that returns boolean.conditional_statement
is enclosed in parentheses ()
.conditional_statement
evaluates to true
, the block of code inside the braces {} is executed.int quantity = 2000;
if (quantity > 1000) {
printf("10%% discount\n");
}
10% discount
if-else
statement is:if(conditional_statement){
// execute is condition is true
}
else{
// execute if false
}
int quantity = 2000;
if (quantity > 1000) {
printf("10%% discount\n");
}
else{
printf("No discount\n");
}
10% discount
else-if
ladder allows us to check multiple conditions in sequence.else-if
ladder:if (condition1) {
// execute if condition1 is true
}
else if (condition2) {
// execute if condition2 is true
}
else if (condition3) {
// execute if condition3 is true
}
else {
// execute if none of the above conditions are true
}
int quantity = 1500;
if (quantity > 2000) {
printf("20%% discount\n");
}
else if (quantity > 1000 && quantity <= 2000) {
printf("10%% discount\n");
}
else {
printf("No discount\n");
}
10% discount
quantity
is more than 2000, it prints "20% discount".quantity
is between 1001 and 2000, it prints "10% discount".quantity
is 1000 or below, it prints "No discount".int number = 0;
if (number >= 0) {
if (number == 0) {
printf("The number is zero.\n");
} else {
printf("The number is positive.\n");
}
} else {
printf("The number is negative.\n");
}
The number is zero.
The conditional or ternary operator (? :
) offers a quick way to check a condition and choose between two values.
This operator is often used to replace simple if-else
statements for readability, especially in assignments and quick checks.
Structure of ternary
operator:
condition ? expression_if_true : expression_if_false;
How the Ternary Conditional Operator Works
condition
is evaluated first.condition
is true (non-zero), expression_if_true
is evaluated and becomes the result.condition
is false (0), expression_if_false
is evaluated and becomes the result.Example:
int age = 18;
const char* eligibility = (age >= 18) ? "Eligible to vote" : "Not eligible to vote";
printf("%s\n", eligibility);
Output:
Eligible to vote
Explaination:
Here, the condition (age >= 18) is evaluated.
As age >= 18 is true, eligibility is set to "Eligible to vote". If age < 18, the condition would be false, and eligibility would be set to "Not eligible to vote".
if
statement: Executes a block if a condition is true.if-else
statement: Executes one block if the condition is true and another if it's false.if-else
: Used when multiple conditions need to be checked, each depending on the previous.if-else
.The switch
statement allows the program to evaluate an expression (typically an integer or character) and jump directly to the corresponding case label.
Each case represents a possible value of the expression, and the corresponding block of code is executed when the value matches.
If no match is found, an optional default
block is executed.
Structure of switch
statement:
switch (expression) {
case constant1:
// code to execute if expression matches constant1
break;
case constant2:
// code to execute if expression matches constant2
break;
case constant3:
// code to execute if expression matches constant3
break;
default:
// code to execute if no cases match
}
How the switch Statement Works
break
statement ensures that the program exits the switch after executing the matching case.Example:
int day = 3;
switch (day) {
case 1:
printf("Monday\n");
break;
case 2:
printf("Tuesday\n");
break;
case 3:
printf("Wednesday\n");
break; // <----------------------(1)
default:
printf("Other day\n");
}
Output:
Wednesday
Output if the break (1) is not used:
Wednesday
Other day
This is a comment 1.
This is a comment 2.