C if-else statement
Contents
If-else Statement in C Programming
We can use if-else statement in c programming so that we can check any condition and depending on the outcome of the condition we can follow appropriate path. We have true path as well as false path.
Syntax :
if(expression) { statement1; statement2; } else { statement1; statement2; } next_statement;
Explanation :
- If expression is True then Statement1 and Statement2 are executed
- Otherwise Statement3 and Statement4 are executed.
Sample Program on if-else Statement :
void main() { int marks=50; if(marks>=40) { printf("Student is Pass"); } else { printf("Student is Fail"); } }
Output :
Student is Pass
Flowchart : If Else Statement
Consider Example 1 with Explanation :
Consider Following Example -
int num = 20; if(num == 20) { printf("True Block"); } else { printf("False Block"); }
If part Executed if Condition Statement is True.
if(num == 20) { printf("True Block"); }
True Block will be executed if condition is True.
Else Part executed if Condition Statement is False.
else { printf("False Block"); }
Consider Example 2 with Explanation :
More than One Conditions can be Written inside If statement.
int num1 = 20; int num2 = 40; if(num1 == 20 && num2 == 40) { printf("True Block"); }
Opening and Closing Braces are required only when “Code” after if statement occupies multiple lines. Code will be executed if condition statement is True. Non-Zero Number Inside if means “TRUE Condition”
If-Else Statement :
if(conditional) { //True code } else { //False code }
Note :
Consider Following Example -
int num = 20; if(num == 20) { printf("True Block"); } else { printf("False Block"); }
- If part Executed if Condition Statement is True.
- Else Part executed if Condition Statement is False.
- More than One Conditions can be Written inside If statement.
- Opening and Closing Braces are required only when “Code” after if statement occupies multiple lines.
- Code will be executed if condition statement is True.
- Non-Zero Number Inside if means “TRUE Condition”
if(num == 20) { printf("True Block"); }
True Block will be executed if condition is True.
else { printf("False Block"); }
int num1 = 20; int num2 = 40; if(num1 == 20 && num2 == 40) { printf("True Block"); }