It is possible to ask a program to execute a specific section of code only if an if statement is considered false. For this, we use the else key word.
if(statement)
{
// Code to execute if the statement is true.
}
else
{
// Code to execute if the statement is false.
}
Both code sections will never be executed together. The first section (the if one) is only executed if the statement is true, while the section section (the else one) is only executed if the statement is false.
It is also possible to ask, if a statement has not been verified, to verify another one. For this, we use the else if key words. This statement work the exact same way as a regular if statement, except that the test is only executed if the previous statement is considered false.
if(statement)
{
// Code to execute if the statement is true.
}
else if(another_statement)
{
// Code to execute if the second statement is true.
}
The same way as before, both code will never be executed together. If the first statement is true, the second test will simply be skipped, and the first section of code will be executed. If the first statement is false, the second statement is verified, and the second section is executed only if this statement is true.
It is possible to add as many else if sections one after another as needed to test different statements. It is also possible to add an else section at the end of all the else if sections that will be executed only if all the statements are false.
if(statement)
{
// Code to execute if the statement is true.
}
else if(second_statement)
{
// Code to execute if the second statement is true.
}
else if(third_statement)
{
// Code to execute if the third statement is true.
}
else
{
// Code to execute if none of the three above statements are true.
}
Only one code section will be executed. At the moment one statement is verified, all the following sections are skipped and won't be executed.