If/Else Statement:
The code between the curly brackets executes only if it meets the value of the boolean expression:
if(expression)
{
statement1
}
else
{
statement2
}
If expression is true, execute statement1, else if it is false, execute statement2. The following is a much more realistic example:
string sentence = "";
if(name == "Stephanie")
{
sentence = "Hello " + name;
}
else
{
sentence = "Hello there";
}
An improvement over this if/else statement is the nested if/else. This means that when the expression is executed, the next step is to find another expression to fullfill. Example:
string sentence = "";
if(name == "Stephanie")
{
if(surname == "Grima")
sentence = "Hello " + name + " " + surname;
else
sentence = "Hello " + name;
}
else
{
sentence = "Hello there";
}
In the example above, we first check if the user's name is Stephanie, and if it is, we check whether her surname is Grima or not. If it is, her full name is saved.