Conditional Statements are used to execute different blocks of code based on whether a condition is true or false.
In C programming language, the most common conditional statements are:
The if else statement is used to execute one block of code if a condition is true and another block of code if the condition is false.
When the condition is true, the first block of code is executed. When the condition is false, the second block of code is executed.
The computer can be said to be lazy. It starts at the top and keeps checking until it finds a "TRUE" answer. Once it finds one, it does that action and ignores everything else below it.
In C programming language, the syntax for an if else statement is as follows:
if (condition) {
// code to be executed if condition is true
} else {
// code to be executed if condition is false
}
Comparators are used to compare values. The most common comparators are:
To illustrate the use of if else statements, let's write a simple program which takes an integer value from the user and displays whether it is even or odd.
The algorithm is as follows:
In C programming language, the code for this algorithm would look like this:
#include <stdio.h>
int main() {
int num;
printf("Enter an integer: ");
scanf("%d", &num);
if (num % 2 == 0) {
printf("The number is even.\n");
} else {
printf("The number is odd.\n");
}
return 0;
}
In the above C program, the condition being checked is whether the number is divisible by 2 using the modulo operator (%). If the result of num % 2 is 0, it means the number is even; otherwise, it is odd.
Note: The modulo operator (%) returns the remainder of a division operation. If a number is divisible by 2, the remainder will be 0, indicating that the number is even. Otherwise, the number is odd.
Another program could be a program which asks the user to enter the age and determines if they are eligible to vote (assuming the voting age is 18).
The algorithm is as follows:
In C programming language, the code for this algorithm would look like this:
#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;
}
The if else statement is used to check multiple conditions and execute different blocks of code based on which condition is true.
In C programming language, the syntax for an if else statement is as follows:
if (condition1) {
// code to be executed if condition1 is true
} else if (condition2) {
// code to be executed if condition2 is true
} else if (condition3) {
// code to be executed if condition3 is true
}
.
.
.
else if (conditionN) {
// code to be executed if conditionN is true
} else {
// code to be executed if none of the conditions are true
}
The number of the else if clauses depends on the number of conditions to be checked.
Since the first is if and the last is else, the number of else if can be calculated as:
Number of else if clauses = Number of conditions - 2
For instance if there are 5 position conditions, there will be 3 (5 - 2) else if clauses.
Let's use the B.E.C.E grading system as an example:
| Score | Grade |
| 75% - 100% | 1 |
| 70% - 74% | 2 |
| 65% - 69% | 3 |
| 60% - 64% | 4 |
| 55% - 59% | 5 |
| 50% - 54% | 6 |
| 45% - 49% | 7 |
| 40% - 44% | 8 |
| 0% - 39% | 9 |
There are 9 options and hence 7 (9 - 2) else if clauses.
To write a program that asks the user to enter their score and displays the corresponding grade:
The algorithm is as follows:
In C programming language, the code for this algorithm would look like this:
#include <stdio.h>
int main() {
int score;
printf("Enter your score: ");
scanf("%d", &score);
if (score >= 75) {
printf("Your grade is 1.\n");
} else if (score >= 70) {
printf("Your grade is 2.\n");
} else if (score >= 65) {
printf("Your grade is 3.\n");
} else if (score >= 60) {
printf("Your grade is 4.\n");
} else if (score >= 55) {
printf("Your grade is 5.\n");
} else if (score >= 50) {
printf("Your grade is 6.\n");
} else if (score >= 45) {
printf("Your grade is 7.\n");
} else if (score >= 40) {
printf("Your grade is 8.\n");
} else {
printf("Your grade is 9.\n");
}
return 0;
}
It is assumed that the user will enter a valid score between 0 and 100.
We will modify this code when we get to iteration (repetition) to repeatedly ask the user to enter a valid score.
Nested if else statements are if else statements that are contained within another if else statement. They allow you to check multiple conditions in a hierarchical manner.
We can use a nested if else statement to implement the grade-checking algorithm more efficiently.
We will this time check if the score entered is within a specific range before determining the grade
The disadvantage of this design is that, it only runs once and user has to start the program again to enter a new score.
The algorithm is as follows:
The modified C code for this algorithm would look like this:
#include <stdio.h>
int main() {
int score;
printf("Enter your score: ");
scanf("%d", &score);
if (score < 0 || score > 100) {
printf("Invalid score. Please enter a score between 0 and 100.\n");
} else {
if (score >= 75) {
printf("Your grade is 1.\n");
} else if (score >= 70) {
printf("Your grade is 2.\n");
} else if (score >= 65) {
printf("Your grade is 3.\n");
} else if (score >= 60) {
printf("Your grade is 4.\n");
} else if (score >= 55) {
printf("Your grade is 5.\n");
} else if (score >= 50) {
printf("Your grade is 6.\n");
} else if (score >= 45) {
printf("Your grade is 7.\n");
} else if (score >= 40) {
printf("Your grade is 8.\n");
} else {
printf("Your grade is 9.\n");
}
}
return 0;
}
The grade checking if else is the nested if-else structure.
We could also modify the voting system to ensure user doesn't enter a negative number using the nested if-else structure.
The modified C code for this would look like this:
#include <stdio.h>
int main() {
int age;
printf("Enter your age: ");
scanf("%d", &age);
if (age < 0) {
printf("Invalid age. Please enter a valid age.\n");
} else {
if (age >= 18) {
printf("You are eligible to vote.\n");
} else {
printf("You are not eligible to vote.\n");
}
}
return 0;
}
The switch statement is used to execute different blocks of code based on the value of a variable or expression. It is an alternative to using multiple if else statements when you have a large number of conditions to check.
In C programming language, the syntax for a switch statement is as follows:
switch (variable) {
case value1:
// code block
break;
case value2:
// code block
break;
.
.
.
case valuen:
// code block
break;
default:
// code block
break;
}
The switch statement evaluates the variable and compares it with each case label. When a match is found, the corresponding code block is executed. The break statement is used to exit the switch statement after a match is found and block of code executed.
The default case is optional and is executed if none of the case labels match the variable.
We can use the switch alternative to write the code for the even number check.
The C code for this would look like this:
#include <stdio.h>
int main() {
int number;
printf("Enter an integer: ");
scanf("%d", &number);
switch (number%2) {
case 0:
printf("The number is even.\n");
break;
default:
printf("The number is not even.\n");
break;
}
return 0;
}
In the above code, the expressing number%2 returns either 0 or 1 which is compared to the cases (0 and default).
When it is 0, the block of code after the case 0: is executed.
The break; makes it to exit the switch since a match is found.
Without the break;, the execution will continue until next break; is found or the entire rest of code is executed.
Another example of the switch statement would be to ask a user to enter from 1 - 7 to represent the days of the week.
1 - Monday
2 - Tuesday
3 - Wednesday
4 - Thursday
5 - Friday
6 - Saturday
7 - Sunday
The C code for this would look like this:
#include <stdio.h>
int main() {
int day;
printf("Enter a number (1-7) to represent the day of the week: ");
scanf("%d", &day);
switch (day) {
case 1:
printf("Monday\n");
break;
case 2:
printf("Tuesday\n");
break;
case 3:
printf("Wednesday\n");
break;
case 4:
printf("Thursday\n");
break;
case 5:
printf("Friday\n");
break;
case 6:
printf("Saturday\n");
break;
case 7:
printf("Sunday\n");
break;
default:
printf("Invalid input. Please enter a number between 1 and 7.\n");
break;
}
return 0;
}
Note: the break statements are crucial to prevent fall-through to subsequent cases.
When the break is omitted, the program will continue to execute the next case, leading to unintended behavior.