#171 – if/else Statement Can Have One or More Statements in Body

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();
Advertisement

About Sean
Software developer in the Twin Cities area, passionate about software development and sailing.

One Response to #171 – if/else Statement Can Have One or More Statements in Body

  1. stravinsky7 says:

    lol. Loving all your examples. Student here and finding this very useful.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: