Mastering the C If Statement: Essential for Conditional Execution
Explore the importance of the if statement in C programming for conditional execution. Understand how to use the optional else keyword to define actions based on whether conditions are true or false, enhancing your program's decision-making capabilities.
C - The If Statement
Conditional execution of instructions is fundamental in programming. The if statement in C is used for decision-making based on conditions. An optional else keyword can be used to specify actions if the condition is false.
Syntax of if Statement
The syntax for the if statement is as follows:
Syntax
if(boolean_expression) {
/* statement(s) will execute if the boolean expression is true */
}
How if Statement Works
The if statement uses curly brackets to form a code block. If the boolean expression evaluates to true, the code block inside the if statement executes. If false, the code following the block executes.
In C, any non-zero and non-null values are considered true, while zero or null values are false.
Flowchart of if Statement
The flowchart for the if statement is as follows:
Example of if Statement in C
This example shows a basic use of the if statement, which checks if a variable is less than 20.
Code
#include
int main() {
int a;
a = 12; // Change to 40 and run again
printf("Value of a is : %d\n", a);
if(a < 20) {
printf("a is less than 20\n");
}
return 0;
}
Output
Value of a is : 12
a is less than 20
if Statement with Logical Operations
You can use logical operators && (AND) or || (OR) in the if statement.
Code
#include
int main() {
int a, b, c;
a = 10; b = 5; c = 7; // Change to 10, 20, 15 and run again
if (a >= b && a >= c) {
printf("a is greater than b and c\n");
}
printf("a: %d b: %d c: %d\n", a, b, c);
return 0;
}
Output
// When values for a, b, and c are 10, 5, 7
a is greater than b and c
a: 10 b: 5 c: 7
// When values for a, b, and c are 10, 20, 15
a: 10 b: 20 c: 15
Multiple if Statements
You can use multiple if statements to check different conditions. For example, calculating discounts based on the bill amount.
Code
#include
int main() {
int amount;
float discount, net;
amount = 500; // Change to 2250 and 5200 and run again
if (amount < 1000) {
discount = 0;
}
if (amount >= 1000 && amount < 5000) {
discount = 5;
}
if (amount >= 5000) {
discount = 10;
}
net = amount - amount * discount / 100;
printf("Amount: %d Discount: %f Net payable: %f\n", amount, discount, net);
return 0;
}
Output
// When the bill amount is 500
Amount: 500 Discount: 0.000000 Net payable: 500.000000
// When the bill amount is 2250
Amount: 2250 Discount: 5.000000 Net payable: 2137.500000
// When the bill amount is 5200
Amount: 5200 Discount: 10.000000 Net payable: 4680.000000
Checking Multiple Conditions with if Statement
Multiple conditions can be checked using logical operators. For example, determining if a student passes based on average marks and individual subject marks.
Code
#include
int main() {
int phy, maths;
float avg;
phy = 50; maths = 50; // Change to 40, 40 and 80, 40
avg = (float)(phy + maths) / 2;
printf("Phy: %d Maths: %d Avg: %f\n", phy, maths, avg);
if (avg >= 50 && (maths >= 35 && phy >= 35)) {
printf("Result: Pass\n");
}
if (avg < 50) {
printf("Result: Fail\n");
}
return 0;
}
Output
// When marks in Phy and Maths are 50 and 50
Phy: 50 Maths: 50 Avg: 50.000000
Result: Pass
// When marks in Phy and Maths are 40 and 40
Phy: 40 Maths: 40 Avg: 40.000000
Result: Fail
// When marks in Phy and Maths are 80 and 40
Phy: 80 Maths: 40 Avg: 60.000000
Result: Pass