Decision-making statements are those statements which allow the programmers to decide which statement should run in different conditions. There are four ways to achieve this:
IF Statement:
This type of statements simply checks the condition and if it is true the statements within it is executed but if it in is not then the statements are simply ignored in the code.
Syntax:
eg:-
void
main()
{
int
a = 10;
// Condition is true
if
(a > 3) {
// This will be printed
print("Condition is true");
}
}
Output:
Condition is true
IF…else Statement:
This type of statement simply checks the condition and if it is true, the statements within is executed but if not then else statements are executed.
eg:-
void
main()
{
int
a = 10;
// Condition is false
if
(a > 30) {
// This will not be printed
print("Condition is true");
}
else
{
// This will be printed
print("Condition id false");
}
}
output:
Condition id false
ELSE…IF Ladder:
This type of statement simply checks the condition and if it is true the statements within it is executed but if it in is not then other if conditions are checked, if they are true then they are executed and if not then the other if conditions are checked. This process is continued until the ladder is completed.
eg:-
void
main()
{
int
a = 10;
if
(a < 9) {
print("Condition 1 is true");
a++;
}
else
if
(a < 10) {
print("Condition 2 is true");
}
else
if
(a >= 10) {
print("Condition 3 is true");
}
else
if
(++a > 11) {
print("Condition 4 is true");
}
else
{
print("All the conditions are false");
}
}
output:
Condition 3 is true
Nested IF Statement:
This type of statements checks the condition and if it is true then the if statement inside it checks its condition and if it is true then the statements are executed otherwise else statement is executed.
eg:-
void
main()
{
int
a = 10;
if
(a > 9) {
a++;
if
(a < 10) {
print("Condition 2 is true");
}
else
{
print("All the conditions are false");
}
}
}
Output:
All the conditions are false