The if-else construct in C is employed to execute actions depending on a particular condition. The actions defined within the if block are carried out only when the specified condition evaluates to true.
Types of if Statements in C++
There are several types of if statements in C language. Some of them are as follows:
- If statement
- If-else statement
- If else-if ladder
- Nested if
Every decision-making construct in C programming offers specific features that enhance programming adaptability by executing different operations.
1) if Statement
In the realm of C programming, the if statement serves the purpose of evaluating a specified condition and executing corresponding operations based on the truthfulness of the condition. This feature finds frequent application in situations necessitating distinct operations for different scenarios.
Syntax
It has the following syntax:
if (expression){
//code to be executed
}
In this syntax,
- The if statement represents a conditional control structure that is used to execute a block of code only if the specified condition (expression) evaluates to true.
- The expression inside the parentheses is evaluated:
- If it returns true (non-zero), the code inside the {} block execute.
- If it returns false (zero), the block is skipped.
Flowchart of if Statement
C if Statement Example
Let's consider a basic example to demonstrate the implementation of the if statement in the C programming language.
Example
#include<stdio.h>
int main(){ //main function
int number=0;
printf("Enter a number:");
scanf("%d",&number);
if(number%2==0){
printf("%d is even number",number);
}
return 0;
}
Output
Enter a number:4
4 is an even number
Another illustration to determine the greatest value among three numbers
Let's consider an example to determine the greatest number among three numbers by utilizing the if statement in the C programming language.
Example
#include <stdio.h>
int main() //main function
{
int a, b, c;
printf("Enter three numbers: ");
scanf("%d %d %d",&a,&b,&c);
if(a>b && a>c)
{
printf("%d is largest",a);
}
if(b>a && b > c)
{
printf("%d is largest",b);
}
if(c>a && c>b)
{
printf("%d is largest",c);
}
if(a == b && a == c)
{
printf("All are equal");
}
}
Output
Enter three numbers: 12
23
34
34 is largest
Explanation:
In this instance, we've defined three integer variables: a, b, and c. Subsequently, it prompts the users to input the numbers. Upon entering the numbers, it evaluates the conditions and determines the largest number from the given three.
Characteristics of If statement
Several characteristics of if statement in C language are as follows:
- Objective: The if statement allows conditional execution of statement code on the basis of whether the expression given is correct.
- Condition Evaluation: The condition inside the if statement should be an expression that returns a boolean result-the true (non-zero) or false (zero).
- Flow Control: If the condition is true, the code block is executed. If the condition is false, the code block is skipped.
- Efficiency: The if statement helps to avoid unnecessary computations or operations by executing code only when needed.
2) if-else Statement
In C programming, the if-else statement is employed to execute two different operations based on a single condition. This statement extends the functionality of the if statement, allowing for the execution of distinct operations depending on whether the condition is true or false. If the condition evaluates to true, the if block is executed; otherwise, the else block is executed.
Syntax
It has the following syntax:
if (expression){
//code to be executed if the condition is true
}else{
//code to be executed if the condition is false
}
Flowchart of the if-else statement
C if-else statement Example
Let's examine a basic example to verify if a number is even or odd by utilizing an if-else statement in the C programming language.
Example
#include<stdio.h>
int main(){ //main function
int number=0;
printf("Enter a number: ");
scanf("%d",&number);
if(number%2==0){
printf("%d is even number",number);
}
else{
printf("%d is odd number",number);
}
return 0;
}
Output
Enter a number: 4
4 is an even number
Enter a number: 5
5 is an odd number
Another illustration to determine if an individual meets the criteria for voting or not.
Let's consider an example to determine the eligibility of an individual to participate in voting by utilizing the if-else statement in the C programming language.
Example
#include <stdio.h>
int main() //main function
{
int age;
printf("Enter your age: ");
scanf("%d",&age);
if(age>=18)
{
printf("You are eligible to vote...");
}
else
{
printf("Sorry ... you can't vote");
}
}
Output
Enter your age: 18
You are eligible to vote...
Enter your age: 13
Sorry ... you can't vote
Explanation:
In this instance, we verify the eligibility for voting by considering the age of the user. The program prompts the user to input their age. Following this, it employs an if-else condition: if the age is 18 years or above, it displays a message confirming the user's eligibility to vote; otherwise, it notifies them that they are ineligible.
Characteristics of if-else statement
Several characteristics of if-else statement in C are as follows:
- Objective: The if-else statement allows conditional execution of a block of the statement code if a given expression is evaluated and an alternative block if it evaluates incorrectly.
- Condition Evaluation: The condition inside should be an expression that returns a boolean result (non-zero) or false (zero).
- Flow control: If the condition is true, the first block (after is statement) executes; otherwise, the following code block executes, which ensures that one of the two blocks will be executed.
- Efficiency: The use of if-else statement helps the program make special decisions, which helps to avoid unnecessary checks or code execution by clearly separating the alternative paths.
3) If else-if ladder Statement
In C programming, the if-else-if ladder construct serves as a continuation of the if-else statement, especially when dealing with various conditions that require different actions. This construct enables the execution of a single condition among several statements. Within this structure, multiple scenarios can be managed using a sequence of if-else if conditions. It bears resemblance to the switch case statement, with the default block being executed in the absence of a matching case instead of the else block.
Syntax
It has the following syntax:
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{
//code to be executed if all the conditions are false
}
Flowchart of else-if ladder statement in C
C if-else-if ladder Example
Let's consider an illustration of an if-else-if statement in the C programming language.
Example
#include<stdio.h>
int main(){ //main function
int number=0;
printf("Enter a Number: ");
scanf("%d",&number);
if(number==10){
printf("number is equals to 10");
}
else if(number==50){
printf("number is equal to 50");
}
else if(number==100){
printf("number is equal to 100");
}
else{
printf("number is not equal to 10, 50 or 100");
}
return 0;
}
Output
Enter a Number: 4
the number is not equal to 10, 50 or 100
Enter a Number: 50
the number is equal to 50
Another instance to determine the student's grade based on the provided marks
Example
#include <stdio.h>
int main() //main function
{
int marks;
printf("Enter your Marks: ");
scanf("%d",&marks);
if(marks > 85 && marks <= 100)
{
printf("Congrats ! you scored grade A ...");
}
else if (marks > 60 && marks <= 85)
{
printf("You scored grade B + ...");
}
else if (marks > 40 && marks <= 60)
{
printf("You scored grade B ...");
}
else if (marks > 30 && marks <= 40)
{
printf("You scored grade C ...");
}
else
{
printf("Sorry you are fail ...");
}
}
Output
Enter your Marks: 10
Sorry, you are failing...
Enter your Marks: 40
You scored a grade C ...
Enter your Marks: 90
Congrats! You scored grade A ...
Explanation:
In this instance, we determine a grade according to the marks provided by the user. By utilizing a sequence of if-else if statements, the program evaluates the specific range in which the marks lie and displays the appropriate grade notification. If the marks are equal to or less than 30, it outputs a message indicating failure.
Characteristics of the if-else-if ladder
Several characteristics of the if-else-if ladder in C are as follows:
- Objective: The if-else-of ladder allows to examination of several conditions sequentially, the first correct positions execute the block and release the rest.
- Condition Evaluation: Each position is evaluated one by one, from top to bottom, until a true position is found. After the match, the later situations are ignored.
- Flow Control: Only the code block corresponding to the first true condition executes; if none are true, an optional final block can perform as a default.
- Efficiency: This structure efficiently handles many exclusive conditions, ensuring only one block run, which helps to avoid redundant checks.)
4) Nested if Statement
In the C programming language, a nested if statement consists of having another if statement inside its body. Successive tiers of conditional evaluations necessitate the use of this programming construct.
Syntax
It has the following syntax:
if (condition1) {
if (condition2) {
// code if both condition1 and condition2 are true
}
}
Flowchart of Nested if statement
C Nested if Statement Example
Let's consider a scenario to demonstrate the nested if statement in the C programming language.
Example
#include <stdio.h>
int main() { //main function
int marks;
printf("Enter your marks: ");
scanf("%d", &marks);
if (marks >= 50) {
if (marks >= 75) {
printf("Distinction");
} else {
printf("Passed");
}
} else {
printf("Failed");
}
return 0;
}
Output
Enter your marks: 65
Passed
Explanation:
In this instance, we showcase a nested if statement in the C programming language. Initially, it evaluates whether the student has achieved a passing grade with marks greater than or equal to 50, and subsequently examines if they have attained a distinction with marks greater than or equal to 75. Depending on the outcome, it displays either "Distinction", "Passed", or "Failed".
Difference between if, if-else, if-else-if ladder
There exist multiple variances among if, if-else, and if-else-if ladder statements in the C programming language. The key disparities include:
| Feature | if Statement | if-else Statement | if-else if ladder |
|---|---|---|---|
| Purpose | It executes a block if a condition is true. | It executes one block if the condition is true; else, executes another block. | It checks multiple conditions sequentially and executes a block for the first true condition. |
| Syntax | Single condition check | Condition with two possible outcomes | Multiple conditions checked in sequence |
| Number of branches | One branch (conditional block) | Two branches (if and else blocks) | Various branches (if, else if, else) |
| Execution flow | It executes the block only if the condition is true; skips otherwise. | It executes exactly one of the two blocks. | It executes only the block corresponding to the first true condition and skips remaining. |
| When to use | When only one condition needs checking. | When there is a binary choice between two outcomes. | When you have multiple mutually exclusive conditions. |
| Example use case | Check if a number is positive | Check if a number is even or odd | Grade classification based on score ranges. |
| Number of condition evaluations | Only one condition was evaluated. | One condition was evaluated with an alternate block. | Multiple conditions are evaluated in order until one is true. |
Conclusion
In summary, C programming employs if statements as fundamental decision-making tools that activate specific responses depending on varying conditions. Various forms include basic if statements, if-else structures, if-else-if chains, nested if statements, and the ternary operator, allowing adaptation to a wide range of scenarios. The choice of if statement structure by a programmer significantly impacts the code's readability, efficiency, and maintainability.