< C++ .NET Loop and Decision 1 | Main | C++ .NET Loop and Decision 3 >


 

 

Decision and Loop Statements:

Repetition & Selection 2

 

 

The following are the topics available in this part.

  1. Performing Two-Way Tests

  2. Performing Multiway Tests

 

 

Performing Two-Way Tests

 

The following illustration shows a simple two-way test.

 

C++ .Net programming - if-else loop console application program output example

 

The following code shows how to define a two-way test for the Calendar Assistant application:

if (month >= 1 && month <= 12 && day >= 1 && day <= 31)

{

    DisplayDate(year, month, day);

}

else

{

    Console::WriteLine(L"Invalid date");

}

Console::WriteLine(L"\nThe end\n");

The else body defines what action to perform if the test condition fails.

 

In this exercise, you will enhance your Calendar Assistant application to display an error message if an invalid date is entered.

Continue working with the project from the previous exercise.

Modify the main() function so that it uses an if-else statement to test for valid or invalid dates.

int main(array<System::String ^> ^args)

{

   Console::WriteLine(L"Welcome to your calendar assistant");

   Console::WriteLine(L"\nPlease enter a date");

   int year = GetYear();

   int month = GetMonth();

   int day = GetDay(year, month);

   // Simplified test for now – assume there are 31 days in

   // every month

   if (month >= 1 && month <= 12 && day >= 1 && day <= 31)

   {

      DisplayDate(year, month, day);

   }

   else

   {

      Console::WriteLine(L"Invalid date");

   }

   Console::WriteLine(L"\nThe end\n");

   return 0;

}

Build and run the program. Enter an invalid date, such as 2005, 0, and 31. The program now displays an error message.

 

C++ .Net programming - the if-else loop console program output example

 

Performing Multiway Tests

 

You can arrange if-else statements in a cascading fashion to achieve multiway decision making. The following illustration shows multiway testing in a flowchart.

 

 

 

 

 

 

 

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

C++ .Net programming - multiple if-else loop flow chart

 

The following code shows how to use a multiway test to determine the maximum number of days (maxDay) in a month:

int maxDay;

 

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

{

   maxDay = 30;

}

else if (month == 2)

{

   maxDay = 28;

}

else

{

   maxDay = 31;

}

If the month is April, June, September, or November, maxDay is set to 30. If the month is February, maxDay is set to 28. (We’ll ignore leap years for now!) If the month is anything else, maxDay is set to 31.

 

There is a space between the keywords else and if because they are distinct keywords. This is unlike Microsoft Visual Basic .NET, which has the single keyword ElseIf.

In this exercise, you will enhance your Calendar Assistant application to display the maximum number of days in the user’s chosen month. Continue working with the project from the previous exercise.

Modify the GetDay function so that it uses an if-else-if statement to determine the maximum allowable number of days.

int GetDay(int year, int month)

{

    int maxDay;

 

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

    {

        maxDay = 30;

    }

    else if (month == 2)

    {

        maxDay = 28;

    }

    else

    {

        maxDay = 31;

    }

    Console::Write(L"Day [1 to ");

    Console::Write(maxDay);

    Console::Write(L"]? ");

    String ^ input = Console::ReadLine();

    int day = Convert::ToInt32(input);

    return day;

}

C++ .Net programming - the if-else program source code

 

Build and run the program. Enter the year 2005 and the month 1. The program prompts you to enter a day between 1 and 31.

 

The multiple if-else loop console program output

 

Enter a valid day, and close the console window when the date is displayed.

Run the program again. Enter the year 2005 and the month 2. The program prompts you to enter a day between 1 and 28.

 

C++ .Net programming - multiple if-else console output sample

 

Enter a valid day, and close the console window when the date is displayed. Don’t worry about the date validation in main(). You will remove it later and replace it with more comprehensive validation in the GetMonth and GetDay functions.

 

 

 

 

 

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


< C++ .NET Loop and Decision 1 | Main | C++ .NET Loop and Decision 3 >