一、基本控制結構

ISO/ANSI C++中的控制與循環全部適用于C++/CLI。下例展示了C++/CLI控制臺程序中的控制循環:

例子:基本循環控制

- - - - - - - - - - - - - - - - <<== 華麗的分割線 ::開始==>> [Ex3_15.cpp] - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Ex3_15.cpp : main project file.
#include "stdafx.h"

using namespace System;

int main(array<System::String ^> ^args)
{
    wchar_t letter;

    Console::Write(L"Enter a letter:");
    letter = Console::Read();

    if(letter>='A')
        if(letter<='Z')
        {
            Console::WriteLine(L"You entered a captial letter.");
            return 0;
        }

    if(letter>='a')
        if(letter<='z')
        {
            Console::WriteLine(L"You entered a small letter.");
            return 0;
        }

    Console::WriteLine(L"You did not enter a letter.");
    return 0;
}
- - - - - - - - - - - - - - - - <<== 華麗的分割線 ::結束==>> [Ex3_15.cpp] - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

letter被聲明為wchar_t類型,映射為C++/CLI中的System::Char類型,它具有一些特殊的功能,其中包括將字符代碼轉換為大寫和小寫的函數:Char::ToUpper()和Char::ToLower(),被轉換的函數作為參數被傳遞給它:

wchar_t uppercaseLetter = Char::ToUpper(letter);

此外還包括檢測字母是否大寫或小寫的函數:IsUpper()和IsLower(),因此上例可改為

wchar_t letter; 
wchar_t upper; 

Console::Write(L"Enter a letter:"); 
letter = Console::Read(); 
upper = Char::ToUpper(letter); 

if(upper>='A' && upper<='Z') 
    Console::WriteLine(L"You entered a {0} letter.", Char::IsUpper(letter) ? "Capital":"Small"); 
else 
    Console::WriteLine(L"You entered a small letter.");

Console::ReadKey()函數用于測試按鍵,并將結果存儲在ConsoleKeyInfo類對象里。該類有3個可訪問屬性用于幫助確定被按下的鍵是哪個或哪些鍵。屬性Key識別被按下的鍵是哪個,屬性KeyChar是被按鍵的Unicode字符碼,屬性Modifiers表示Shift,Alt,Ctrl鍵的按位組合,它是定義在System命名空間中的枚舉類型ConsoleModifiers的常量,包括Shift\Alt\Control。

應該注意的是,在C++/CLI中枚舉常量在用作數值之前必須被顯示的強制轉換為值類型(整數類型)

例子:使用Console::ReadKey()函數

- - - - - - - - - - - - - - - - <<== 華麗的分割線 ::開始==>> [Ex3_16.cpp] - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Ex3_16.cpp : main project file.

#include "stdafx.h"

using namespace System;

int main(array<System::String ^> ^args)
{
	ConsoleKeyInfo keyPress;

	Console::WriteLine(L"Press a key combination - press Escape to quit.");
	do{
		keyPress = Console::ReadKey(true);
		Console::Write(L"You pressed");
		if(safe_cast<int>(keyPress.Modifiers)>0)
			Console::Write(L" {0}", keyPress.Modifiers);
		Console::WriteLine(L" {0} which is the {1} character", keyPress.Key, keyPress.KeyChar);
	}while(keyPress.Key != ConsoleKey::Escape);
    return 0;
}
- - - - - - - - - - - - - - - - <<== 華麗的分割線 ::結束==>> [Ex3_16.cpp] - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

該程序的輸入示例如下:

Press a key combination - press Escape to quit.
You pressed Enter which is the  character
You pressed Spacebar which is the   character
You pressed Spacebar which is the   character

從輸出中可以看出,當不只一個鍵被按下時,用一條語句就可以得到所有的鍵。這是因為Modifiers枚舉類型是用FlagsAttribute屬性定義的,該屬性表明這種枚舉類型是一組唯一的位標志。這使得該枚舉類型的變量可以由若干與在一起的標志位組成,而Write()或WriteLine()函數可以識別并輸出各標志位。

二、for each循環

以例子開始

- - - - - - - - - - - - - - - - <<== 華麗的分割線 ::開始==>> [Ex3_17.cpp] - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Ex3_17.cpp : main project file.

#include "stdafx.h"

using namespace System;

int main(array<System::String ^> ^args)
{
	int volwels = 0;
	int consonants = 0;
	String^ proverb = L"A nod is as good as a wink to a blind horse.";

	for each(wchar_t ch in proverb)
	{
		if(Char::IsLetter(ch))
		{
			ch = Char::ToLower(ch);
			switch(ch)
			{
			case 'a': case 'e': case 'i': case 'o': case 'u':
				++volwels;
				break;

			default:
				++consonants;
				break;
			}
		}
	}

	Console::WriteLine(proverb);
	Console::WriteLine(L"The proverb contains {0} volwels and {1} consonants.",
		volwels, consonants);
    return 0;
}
- - - - - - - - - - - - - - - - <<== 華麗的分割線 ::結束==>> [Ex3_17.cpp] - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

輸出為:

A nod is as good as a wink to a blind horse. 
The proverb contains 14 volwels and 18 consanants. 

注意:由于proverb字符串中的字符都是Unicode字符,因此用wchar_t(映射為Char類型)類型的變量來存儲這些字符。變量ch為循環內的局部變量。