Selection Statements in C
Introduction
Programming languages must-have selection statements because they allow programmers to direct execution based on specific criteria. Selection statements allow you to make judgments and pick multiple courses of code execution in the C programming language depending on the values of variables or expressions. This article offers a thorough tutorial on how to master selection statements in C. It explores the different categories of selection statements and offers real-world examples to help you better grasp them.
If Statement
In C, the if Statement is the simplest basic selection statement form. You can use a given to run some code if a given is met. In an if statement, the following syntax is used:
if (condition) { { // code to be executed if the condition is true }
Example Programs for if Statement:
#include<stdio.h> int main(){ int age = 18; if (age >= 18) { printf("You are eligible to vote. "); } else { printf("You are not eligible to vote. "); } return 0; }
Output
Explanation
In the above program, we utilize the if Statement to check a person's age to see whether they are old enough to vote. The program prints "You are eligible to vote" if the criterion "age >= 18" evaluates to true. In the absence of that, it says, "You are not eligible to vote."
If else Statement
A source code block can be executed if a certain condition is true and a different piece of code if the condition is false, using the if-else Statement, a selection statement.
Syntax:
if (condition) { // code will be executed if the condition is true } else { // code will be executed if the condition is false }
Example 1:
#include<stdio.h> int main(){ int num = 10; if (num % 2== 0) { printf("The number is even. "); } else { printf("The number is odd. "); } return 0; }
Output
Explanation:
The program in this example determines if the number "num" is even or odd. The statement "The number is even" is printed if "num" is divisible by 2 (that is if the condition "num% 2 == 0" is true). If not, it displays "The number is odd."
Example 2:
#include<stdio.h> int main(){ int age = 18; if (age >= 18) { printf("You are eligible to vote. "); } else { printf("You are not eligible to vote. "); } return 0; }
Output:
Explanation:
In the abovementioned illustration, the code uses a person's age to calculate their qualifying requirements. If the age is higher than 18, then it will execute the statement "You are eligible to vote." If it is lesser, it will execute the statement, "You are not eligible to vote."
Nested If-else Statement
A nested if-else statement combines numerous if-else statements by nesting one if-else Statement within another. It enables the evaluation of several conditions to construct complicated decision-making systems.
Syntax:
if (condition1) { // Code is executed if condition1 is true if (condition2) { // Code is executed if both condition1 and condition2 are true } else { // Code is executed if condition1 is true but condition2 is false } } else { // Code is executed if condition1 is false }
Example 1:
Int main(){ int num = 10; if (num > 0){ if (num % 2== 0) { printf("The number is positive and even. "); } else { printf("The number is positive and odd. "); } } else { printf("The number is not positive. "); } return 0; }
Output:
Explanation:
The program in this example decides if the input number "num" is positive and even, positive and odd, or not positive. If 'num' is more than 0 (i.e., the condition 'num > 0' is true), the inner if-else Statement further decides whether it is even or odd.
Example 2:
#include<stdio.h> int main(){ int score = 75; if (score >= 90) { printf("Grade: A "); } else { if (score >= 80) { printf("Grade: B "); } else { printf("Grade: C or below "); } } return 0; }
Output:
Explanation:
In this case, the program grades students depending on their exam "score." "Grade: A" is printed if the "score" is greater than or equal to 90. It examines the nested if-else expression if the score is less than 90. "Grade: B" is printed if the "score" exceeds or exceeds 80. Without that, it publishes "Grade: C or below."
Switch Statement
The switch statement offers a stylish approach to selecting between several options based on the result of an expression or a variable. When you have many cases to weigh, it is really helpful.
The switch statement's syntax is as follows:
switch (expression) { case constant1: // Code to be executed if the expression matches constant1 break; case constant2: // Code to be executed if the expression matches constant2 break; ... default: // Code to be executed if the expression doesn't match any constant break; }
A switch statement compares each constant with the expression after evaluating it. The relevant piece of code is run if a match is discovered. If no match is discovered, the default block's code is run. Check out the illustration below:
#include<stdio.h> int main(){ int day = 3; switch (day) { case 1: printf("Monday "); break; Case 2: printf("Tuesday "); break; Case 3: printf("Wednesday "); break; default: printf("Invalid day "); break; } }
Output:
Explanation:
In the above program, a switch statement is used to identify the day's name based on the variable's value, "day".
- The variable 'day' is given the value 3.
- The " day " value is checked when the switch statement is evaluated.
- If "day" is 1, the "case 1" block's function is run, printing "Monday" before exiting the switch statement.
- 'case 2' block code, which outputs "Tuesday" and exits the switch statement, is run if 'day is 2'.
- If "day" is 3, the "case 3" block's code is run, printing "Wednesday" before exiting the switch statement.
- If none of those as mentioned earlier scenarios apply, the default block's code is run, which outputs "Invalid day" and exits the switch statement.
Since the day has a value of 3 in this specific example, the code in the case 3 block is performed, and the word "Wednesday" is written.
The switch statement provides a straightforward approach to choosing between multiple possibilities when dealing with various circumstances based on the value of a variable or an expression, making the code simpler to comprehend and maintain.
Conclusion:
Programmers frequently employ selection statements because they enable them to choose what to do and how to continue in response to certain conditions. The if Statement, switch statement, and ternary operator were just a few of the selection statements in C that were covered in this article. We also looked at layered selection statements, emphasizing how they may be used to build intricate decision-making frameworks. By mastering selection statements, you may develop effective, adaptable code that reacts dynamically to many conditions. Your newly acquired skills prepare you to take on various C programming tasks.