When we need to execute a group of statements only when a given condition is true then we use if statement.
If statement allows us to select an action based on some condition. It gives programmer to take control over a piece of code. Programmer can control the execution of code based on some condition or user input. For example: if a user inputs valid user name and password to login to his laptop, then will be able to login else not.
- How if statements works?
- Flow chart of simple if
- Example 1: simple if
- Multiple if statements
Quick links
Boolean Expression:
If statement perform action based on Boolean expression either true or false.
A C expression that evaluates either true or false is known as Boolean expression. However, in C programming there is no concept of true or false value.
In C we represent true with a non-zero integer and false with zero. Hence, if an expression evaluates to integer is considered as Boolean expression.
Syntax of simple if statement:
if (condition)
{
//Group of C statements.
//These statements will only execute if the condition is true.
}
How if statement works?
- The if statement check the given condition inside the parenthesis().
- If the condition returns true, statements inside the body of ''if'' are executed. otherwise, the condition returns false then the statements inside “if” are skipped.
For Example:
Simple if | welcome2protec.com |
Note: Relational and logical operators are used to compare things and take action based on the comparison.
Flow chart of If statement:
Flow chart of simple if | welcome2protec.com |
Example 1: Simple if statement
// Program to display a number if it is negative
#include <stdio.h>
int main() {
int number;
printf("Enter an integer: ");
scanf("%d", &number);
// if number is less than 0 then number is negative
if (number < 0) {
printf("You entered %d, i.e. a negative number.\n", number);
}
printf("You entered %d, i.e. a positive number.\n", number);
return 0;
}
Output 1:
Enter an integer: -2
You entered -2, i.e. a negative number.
In output-1, when the user enter -2, then the condition if (number < 0) evaluated to true. Hence, you entered -2 is displayed on the screen.
Output 2:
Enter an integer: 5
You entered 5, i.e. a positive number.
In output-2, when the user enter 5, then the condition if (number < 0) evaluated to false. Then the statement inside if is skipped, and will display whatever statement written outside of if.
Try some more exercises on if statement.
- Practice exercises - If else programming exercises in C.
Multiple if statement:
We can use multiple if statements to check more than one conditions. Let's have a look at the below example.
Example 2: Multiple if statement
/* Program to find the largest number among the three. */
#include <stdio.h>
int main()
{
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");
}
printf("End of program");
return 0;
}
Output
/*The output depends on the user input.*/
Enter three numbers: 10
20
30
30 is largest.
- Control statement in C
- If...else and Ladder if...else
- C Nested if...else
0 Comments:
Post a Comment
Please don't enter any spam link in the comment box.