Conditional Structures
1 The Importance of Decision-Making in Problem Solving
Decision-making is a fundamental aspect of problem-solving. Many real-world problems require different actions to be taken based on specific conditions. For example:
-
If it is raining, take an umbrella.
-
If the temperature is above 30°C, turn on the air conditioner.
Conditional structures in algorithms help model such decisions, allowing programs to choose a path based on the input or situation.
In terms of programming, we often need to respond to input differently based on its values. For example, choosing a mode of operation, such as enabling a calculator to perform addition, subtraction, multiplication, or division based on a user’s selection. Another scenario could be validating input, such as checking if a password meets security criteria before granting access. Decision-making is the foundation of creating dynamic, responsive programs that adapt to varying situations and requirements. It allows algorithms to intelligently branch and handle diverse problem-solving pathways.
In programming, conditional statements allow us to control the flow of execution based on certain conditions. They enable a program to make decisions and execute different blocks of code depending on whether a specific condition evaluates to true or false. This introduces the concept of multi-path execution, meaning that the program can follow different paths or actions based on various conditions.
1.1 The Concept of Conditions
A condition is an expression that evaluates to either true or false. Based on the result of this evaluation, the program can choose which set of instructions to execute. Conditional statements usually follow a structure where the program checks one or more conditions and takes actions accordingly.
Consider a simple decision-making scenario:
-
If it’s raining, you take an umbrella.
-
If it’s sunny, you wear sunglasses.
-
If it’s cold, you wear a jacket.
This is a multi-path decision where the action changes based on the condition (weather).
1.2 Types of Conditional Statements
-
If Statement: Executes a block of code if a condition is true.
-
If-Else Statement: Executes one block of code if a condition is true, otherwise executes another block.
-
Else-If Ladder: Allows checking multiple conditions in sequence, executing different blocks of code based on which condition is true.
-
Switch (or equivalent structures): Allows the execution of one block of code among many options based on the value of an expression (typically used for checking discrete values).
1.3 Conditional Flow Control
Let’s visualize the control flow:
-
If the condition is true, the program takes one path.
-
If the condition is false, it takes another path (either an else block or simply skips to the next statement).
-
Some programs may have multiple checks to handle more complex decision-making scenarios.
This capability makes programs more dynamic and responsive to user input or data conditions.
2 Simple Conditional Structure (If ... Then ... EndIf)
A simple conditional structure executes a block of code only if a condition is true.
Pseudo-code Example:
Vars num: integer Start read (num) If num > 0 Then write ("The number is positive") EndIf End
Equivalent C Code:
#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (num > 0) {
printf("The number is positive.\n");
}
return 0;
}
3 Double Conditional Structure (If ... Then ... Else ... EndIf)
A double conditional structure provides an alternative action if the condition is false.
Pseudo-code Example:
Vars num: integer Start write ("Enter a number:") read (num) If num % 2 = 0 Then write ("The number is even") Else write ("The number is odd") EndIf End
Equivalent C Code:
#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (num % 2 == 0) {
printf("The number is even.\n");
} else {
printf("The number is odd.\n");
}
return 0;
}
4 Nested Conditional Structures
Nested conditionals are conditionals inside other conditionals, used for more complex decisions.
Pseudo-code Example:
Vars num: integer Start write ("Enter a number:") read (num) If num > 0 Then If num < 100 Then write ("The number is positive and less than 100") EndIf Else write ("The number is not positive") EndIf End
Equivalent C Code:
#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (num > 0) {
if (num < 100) {
printf("The number is positive and less than 100.\n");
}
} else {
printf("The number is not positive.\n");
}
return 0;
}
5 Multiple-Choice Conditional Structures
For multiple conditions, If ... Else If ... Else ... EndIf can be used.
Pseudo-code Example:
Vars marks: integer Start read (marks) If marks >= 90 Then write ("Grade: A") Else If marks >= 75 Then write ("Grade: B") Else If marks >= 50 Then write ("Grade: C") Else write ("Grade: F") EndIf End
Equivalent C Code:
#include <stdio.h>
int main() {
int marks;
printf("Enter marks: ");
scanf("%d", &marks);
if (marks >= 90) {
printf("Grade: A\n");
} else if (marks >= 75) {
printf("Grade: B\n");
} else if (marks >= 50) {
printf("Grade: C\n");
} else {
printf("Grade: F\n");
}
return 0;
}
6 Conditional Statements in C Language
In the C language, conditionals are implemented using the if, else, and else if constructs, as shown in the examples above. They are an essential part of control flow, enabling decisions to be made during program execution.
6.1 If Statement
#include <stdio.h>
void main()
{
int number = 5;
// If the number is positive, print the message
if (number > 0)
{
printf("The number is positive.\n");
}
}
In this example, the program checks if number > 0. If the condition is true, it prints "The number is positive." If the condition were false, nothing would be printed.
6.2 If-Else Statement
#include <stdio.h>
void main()
{
int number = -3;
// Check whether or not a number is positive
if (number > 0)
{
printf("The number is positive.\n");
}
else
{
printf("The number is not positive.\n");
}
}
Here, the program checks if the number is positive. If true, it prints one message, but if false, it prints a different message.
6.3 Else-If Ladder
#include <stdio.h>
void main()
{
int number = 0;
// Check if the number is positive, negative, or zero
if (number > 0)
{
printf("The number is positive.\n");
}
else if (number < 0)
{
printf("The number is negative.\n");
}
else
{
printf("The number is zero.\n");
}
}
Here, multiple conditions are evaluated. First, it checks if the number is positive, then negative, and finally, if neither is true, it checks if the number is zero.
6.4 Switch Statement
#include <stdio.h>
void main()
{
int choice = 2;
// Switch case for selecting an option
switch (choice)
{
case 1:
printf("Option 1 selected.\n");
break;
case 2:
printf("Option 2 selected.\n");
break;
case 3:
printf("Option 3 selected.\n");
break;
default:
printf("Invalid option.\n");
break;
}
}
In this example, the switch statement checks the value of choice and executes the corresponding block of code. If choice doesn’t match any of the case values, it falls to the default case.
Example: Find the Maximum of Two Numbers
#include <stdio.h>
void main()
{
int num1;
int num2;
int max;
printf("Enter the first value: ");
scanf("%d", &num1);
printf("Enter the second value: ");
scanf("%d", &num2);
// Conditional statement to find the maximum number
if (num1 > num2)
{
// this block is executed only if num1 > num2
max = num1;
}
else
{
// this block is executed only if num1 =< num2
max = num2;
}
printf("The maximum is: %d\n", max);
}
Example: Check if a Number is Multiple of Some Other Numbers
#include <stdio.h>
void main()
{
int num;
// Ask for the user's input
printf("Enter an integer: ");
scanf("%d", &num);
// Check if the number is a multiple of 2, 3, or 7
if (num % 2 == 0)
{
printf("%d is a multiple of 2.\n", num);
}
if (num % 3 == 0)
{
printf("%d is a multiple of 3.\n", num);
}
if (num % 7 == 0)
{
printf("%d is a multiple of 7.\n", num);
}
// If the number is not a multiple of 2, 3, or 7
if (num % 2 != 0 && num % 3 != 0 && num % 7 != 0)
{
printf("%d is not a multiple of 2, 3, or 7.\n", num);
}
}