What is if statement in C
If statement in C is a most powerful, decision-making statement and is used to control the flow of execution of the statement. In which statements are executed if the test expression (condition) is true. When the condition is false, there is no option to go within this structure; in such a situation, control must get out from the structure, and statements outside this structure will be executed.
In this control statement first if statement is evaluated if it is true then the statement will execute. Otherwise, program control will be out of the decision-making.
Syntax of if statement in C
| if(condition) { Statement; } Braces are optional for only one statement as shown below. if(condition) statement; |
Flow chart of if statement in C
How does if statement work
- In if statement the condition is evaluated.
- If the condition becomes true then the statement along if will be executed. if statement is an excepted result or true result
- If the condition becomes false then program control will be out of the decision-making area i.e. if statement.
Example of if statement in C
C program to check whether the given number is positive
| #include<stdio.h> #include<conio.h> void main() { int n; printf("\n Enter a number"); scanf("%d",&n); if(n>0) { printf("\n %d is positive number",n); } getch(); } |
Here in this example, at the first user has to give a number. And the condition is checked here if(n>0), this condition is evaluated because the positive number always be greater than 0. If the condition is true then printf("\n %d is positive number",n); is execute.
Here user gives 5 and the program checks the condition as 5>0 which is true and printf("\n %d is positive number",n); part display 5 is a positive number.
If any users give any negative number. In the output, there is nothing show.
Let check another example
C program to print whether the given number is divisible by 5
| #include<stdio.h> #include<conio.h> void main() { int n; printf("\n Enter a number"); scanf("%d",&n); if(n%5==0) { printf("%d is exactly divisible by 5",n); } getch(); } |
For if-else statement
For else if the ladder
For nested if-else statement
For switch case statement




0 Comments
Please do not enter any spam link on comment box