Making decisions is a complicated task for machines, and it doesn’t differ too much from how we make decisions on a day-to-day basis. Think about driving in traffic from one place to another. Unless traffic lights tell you to stop, go, or take a turn, you will drive continuously without stopping. Similarly, programming machines require you to tell the computer what to do when it encounters certain conditions. In programming, conditions would mean it can run or skip blocks of code if certain conditions or set of conditions are met or not met.
What are Conditional Statements?
Conditional statements in C allow programs to make decisions based on conditions we define, just like everyday life. They let the computer decide which part of the code to execute, depending on whether a condition is true or false. For example, consider ordering food online: "If the total exceeds ₹500, apply a discount." These statements help control a program's flow, direct it to run in specific ways, and make it more responsive to different situations. Without conditional statements in c, coding would feel rigid and linear.
Importance of Conditional Statements
Conditional statements are the backbone of decision-making in programming. Here’s why they matter:
- Control the Program’s Flow: This helps direct the execution of code based on specific conditions, making the programs flexible and dynamic.
- Handle Real-Life Scenarios: Conditional statements are needed for different tasks, from login authentication to showing discounts during checkout. They allow computers to act based on user inputs.
- Improve Efficiency: Conditional statements improve efficiency by skipping unnecessary code or branching into relevant blocks when needed. It can optimize performance and speed of execution.
- Enable Complex Logic: Conditional statements allow the implementation of algorithms that rely on multiple decision points.
- Essential for Interaction: Interactive features like menus or error handling are built using conditional statements.
Types of Conditional Statements in C
Now, let’s take a look at some conditional statements:
1. The “If” Statement
The “If” statement in C is like giving the computer a simple decision. It checks a condition, and if it is true, it executes a specific block of code. If the condition is false, it skips that block entirely.
The flow chart above shows how the command in the If condition flows. The result is that either the condition is true or false. The value of true is nonzero, and the value of false is always zero.
Below is an example of how the if statement looks like
if (condition) {
// code to run if the condition is true
}
Now let’s see an example:
int grade = 95;
if (grade >= 90) {
printf("You got an A");
}
This line of code states that if the grade is 90 or higher, it should print “You got an A.” If it doesn’t satisfy that condition, the program moves on to the next line or code.
2. If-Else Statement
The if-else is a more developed form of the If statement. It comes into play if you want the program to do one thing if the if statement is true and a different thing if it is false. The flowchart of this statement is as follows:
Here’s the structure of how the c language if else program looks like:
if (condition) {
// code to run if the condition is true
} else {
// code to run if the condition is false
}
Now let’s look at c if else examples. In this one, the program checks if the person is old enough to vote by checking their age. If they’re 18 or above, it says old enough to vote, if not, It says no old enough to vote.
int age = 25;
int voting_age = 18;
if (age >= voting_age) {
printf("Old enough to vote!");
} else {
printf("Not old enough to vote.");
}
3. Else- If Statements
Oftentimes, we’ll have to check multiple conditions when running a program. The Else-If condition is then used to test a series of conditions sequentially. Simply put, it says, “if this condition is true, execute this. If not, check the next condition”. The flow chart below shows the else-if c programming for three conditions:
Here’s how the c else if structure looks:
if (condition1) {
// code to run if condition1 is true
} else if (condition2) {
// code to run if condition2 is true
} else {
// code to run if all conditions are false
}
Now, let’s examine the else if statement in c with an example. In this, the program gives a grade based on the numerical score of the student.
int grade = 85;
if (grade >= 90) {
printf("You got an A");
} else if (grade >= 80) {
printf("You got a B");
} else if (grade >= 70) {
printf("You got a C");
} else {
printf("You need to study more");
}
4. Nested If-Else Statements
Nested if-else statements in C are like decision-making within another decision. It means having an if or else block inside an existing if or else block.Here's how it works:
- Outer if condition: First, the program begins by checking the outer if condition. If the condition is true, the code inside the outer if block runs. If the condition is false, the program skips this block and move to the outer else block (if it exists).
- Nested if condition (inside the outer if): Inside the outer if block, there might be an other another if condition (which is the nested part). If the nested if is true, the code inside the nested if block runs. If the nested if is false, the else block inside (if it exists) runs.
- Outer else block: If the outer if condition is false, the program skips the entire outer if block and goes directly to the outer else block. This outer else block can also contain another if inside it for additional decisions.
This structure is useful when decisions depend on multiple levels of conditions, like checking user input and then verifying permissions.The flowchart for nested if else statement in c is as follows:
Here is what this conditional statement in c looks like:
if (condition1) {
// code to run if condition1 is true
if (condition2) {
// code to run if both condition1 and condition2 are true
} else {
// code to run if condition1 is true but condition2 is false
}
} else {
// code to run if condition1 is false
}
Now let’s take a look at an example of how the nested if-else classifies the given number as positive, odd or even. The end result of this program would be that the number 10 is positive and even.
int main() {
int num = 10;
if (num > 0) {
printf("The given number is positive.\n");
if (num % 2 == 0) {
printf("The given number is even.\n");
} else {
printf("The given number is odd.\n");
}
} else {
printf("The given number is non-positive.\n");
}
return 0;
}
Conclusion
As you already have ideas about conditional statements, now it’s time to witness how these concepts work. These are the statements upon which decisions in programming are made, and as you work through them here, they will become second nature to you. You can start simple with programs involving if and else statements or if-else and else-if statements – and then make simple real-world simulations like checking grades, discounts, etc. The more you code, the better you are placed to learn how to make the next program more intelligent and dynamic.
Develop Industry-Relevant Skills While in College to Crack Your Placement!
Explore ProgramFrequently Asked Questions
1. What are conditional statements in C?
Conditional statements in C make decisions based on specified conditions. They determine which block of code to execute and which one to skip.
2. What is the difference between if and else?
The if statement runs the code when a condition is true. The else condition provides an alternative block to run when the condition is false.
3. Can I nest conditional statements?
Yes, you can place if or else blocks inside other if or else blocks. This enables you to create a program with more detailed decision-making.
4. What is the difference between if-else-if and nested if statements?
The if-else-if statements check multiple conditions sequentially in a single structure by using if and else statements. The nested if statements involve placing one if inside another.
5. How do I practice conditional statements?
You can start with small programs, like checking if a number is odd or even, or creating a simple menu-based program. Use nested functions to practice more complicated trees and generate better programs.