#171 – if/else Statement Can Have One or More Statements in Body
December 5, 2010 Leave a comment
The if/else statement can be used to execute a single statement in both the if and the else part, or to execute a block of statements. A block of statements can be executed if curly braces ({, }) are used.
// Example 1 - block of statements
if (age >= 50)
{
Console.WriteLine("Hey, you should consider joining AARP!");
DisplayDetailsOfAARPMembership();
}
else
{
Console.WriteLine("You're still a young pup");
DisplayInfoAboutVideoGames();
}
// Example 2 - single statements
if (airfare < 850.00)
BookTickets();
else
PlanARoadTrip();
// Example 3 - mixed
if (married)
{
KeepPlaceClean();
RaiseKids();
LearnInterpersonalSkills();
}
else
PlayVideoGames();