Conditional statement
When you write code, you often need to perform different actions for different decisions.
You can use conditional statements in your code to accomplish this task.
if...else statement
Execute a block of code when the condition is true, and execute another block of code when the condition is not true
The elseif statement
is used in conjunction with if...else to execute a code block If... when one of several conditions is true. Else Statement
If you want to execute some code when a certain condition is true and other code when the condition is not true, use the if....else statement.
Syntax
if (condition)
code to be executed if condition is true;
else
code to be executed if condition is false;
Example
If the current date is Friday, the following code will output "Have a nice weekend!", otherwise it will output "Have a nice day!":
If you need to execute multiple lines of code when a condition is true or false, you should enclose the lines of code in curly braces:
ElseIf Statement
If you want to execute code when one of multiple conditions is true, use the elseif statement:
Syntax
Example
If the current date is Friday, the following example will output "Have a nice weekend!", if it is Sunday, it will output "Have a nice Sunday!", otherwise it will output "Have a nice day!" ":