What is if statement in c?
If statement in c programming is a decision making a statement. It allows the programmer to code the program in such a way that the execution of those sets of codes if the specified condition is true.
Generally in our daily life we come across many decisions like for example.
- If it is raining outside i will take umbrella with me.
- If highway is busy i will take direction.
- If bus is full i will go by train.
like this we come across some decisions.
C also provides some features to take decisions for programmers.
The simplest decision making statement is if statement in c programming.
If statement in c programming is an decision making statement.
In order to understand first let us look at it’s general form
if(condition is true)
execute the statement;
In the above general form “if” is a keyword i.e. it is a reserved word. It tells the compiler that if the condition is true then execute the statement. Otherwise, skip the statement.
The statement may be one or more. If there are many statements then we write it in curly brackets.
In the above if statement in c condition is represented using relational operators.
following are some of the relational operators
Expression | Meaning |
a==b | a equal to b. |
a!=b | a not equal to b. |
a<b | a less than b. |
a>b | a greater than b. |
a<=b | a less than or equal to b. |
a>=b | a greater than or equal to b. |
In order to understand the if statement in c let us take one simple example
if (grade > 75)
printf(" you have secured distinction \n");
In the above example, we are taking a decision that if the student scores more than 75% then we print that he secured distinction. otherwise, we skip the statement.
The condition may be arithmetic expression also.
let us look at the example
if( a%2 == 0)
printf(" a is an even number \n");
if( a+b == 10)
printf(" condition is met");
if(age >=60 || age <=18)
printf(" you are not eligible for work \n");
In the first example, we are calculating that whether the variable ” a” is even or not by dividing it by 2 and the result is zero.
In the second example we are adding two variables “a” and “b” and if it is 10 then we met some condition.
In the third example, we are taking a decision that if the person’s age crosses 60 or under 18 then he/she is not eligible for work.
let us look at some more examples on multiple statements within block of if statement.
if statement in c example
Example 1
#include <stdio.h>
#include<stdlib.h>
int main()
{
int b=1000,age;
printf("\n\n\tEnter your age \n\t");
scanf("%d",&age);
if(age >= 50)
{
printf("\n\tYou are a senior citizen \n");
printf("\tYour promotion bonus is %d \n\n",b);
exit(0);
}
if(age <= 50)
{
printf("\n\tYou are not a senior citizen \n");
printf("\tYou have no promotion bonus \n\n");
}
return 0;
}
Explanation
In the above example, we have a block of if statement i.e. we have three statements in if block. where if a condition such as age is greater than 50 is met then we print that he/she is a senior citizen and he/she will get a promotion.
Example 2
#include <stdio.h>
#include<stdlib.h>
int main()
{
int b,number;
printf("\n\n\tEnter a number \n\t");
scanf("%d",&number);
if( (number % 2) == 0 )
{
printf("\n\t%d is an even number\n\n", number );
}
if( (number % 2) == 1 )
{
printf("\n\t%d is an odd number\n\n", number );
}
return 0;
}
Explanation
In example 2 we are reading a number using scanf in c.
Then we are performing odd or even number check using if statement in c.
If you want to know more about odd or even number checking read this article.
Odd or even in C | C++ and Java.
Difference between simple if and if-else statements
Before reading difference first let us learn about if-else statement
If-else statement executes block of statements or a single statement depending on a condition.
In if-else statement we execute true block of statements and false block of statements depending on a condition .
let us look at it’s general form
//if else general form
if (condition met)
{
true block ;
}
else
{
false block;
}
In the above general form if the condition in the if statement becomes true then we execute the true block statement. else we execute a false block statement.
let us take one example
if (grade >75)
{
printf("you secured distinction \n");
printf("congrates \n");
}
else
printf(" you have not secured distinction \n");
In the above example if the grade is more than 75 then we execute a true block statement i.e. we print statements such as “you secured distinction”, “congrats”. otherwise, we execute a false block statement i.e. statement followed by else keyword.
Now we look at the “Difference between simple if and if-else statements”.
If statement in c executes only true block statement. If-else executes either true block or false block statement.
All the statements in if block is executed if the condition is met otherwise no statements will be executed. In if-else any one block is executed.
In if-statement if is a keyword. In if-else if and else are keywords.
If is a simple form of conditional statement. if-else is a extended form of if statement.
General forms are as follows
//if block
if(condition is true)
execute the statement;
//if else block
if (condition met)
{
true block ;
}
else
{
false block;
}
if statement in c | if-else statement in c |
Executes only true block statement. | Executes either true block or false block statement. |
if(condition is true) | if (condition met) |
All the statements in if block is executed if condition is met otherwise no statements will be executed. | Any one block is executed. |
If is a keyword. | If, else are keywords. |
Simplest form. | extended if statement. |
Difference between if and nested if-else statements
Before reading difference first let us learn about nested if-else statement in c.
We can write multiple conditions within if and if-else statements. That is called as nesting.
General form is as follows
if (condition met)
{
true block;
}
else
{
if(condition met)
{
true block;
}
else
{
false block;
}
other statements;
}
In the above form we come across another if-else statement within else block. That is called nesting.
Let us take one example
if(a>b)
{
if(a>c)
{
printf(" a is graeter \n");
}
else
{
printf("c is graeter \n");
}
}
else
{
printf("b is greater \n");
}
In the above example, we are comparing three values a,b,c. if a is greater than b then we check if a>c. If both conditions are met then c is greater. If only one condition is met then else block i.e. c is greater is executed. else b is greater is executed.
Now let us look at the difference
If statement in c executes only true block statement. Nested if-else statement executes multiple statements.
All the statements in if block are executed if the condition is met otherwise no statements will be executed. In a nested if-else statement any one block is executed.
In if statement single condition is checked . In nested if-else statement multiple conditions are checked.
General forms are as follows
//if statement
if(condition is true)
execute the statement;
//nested if else statements
if (condition met)
{
if (condition met)
{
true block;
}
else
{
false block;
}
}
else
{
false block;
}
if statement in c | nested if-else statement in c |
Executes only true block statement. | Executes multiple statements. |
if(condition is true) | if (condition met) |
All the statements in if block is executed if condition is met otherwise no statements will be executed. | In nested if-else statement any one block is executed. |
Single condition is checked. | Multiple conditions are checked. |
Difference between if and switch statement
Before reading difference first let us learn about switch statement.
More than one conditions are checked using switch statement. It is the extended form of nested if-else statement.
Here it checks for an integer expression. Then it compares all the case statements within the switch statement. If it matches with any one case then that case statement will be executed. If none is met then the default statement is executed.
General form
switch( integer expression)
{
case constant 1: block1;
case constant 2: block2;
.
.
default:
default-block;
}
Let us look at one example
switch(grade)
{
case 30: {
printf("you have failed \n");
break;
}
case 50:
{
printf("you have secured first class \n");
break;
}
case 75:
{
printf("you have secured distinction \n");
break;
}
default:
exit(0);
}
In the above example grade is an integer value. Its value is compared with case values. If it matches any one case statement then the matched case block is executed. If none of the case statements are met then the default block is executed.
Now let us look at the differences
If statement in c executes only true block statement. In switch the matched case block is executed.
All the statements in if block are executed if the condition is met otherwise no statements will be executed. In switch, any of the matched case blocks is executed otherwise default statement is executed.
In if statement single condition is checked. In switch multiple conditions are checked.
In if statement if is a keyword. In switch statement switch,case,default are keywords.
General forms are as follows
if(condition is true)
execute the statement;
switch( integer expression)
{
case constant 1: block1;
case constant 2: block2;
.
.
default:
default-block;
}
if statement in c | switch statement in c |
Executes only true block statement. | The matched case block is executed. |
if(condition is true) | switch( integer expression) |
All the statements in if block is executed if condition is met otherwise no statements will be executed. | Any of the matched case block is executed otherwise default statement is executed. |
Single condition is checked . | Multiple conditions are checked. |
What is if statement in C?
If statement in c programming is a decision-making statement. It allows executing a set of code if the specified condition is true.
How do you write an if statement in C?
We can write if statement using keyword specified by a condition, then we can write one or more line of codes. If there many lines of code then write inside braces. {}
.
What is if and if else statement?
If statement in c allows executing a set of code if the specified condition is true. If-else executes either true block or false block statement.
What is an if statement give two examples?
If statement in c programming is a decision-making statement which allows executing set of code if the specified condition is true. Check the above examples.
People also read
- C program Structure
- Structures in c
- Arrays in c
- string in c
- printf in c
- Conditional (computer programming)
- scanf in c