< C++ .NET CultureInfo Class Info 2 | Main | C++ .NET Loop and Decision 2 >



Decision and Loop Statements:

Repetition & Selection 1



The following are all the topics available in this module.


  1. Making Decisions with the if Statement

  2. Performing One-Way Tests


  1. Performing Two-Way Tests

  2. Performing Multiway Tests

  3. Performing Nested Tests

  4. Making Decisions with the switch Statement

  5. Defining Simple switch Statements

  6. Defining Fall-Through in a switch Statement

  7. Using Fall-Through in a switch Statement

  8. Performing Loops

  9. Using while Loops

  10. Using for Loops

  11. Using do-while Loops

  12. Performing Unconditional Jumps

  13. Quick Reference


Making Decisions with the if Statement


The most common way to make a decision in Visual C++ is to use the if statement. You can use the if statement to perform a one-way test, a two-way test, a multiway test, or a nested test. Let’s consider a simple one-way test first.



Performing One-Way Tests


The following illustration shows a simple one-way test.


C++ .Net loop - illustration that shows a simple one-way test of program flow


The following example shows how to define a one-way test in Visual C++:

if (number < 0)

  Console::WriteLine(L"The number is negative");

Console::WriteLine(L"The end");

The if keyword is followed by a conditional expression enclosed in parentheses. The parentheses are mandatory. If the conditional expression evaluates to true, the next statement is executed, the message "The number is negative" will be displayed. Notice that the message "The end" will always be executed, regardless of the outcome of the test, because it is outside the if body.


There is no semicolon after the closing parenthesis in the if test. One of the most common programming errors in C++ is to put one in by mistake, as shown here:

// Note the spurious semicolon

if (number < 0);

This statement is equivalent to the following statement, which is probably not what you intended:

if (number < 0)

    ;   // Null if-body – do nothing if number < 0

If you want to include more than one statement in the if body, enclose the if body in braces ({}), as follows:

if (number < 0)


    Console::Write(L"The number ");


    Console::WriteLine(L" is negative");


Console::WriteLine(L"The end");

 It’s good practice to enclose the if body in braces, even if it comprises only a single statement. This style is defensive programming, in case you (or another developer) add more statements to the if body in the future.

In this exercise, you will create a new application to perform one-way tests. As this chapter progresses, you will extend the application to use more complex decision-making constructs and to perform loops. For now, the application will ask the user to enter a date, and then it will perform simple validation and display the date in a user-friendly format on the console.

Start Microsoft Visual C++/Studio .NET, and create a new CLR Console Application project. Name the application CalendarAssistant.


Invoking the new C++ .NET project - selecting the CLR Console Application project - C++ .NEt loop


In Solution Explorer, double-click CalendarAssistant.cpp to view the code file.

At the top of the file, immediately under the using namespace System; line, add the following function prototypes.







C++ .Net loop - Adding functions prototypes to the main project file


At the end of the file, after the end of the main() function, implement the GetYear function as follows:

int GetYear()


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

    String ^ input = Console::ReadLine();

    int year = Convert::ToInt32(input);

    return year;


Implement the GetMonth function as shown in the following code. (This is a simplified implementation; later in this module, you will enhance the function to ensure that the user enters a valid month.)

int GetMonth()


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

    String ^ input = Console::ReadLine();

    int month = Convert::ToInt32(input);

    return month;


Implement the GetDay function as follows. You will enhance this function later to ensure that the user enters a valid day in the given year and month.

int GetDay(int year, int month)


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

    String ^ input = Console::ReadLine();

    int day = Convert::ToInt32(input);

    return day;


C++ .Net loop - Implementing the functions declared previously


Implement the DisplayDate function as follows to display the date as three numbers. Later in this module you will enhance this function to display the date in a more user-friendly format.

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


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








C++ .NET lopp - implementing the member function declared previously


Add the following code inside the main() method. This code asks the user to enter a year, month, and day. If the date passes a simplified validation test, the date is displayed on the console. If the date is invalid, it is not displayed at all.

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


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

This if statement combines several tests by using the logical AND operator &&. Logical tests are performed from left to right. Testing stops as soon as the final outcome is known for sure. For example, if the month is 0, there is no point performing the other tests, the date is definitely invalid. This is known as shortcut evaluation.

Build the program, and fix any compiler errors that you might have.

Run the program. Enter valid numbers for the year, month, and day (for example, 2007, 6, and 7). The program displays the messages shown in the following graphic.


C++ .Net programming tutorial - loop console program output





Notice that the program displays the date because it is valid. The message "The end" is also displayed at the end of the program.

Run the program again, but this time, enter an invalid date (for example, 2005, 0, and 31). The program displays the messages shown in the following graphic.


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


Notice that the program doesn’t display the date because the date is invalid. Instead, the program just displays "The end" at the end of the program. You can make the program more user-friendly by displaying an error message if the date is invalid. To do so, use a two-way test.



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



< C++ .NET CultureInfo Class Info 2 | Main | C++ .NET Loop and Decision 2 >