Conditional Statements – I
What are Conditional Statements ?
The name itself defines the function. Conditional statements check the given condition whether it is True or False and gives the output respectively. This allows a one-way, two-way, and multiway selection. Conditional statements are of five types.
Types of conditional statements:
1. if statement
2. if-else statement
3.nested if/ ladder if statement
4. switch statement
What is an if statement ?
This check whether the given the condition is True or False. If true the if block is executed, and if false if block is not executed. This makes a one-way seletion that is the body to be executed or not.
Syntax:
if(condition)
{
// if body- set of codes to be executed
}
What is an if-else statement ?
This is used to check whether the given condition is True or False. If true if block is executed, and if false else body is executed. This makes a two-way selection that is between two alternative functions.
Syntax:
if(condition)
{
// if body- set of codes to be executed
}
else
{
// else body- set of codes to be executed
}
Note: else body is not mandatory. It is syntactically correct if else body is not given.