In addition to the 3 circular statements defined in ISO/IEC C + + ,C + +/CLI also provides a for each Statement. In C + +/CLI , The For Each loop is more efficient than several other forms of cycle efficiency.
#include "stdafx.h" using namespace System;int Main (array <system::string ^> ^args) {int vowels (0), 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 ': CAs E ' o ': Case ' u ': ++vowels;break;default:++consonants;break;}}} Console::WriteLine (proverb); Console::WriteLine (L "The proverb contains {0} vowels and {1} consonants.", vowels, consonants); Console::readline (); return 0;}
The program calculates the number of vowels and consonants in the string referenced by the variable proverb by repeating each character in the string with a For Each loop.
C + +/CLI programming-using the For Each loop to access characters in a string