Control Statements, Conditional Operator
Control Statements, Conditional Operator
If (condition)
{
Statements;
}else{
Statements;
}
if(condition)
{
// Statements to execute if
// condition is true
}
C If else statement
● We can use the else statement with if statement to execute a block of code when the condition is false.
Syntax:
if (condition)
{
// Executes this block if
// condition is true
}
else
{
// Executes this block if
// condition is false
}
1. #include <stdio.h>
2. int main()
3. {
4. int age;
5. printf("Enter your age?");
6. scanf("%d",&age);
7. if(age>=18)
8. {
9. printf("You are eligible to
vote...");
10. }
11. else
12. {
13. printf("Sorry ... you can't
vote");
14. }
15. }
C Nested If..else statement
When an if else statement is present inside the body of another “if” or “else” then this is
called nested if else.
if(condition) {
//Nested if else inside the
body of "if"
if(condition2) {
//Statements inside the
body of nested "if"
}
else {
//Statements inside the
body of nested "else"
}
}
else {
//Statements inside the body
of "else"
}
C – else..if statement /if-else-if ladder
The else..if statement is useful when you need to check multiple conditions within the
program.
● As soon as one of the conditions controlling the if is true, the statement associated with that if is
executed, and the rest of the C else-if ladder is bypassed.
● If none of the conditions are true, then the final else statement will be executed.
Syntax:
if (condition)
statement;
else if (condition)
statement;
.
.
else
statement;
Conditional Operator
conditional statements are the decision-making statements which
depends upon the output of the expression.
Another form:
variable = condition ? value1: value2