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

About Sean
Software developer in the Twin Cities area, passionate about .NET technologies. Equally passionate about my own personal projects related to family history and preservation of family stories and photos.

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

Follow

Get every new post delivered to your Inbox.

Join 43 other followers