The if statement:
If the condition is TRUE only then it will execute otherwise it will not execute.
It is the simplest form of selection:
If the condition is TRUE only then it will execute otherwise it will not execute.
It is the simplest form of selection:
if (condition)
{
statement-block.
}
if keyword is a hint to compiler about condition. Now if the condition in parentheses evaluates to true, the set of statement following the if statement will be executed, otherwise not. The condition is an expression which evaluates to true or false. Remember, In C everything other than 0 is true, and 0 is false.
For example:
#include <stdio.h>int main(){if(35){printf("35 is evaluated as true in C language, hence this line will be printed.\n");}if(0){printf("0 is evaluated as false in C, so this line will not be executed.\n");}return 0;}
If you run above program you will get the output:
The first if statement will execute in above program as its condition evaluates to true. Whereas in second if the condition is false due to 0.
We will not pass the values to if statement like this, so here the relational and logical operators come into picture. We use the relational and logical operators generally to form the conditions of if statement.
For example, following are some conditions using relational operators:
#include <stdio.h>
int main()
{
int x = 50, y = 49;
if(x < y)
{
printf("X is less than y.\n");
}
if(x > y)
{
printf("X is greater than y.\n");
}
if(x == y)
{
printf("X is equal to y.\n");
}
if(x != y)
{
printf("X is not equal to y.\n");
}
return 0;
}
int main()
{
int x = 50, y = 49;
if(x < y)
{
printf("X is less than y.\n");
}
if(x > y)
{
printf("X is greater than y.\n");
}
if(x == y)
{
printf("X is equal to y.\n");
}
if(x != y)
{
printf("X is not equal to y.\n");
}
return 0;
}
Output:
If the if block has only one statement to execute, we can put the curly braces around it. For example:-
if(x > y)
printf("X is greater than y.\n");
printf("X is greater than y.\n");
But its good to put braces as its a good practice to reduce errors in future if you want to add more line to your code.
Also, while using relational operator, you need to be careful for = and == operators. As = means the assignment operator whereas == is relational operator to check the equality. Assignment operator will assign a value to some variable and return the assigned value, so if you accidentally use the = operator in if condition, there are no errors instead that is a valid program.
For example:-
#include <stdio.h>int main(){int x = 20, y = 10;if(x = 25){y = 15;}printf("x = %d y = %d\n",x,y); // 25 and 15 not 20 and 10return 0;}
This above program will print x = 25 and y = 15.
The above program will not check x with 25, instead it will assign 25 to x, and then return 25, which is treated as true in C, hence y will assigned 15. whereas if you replace the if statement with,
if(x == 25)
Now the program will print 20 and 10 instead of 25 and 15. So == should be used with caution.
The if-else statement:
In this statement if the condition is TRUE then if part will be executed and if the condition is FALSE then the false part / else part will be executed.
In if block we have the statement which will be executed based on the condition and to specify an alternative statement, we use else block.
In this statement if the condition is TRUE then if part will be executed and if the condition is FALSE then the false part / else part will be executed.
In if block we have the statement which will be executed based on the condition and to specify an alternative statement, we use else block.
Syntax:
if(condition){// statements to be executed if condition is TRUE.// commonly referred as true block.}else{// statements to be executed if condition is FALSE.// commonly referred as false block.}
Program:
#include <stdio.h>int main(){int marks;marks=55; // change this value to see different outputif(marks >= 40){printf("PASS");}else{printf("FAIL, Study hard.");}return 0;}
As you can see above else part is written below if block. So if you want to execute some set of statements then use only if statement but if you need an alternative action to be executed then use the else block.
The nested-if statement:
Nested-if means that you can use one if or else-if statement inside another if or else-if statements.
Nested-if means that you can use one if or else-if statement inside another if or else-if statements.
For example:
#include<stdio.h>
int main()
{
int marks;
marks = 60;
if(marks >= 40)
{
if(marks < 60)
{
printf("You are pass.");
}
else
{
printf("You got 1st division.");
}
}
else
{
printf("You are Fail, Study hard.");
}
return 0;
}
Output:
As you can see second if is inside the first if. So, we can nest it upto any number according to the situation.
if-else statement:
Nesting can be done for if-else statements also. One if-else block can be completely inside other if block or else block, which indeed is a part of if block. This is popularly known as else-if ladder or if-else ladder.
Nesting can be done for if-else statements also. One if-else block can be completely inside other if block or else block, which indeed is a part of if block. This is popularly known as else-if ladder or if-else ladder.
Syntax:
if (condition1)//do this thingelseif(condition2)// do this thingelseif(condition3)// do this thingelse// do this thing.
Program:
#include <stdio.h>int main(){int marks;marks = 60;if(marks >= 90)printf("A+");else if(marks >= 80)printf("A");else if(marks >= 70)printf("B");else if(marks >= 50)printf("C");else if(marks >= 40)printf("D");elseprintf("F");return 0;}
Switch Statement:
In switch statement the value of expression is tested against the value, one after the another. Once the match is found the statement associated with that case is executed until the break statement or end of the switch statement is reached. If no match found then the default statement is executed.
In switch statement the value of expression is tested against the value, one after the another. Once the match is found the statement associated with that case is executed until the break statement or end of the switch statement is reached. If no match found then the default statement is executed.
General Form:
switch (expression){case constant1:statement sequencebreak;case constant2:statement sequencebreak;case constant3:statement sequencebreak;...default:statement sequence}
Program:
#include <stdio.h>
int main()
{
int day = 4;
switch (day)
{
case 1:
printf("Monday. \n");
break;
case 2:
printf("Tuesday. \n");
break;
case 3:
printf("Wednesday. \n");
break;
case 4:
printf("Thursday. \n");
break;
case 5:
printf("Friday. \n");
break;
case 6:
printf("Saturday. \n");
break;
case 7:
printf("Sunday. \n");
break;
default:
printf("Wrong number entered. \n");
}
return 0;
}
The above program will print the Thursday.
The break statement is one of C's jump statements.
Tags:
C language