1 | initial version |
An if-else statement can be used to assess an action by evaluating a condition and executing a specific set of instructions based on the outcome of the condition. For example, let's consider a scenario where a user is attempting to log into a website:
if (username == correctUsername && password == correctPassword) {
// Login successful
redirectUserToDashboard();
} else {
// Login failed
displayErrorMessage();
}
In the above code, if the condition (username == correctUsername && password == correctPassword)
is true, the code contained within the if block will execute, which will redirect the user to the dashboard. However, if the condition evaluates to false, the code contained within the else block will execute, which will display an error message to the user.
This demonstrates how an if-else statement can be used to assess an action and execute different sets of instructions depending on the outcome of the evaluation.