Control Statements and Basic Loops in C++/CLI

This article emphasizes on the usage of statements to control the flow of execution through a C++/CLI application. Most languages offer keywords with which you can make decisions and perform loops; for example, the if statement and the switch statement for making decisions. In addition, the while, for, foreach, and do-while statements for performing loops (repetitive instructions). Moreover, C++/CLI provision the break statement to exit a loop immediately and the continue statement to return to the start of the loop for the next iteration.

Prerequisite

The developer must have a moderate level of proficiency in native C++ programming code syntax manipulation, especially the understanding of code compilation, debugging, and execution under the native and CLR platforms. Also, the latest version of the Visual Studio IDE should be installed at the developer site.

Control Statements

Control statements define what code should executed from a given statement. C++/CLI proposed the if statement, a conditional operator, and switch construct as control statements. You can use an if statement to perform a one-way test, a two-way test, a multi-way test, or a nested test. In this, the if keyword is followed by a conditional expression, which must be enclosed in parentheses. If the conditional expression evaluates to true, the next statement is executed. The following example shows how to define a one-way test in C++/CLI:

Int^ nm;
if (nm < 0)
{
   Console::WriteLine("The number is negative");
}

The following code shows how to define a two-way test for the student grading system application:

Int^ Mpercent;
if (Mpercent > 59 && Mpercent < 101)
{
   Console::WriteLine("First Division");
}

You can arrange if-else statements in a cascading fashion to perform multi-way decision making through the following code:

Int^ days, mn;
if (mn == 4 || mn == 6 || mn == 9 || mn == 11)
{
   days = 30;
}
else if (mn == 2)
{
   days = 28;
}
else
{
   days = 31;
}

C++/CLI is also a provision to perform nested tests within one another. This makes it possible for you to perform more complex logical operations. The following code shows how to use nested tests to check whether you have entered small or capital characters as:

wchar_t ltr;
Console::WriteLine("Enter the Letter");
ltr= Console::Read();
if (ltr >='a')
if(ltr<='z')
{
   Console::WriteLine("you have entered a
      small Letter");
}
if (ltr >='A')
if(ltr<='Z')
{
   Console::WriteLine("you have entered a
      capital Letter");
}

In addition, the conditional operator in C++/CLI is known as a ternary operator. The first argument must result to a Boolean result; if the result is true, the first expression is evaluated. Otherwise, the second one is, as in the following:

String^ str= i>5 ? "India" : "USA";

The switch construct is very similar to that used in C# but with a difference: C++/CLI does not support a string with the case selection. Instead, you have to use an if/else construct. You can use the switch keyword followed by an expression in parentheses in a switch construct. This expression must evaluate to an integer, a character, or an enumeration value. The body of the switch consists of a series of case branches, each of which comprises the keyword case, a value, and a colon. The following sample given you a brief demonstration of this construct:

wchar_t days;
Console::WriteLine("1 = Sunday");
Console::WriteLine("2 = Monday");
Console::WriteLine("3 = Tuesday");
Console::WriteLine("Enter your choice");
days= Console::Read();
switch(days)
{
   case '1': Console::WriteLine("Sunday");
   break;
   case '2': Console::WriteLine("Monday");
   break;
   case '3': Console::WriteLine("Tuesday");
   break;
   default: Console::WriteLine("Out of range");
   break;
}

In addition, if you omit the break statement at the end of a case branch, the flow of control continues on to the next statement, as in the following.

Char^ lcase;
...
switch (lcase)
{
   case 'a':
   case 'e':
   ..
   case 'i': Console::Write("Vowel"); break;
   default: Console::Write("Consonant"); break;
}

Loops

C++/CLI offers for, for each, while, and do-while loop constructs. With loops, code is repeatedly executed until a condition is met. A while loop continues executing its body for as long as the condition in parentheses evaluates to true. You must follow the while keyword with a conditional expression enclosed in parentheses. As long as the expression evaluates to true, the while body executes, followed by the loop body. When the command 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. The following example shows how to write a simple while loop in C++/CLI:

//while loop
Int^ x=0;
while(x<3)
{
   //statements
}

Next, C++/CLI offers a for loop that is an alternative to the while loop. It provides more control over the way in which the loop executes. You can rely on the for keyword to define this loop, in which the parentheses after the for keyword contain three expressions separated by semicolons. The first expression performs the loop initialization, such as initializing the loop counter. This initialization expression is executed once only, at the start of the loop. The following example shows how to write a simple for loop in C++/CLI.

Int^ i=0;
for (i=0;i<5;i++)
{
   Console::WriteLine(i);
}

A for each loop is introduced in C++/CLI. It didn't exist with ANSI C++ because it makes use of the interface IEnumerable. This loop is especially used for iterating through all the objects in a particular kind of set of objects.

array<int>^ arry= {1,2,3,4,5};

foreach(int x in arry)
{
   Console::WriteLine(x);
}

Finally, there is the do-while loop construct that is fundamentally different from the while and for loops because the test comes at the end of the loop body. This means that the loop body is always executed at least once.

//do-while loop
do
{
   //statements
}while(i<3);

Conclusion

In this article, you have learned all of the essential mechanisms for making decisions; for instance, the if and switch statements in C++/CLI programs. You have also gone through all the facilities for repeating a group of statements. That is, there are four basic methods provided in CLI for repeating a block of statements including for, foreach, while, and do-while loop constructs. Finally, you have come across some other keywords, such as break and continue, in the context of loop manipulation.



Related Articles

Comments

  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date