if Statement and if else Statement in C
Notes:
Conditional Statements in C programming language:
- execute set of statements based on the result of a given conditional expression
- if
- if else
- nested if else
- else if ladder
- switch case
if statement:
syntax:
if(conditional expression) {
statements; // true part or true block
}
- Computer executes true part; if the conditional expression inside the parenthesis evaluates to true else control is transferred to the first executable statement written outside the true block.
if else statement:
syntax:
if(conditional expression) {
statements; // true part or true block
}
else {
statements; // false part or false block
}
- Computer executes true part; if the conditional expression inside the parenthesis evaluates to true, else it executes the false part.
Note:
- if true part is executed then the false part is not executed, vice versa.
- pair of flower brackets can be left out if there is only one statement in the block
Example Code:
#include <stdio.h>
int main()
{
int a = 10;
if(a==10)
printf("a is equal to 10\n");
else
printf("a is not equal to 10\n");
return 0;
}