< C++ .NET Loop and Decision 2 | Main | C++ .NET Loop and Decision 4 >


 

 

Decision and Loop Statements:

Repetition & Selection 3

 

 

The following are the topics available in this part.

  1. Performing Nested Tests

  2. Making Decisions with the switch Statement

  3. Defining Simple switch Statements

 

 

Performing Nested Tests

 

You can perform nested tests, as shown in the following illustration.

 

C++ .Net nested if-else loop flow chart

 

 

The following code shows how to use nested tests to process leap years correctly in the Calendar Assistant application:

int maxDay;

 

if (month == 4 || month == 6 || month == 9 || month == 11)

{

    maxDay = 30;

}

else if (month == 2)

{

    bool isLeapYear = (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);

    if (isLeapYear)

    {

        maxDay = 29;

    }

    else

    {

        maxDay = 28;

    }

}

else

{

    maxDay = 31;

}

If the month is February, we define a bool variable to determine if the year is a leap year. A year is a leap year if it is evenly divisible by 4 but not evenly divisible by 100 (except years that are evenly divisible by 400, which are leap years). The following table shows some examples of leap years and non-leap years.

 

Year

Leap Year?

1996

Yes

1997

No

1900

No

2000

Yes

 

Table 1

 

We then use a nested if statement to test the bool variable isLeapYear so that we can assign an appropriate value to maxDay.

There is no explicit test in the nested if statement. The condition if (isLeapYear) is equivalent to if (isLeapYear != false).

In this exercise, you will enhance your Calendar Assistant application to deal correctly with leap years.

Continue working with the project from the previous exercise.

Modify the GetDay function to match the block of code just described to test for leap years.

int GetDay(int year, int month)

{

   int maxDay;

 

   if (month == 4 || month == 6 || month == 9 || month == 11)

   {

       maxDay = 30;

   }

   else if (month == 2)

   {

       bool isLeapYear = (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);

       if (isLeapYear)

       {

          maxDay = 29;

       }

       else

       {

          maxDay = 28;

       }

   }

   else

   {

     maxDay = 31;

   }

   return maxDay;

}

Build and run the program. Enter the year 1996 and the month 2. The program prompts you to enter a day between 1 and 29. Enter a valid day, and close the console window when the date is displayed.

 

C++ .Net programming - the nested if-else console application output example

 

Run the program again. Enter the year 1997 and the month 2. Verify that the program prompts you to enter a day between 1 and 28.

 

The nested if-else console application output

 

Run the program several more times using the test data from the previous table.

 

 

 

 

 

 

---------------------------------------------------

Another nested if-else program output console application example

 

Making Decisions with the switch Statement

 

Now that you have seen how the if statement works, let’s take a look at the switch statement. The switch statement enables you to test a single variable and to execute one of several branches depending on its value.

 

Defining Simple switch Statements

 

The following illustration shows how the switch statement works.

 

The C++ .Net - the switch-case-break statement flowchart

 

The following example shows the syntax for the switch statement. The switch statement tests the numberOfSides in a shape and displays a message to describe that shape:

// Number of sides in a shape

int numberOfSides;

// ...

switch (numberOfSides)

{

    case 3:  Console::Write(L"Triangle");      break;

    case 4:  Console::Write(L"Quadrilateral"); break;

    case 5:  Console::Write(L"Pentagon");      break;

    case 6:  Console::Write(L"Hexagon");       break;

    case 7:  Console::Write(L"Septagon");      break;

    case 8:  Console::Write(L"Octagon");       break;

    case 9:  Console::Write(L"Nonagon");       break;

    case 10: Console::Write(L"Decagon");       break;

    default: Console::Write(L"Polygon");       break;

}

The switch keyword is followed by an expression in parentheses. The expression must evaluate to an integer-based value, a character, or an enumeration value. Next a series of case branches are defined within braces. Each case label specifies a single constant number. You can’t specify multiple values, and you can’t define a range of values. Each case branch can contain any number of statements. At the end of each branch, use a break statement to exit the switch statement.

There is normally no need to use braces within each case branch. The break statement marks the end of each case branch. However, you’ll need to use braces if you need to declare a variable within the branch code. You can define an optional default branch in the switch statement. The default branch will be executed if the expression doesn’t match any of the case labels. It’s good practice to define a default branch even if you don’t have any specific processing to perform. Including the default branch shows that you haven’t just forgotten it. Also, the default branch can help you trap unexpected values and display a suitable warning to the user.

In this exercise, you will enhance your Calendar Assistant application to display the month as a string such as January, February, and so on.

Continue working with the project from the previous exercise.

Modify the DisplayDate function. Rather than display the month as an integer, use a switch statement to display the month as a string:

void DisplayDate(int year, int month, int day)

{

    Console::WriteLine(L"\nThis is the date you entered:");

    Console::Write(year);

    Console::Write(L"-");

    Console::Write(month);

    Console::Write(L"-");

    Console::Write(day);

    Console::WriteLine();

 

      switch (month)

      {

            case 1:  Console::Write(L"January");   break;

            case 2:  Console::Write(L"February");  break;

            case 3:  Console::Write(L"March");     break;

            case 4:  Console::Write(L"April");     break;

            case 5:  Console::Write(L"May");       break;

            case 6:  Console::Write(L"June");      break;

            case 7:  Console::Write(L"July");      break;

            case 8:  Console::Write(L"August");    break;

            case 9:  Console::Write(L"September"); break;

            case 10: Console::Write(L"October");   break;

            case 11: Console::Write(L"November");  break;

            case 12: Console::Write(L"December");  break;

            default: Console::Write(L"Unknown");   break;

      }

}

Build the program. Run the program several times, and enter a different month each time. Verify that the program displays the correct month name each time. A sample program outputs are shown below.

 

C++ .NEt programming - the switch-case-break program output example

--------------------------------------------------

 

 

 

 

 

 

--------------------------------------------------

C++ .Net programming - The switch-case-break-default console application output example

 

 

 

 

 

Part 1 | Part 2 | Part 3 | Part 4 | Part 5


< C++ .NET Loop and Decision 2 | Main | C++ .NET Loop and Decision 4 >