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



Decision and Loop Statements:

Repetition & Selection 4



The following are the topics available in this part.

  1. Defining Fall-Through in a switch Statement

  2. Using Fall-Through in a switch Statement

  3. Performing Loops

  4. Using while Loops



Defining Fall-Through in a switch Statement


If you omit the break statement at the end of a case branch, flow of control continues on to the next statement. This process is called fall-through. The following example illustrates why fall-through might be useful. This example tests a lowercase letter to see if it is a vowel or a consonant:

// Single lowercase letter, for example 'a'

char lowercaseLetter;


switch (lowercaseLetter)


    case 'a':

    case 'e':

    case 'i':

    case 'o':

    case 'u':  Console::Write(L"Vowel"); break;


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


There is no break statement in the first four case labels. Flow of control passes on to the next executable statement to display the message Vowel. The default branch deals with all the other letters and displays the message Consonant.


Using Fall-Through in a switch Statement


In this exercise, you will enhance your Calendar Assistant application to display the season for the user’s date.

Continue working with the project from the previous exercise.

Modify the DisplayDate function. After displaying the year, month, and day, add the following code to display the season:

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


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








  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;



  switch (month)


    case 12:

    case 1:

    case 2:  Console::WriteLine(L" [Winter]"); break;

    case 3:

    case 4:

    case 5:  Console::WriteLine(L" [Spring]"); break;

    case 6:

    case 7:

    case 8:  Console::WriteLine(L" [Summer]"); break;

    case 9:

    case 10:

    case 11: Console::WriteLine(L" [Fall]"); break;



Build the program.

Run the program several times, and enter a different month each time. Verify that the program displays the correct season name each time.


C++ .NEt - the switch-case-break-default output example







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


Performing Loops


For the rest of this module, you’ll see how to perform loops in Visual C++. You’ll also see how to perform unconditional jumps in a loop by using the break and continue statements. Visual C++ has three loop constructs: the while loop, the for loop, and the do-while loop. Let’s look at the while loop first.


Using while Loops


The following illustration shows a simple while loop.


C++ .Net - the while flowchart





The following example shows how to write a simple while loop in Visual C++:

int count = 1;

while (count <= 5)


    Console::WriteLine(count * count);



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

The while keyword is followed by a conditional expression enclosed in parentheses. The parentheses are mandatory. If the conditional expression evaluates to true, the while body is executed. After the loop body has been executed, control returns to the while statement and the conditional expression is tested again. This sequence continues until the test evaluates to false. Remember to include some kind of update statement in the loop so that the loop will terminate eventually. The update statement in the preceding example is count++, which increments the loop counter. If you don’t provide an update statement, the loop will iterate forever.

The preceding example displays the output in the following graphic.


C++ .NEt programming - the while program output example


In this exercise, you will enhance your Calendar Assistant application so that the user can enter five dates.

Continue working with the project from the previous exercise.

Modify the code in the main() function to enable the user to enter five dates.

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


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

   // Declare and initialize the loop counter

   int count = 1;

   // Test the loop counter

   while (count <= 5)


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



      int year = GetYear();

      int month = GetMonth();

      int day = GetDay(year, month);

      DisplayDate(year, month, day);

      // Increment the loop counter



   return 0;


Build and run the program. The program prompts you to enter the first date. After you have entered this date, the program prompts you to enter the second date. This process continues until you have entered five dates, at which point the program closes.


C++ .Net programming tutorial - the while loop program output example



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



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