if...else Statement
You can enhance the decision making process by providing an alternative choice through adding an else statement to the if statement. The if...else statement allows you to execute one block of code if the specified condition is evaluated to true and another block of code, if it is, evaluated to false. It can be written, like this:
if (condition) {
code to be executed if condition is true;
} else {
code to be executed if condition is false;
}
The example below will output "Have a good day!" if the current time is less than 20, and "Have a good night!" otherwise:
The if...elseif...else a special statement that is used to combine multiple if...else statements.
if(condition){
// Code to be executed if condition is true
} elseif(condition){
// Code to be executed if condition is true
} else{
// Code to be executed if condition is false
}
The ternary operator provides a shorthand way of writing the if...else statements. The ternary operator is represented by the question mark (?) symbol and it takes three operands: a condition to check, a result for ture, and a result for false.
© 2024 Easy To Learning. All Rights Reserved | Design by Easy To Learning