Conditional Statement in C
Conditional Statement in C
You have already learned that C supports the usual logical conditions from
mathematics:
If statement Syntax:
if (condition) {
// block of code to be executed if the condition is true
}
Example: Write a c program to print the greater value among two entered numbers.
Assume int a = 20 and int b = 16;
If else statement Syntax:
Use the else statement to specify a block of code to be executed if the condition is false.
if (condition) {
// block of code to be executed if the condition is true
} else {
// block of code to be executed if the condition is false
}
Use the else if statement to specify a new condition if the first condition is false.
if (condition1){
// block of code to be executed if condition1 is true
} else if (condition2){
// block of code to be executed if the condition1 is false and
condition2 is true
} else {
// block of code to be executed if the condition1 is false and
condition2 is false
}
switch(expression) {
case x:
// code block
break;
case y:
// code block
break;
default:
// code block
}
This is how it works:
This will stop the execution of more code and case testing inside the block.
The default keyword specifies some code to run if there is no case match.