C++/hi

Advertisement


Question
hi can you tell me these definition in your own words because im having trouble remembering what each does could you provide me an example too?
for loop
while loop
do .. while loop
if statement
if..else statement
nested if statement
counter controlled loop
sentinel controlled loop


Answer
Hello, again, Raymond.

I'd be happy to help you understand these concepts.  I'll start with the if and if/else statements.

The if statement is one of those statements in C++ that almost works as a plain-English statement.  The idea behind the if statement is that a block of code is only executed if a particular condition is met.  The format for the if statement is:

   if (condition)
       body

The body section can either be a single statement (one "line" of code -- executes up to the semicolon), or a block of code, which is delineated with curly braces -- {}.  It is possible to write a single-statement if on one line, but it really is not advisable because you cannot set a break point inside the body of the if statement.  Generally, try to keep if statements formatted in the way I've given above.  In fact, I generally even suggest people avoid if statements without curly braces, even if there is only a single statement being executed inside the if.  It makes things easier if you decide to alter the if statement body in the future.

A single-statement if would look something like:

   if (x < y)
       cout << "X is less than Y.\n";

A multi-line statement would look something like:

   if (x < y)
   {
       cout << "X is less than Y.  Adding 5 to X.\n";
       x += 5;
   }

One very, very, very important thing to note is the lack of a semicolon after the actual if statement.  If you place a semicolon at the end of the if statement itself, it will cause problems.  It's perfectly compilable, but the body of the if statement no longer belongs to the if statement and would be executed every time.  Most compilers will give a warning about that these days.

The logical English way to read an if statement is something along the lines of "If the condition is met, then execute the code within the if statement's body."

The if/else statement is basically the same as the if statement, except that there is a block of code to execute in case the condition is not true.  A good example of how this works is:

   if (x < y)
   {
       cout << "X is less than Y.\n";
   }
   else
   {
       cout << "Y is less than X.\n";
   }

The term "nested if statement" simply refers to an if statement with another if statement within it.  It is a fairly common thing to do.  A quick example for a nested if:

   if (x == y)
   {
       cout << "X equals Y.\n";
   }
   else
   {
       if (x < y)
       {
         cout << "X is less than Y.\n";
       }
       else
       {
         cout << "Y is less than X.\n";
       }
   }

You can see how the else contains a nested if block.

One of the important ideas to take away from the previous explanation, outside of simply how to use if statements, is the idea of a body of code.  For loops and while loops also use this concept.  Next I will explain the while and do/while loops.

The while loop is also pretty easy to understand at a logical, English level.  The format for a while loop looks something like:

   while (condition)
       body

You can see that it is virtually identical to an if statement, except that the "if" has changed to "while."  The idea behind this statement is that the body of the statement will execute as long as the condition is true.  This, of course, means that it is important to ensure that your condition will become false at some point, else you will loop indefinitely (unless a "break" command is encountered -- the break command will immediately exit the current loop).

In English, the while can be read as "While the condition is met, execute the code within the while statement's body."

A simple example of a while loop:

   while (x < y)
   {
       x++;
   }

One important thing to note about the while loop: the condition is checked before the body is ever executed.  That means that, in the above example, if x is equal to or greater than y when the loop is reached, the body will never execute at all.  This, in fact, is the important distinction between the while loop and the do/while loop.

In a do/while loop, the body of the code is guaranteed to execute at least once.  The format for the do/while loop looks like:

   do
   {
       body
   } while (condition);

One important thing to note here is the semicolon after the while statement in the do/while loop.  This is the only time you will use a semicolon at the end of a while.

The English way to read a do/while loop is something along the lines of "Execute the code within the statement's body, and then continue to execute it while the condition is met."

You'll likely find you don't use do/while loops very often, but when you do find a use for them, it makes the code far cleaner than it would be otherwise.  Often times, if the do/while loop didn't exist, then you would have to duplicate a large section of code to accomplish the same procedure.  Probably the most common use for the do/while loop is gathering and responding to user input.  Something like:

   char userInput;
   do
   {
       cin >> userInput;
   } while (userInput != 'q');

So, that covers while and do/while loops.  The statement which has the hardest time translating into English is the for loop.  First, let me just give you the format, then I'll discuss what it all means:

   for (initialization; condition; counter)
       body

The initialization section of the for loop is where you can set up a loop counter or similar variables.  The initialization section executes once before anything else in the loop occurs.  The next section of the for loop is the condition.  The condition is checked before every iteration of the loop, including the first iteration.  This means if the condition is false at the beginning of the loop, the loop will not execute, just like a standard while loop.  The counter section of the loop executes once after each iteration of the loop.  A for loop that executes three times will execute something like:

   initialization
   condition
   body
   counter
   condition
   body
   counter
   condition
   body
   counter
   condition

The for loop is probably the most commonly used loop in the C++ language.  It is used for simple iterations through arrays:

   for (int i = 0; i < arraySize; ++i)

It is used for simple iteration through linked lists:

   for (Node* pCurr = list.head; pCurr != NULL; pCurr = pCurr->nextNode)

It's used for a great many things.  The for loop will become second nature as you learn C++.

In English, the for loop can be read as "Execute the code within the initialization block.  Then, while the condition is met, execute the code within the statement's body followed by the code within the counter block."

Finally, counter and sentinel controlled loops.  These simply describe the condition section of a loop.  For loops are generally counter controlled loops -- they loop a certain number of times based on a counter condition, such as the array example above.  A sentinel controlled loop is a loop which executes until a special value is met.  The linked list for loop example above is a sentinel controlled loop -- we are waiting until we encounter a specific value to terminate the loop.  The user input do/while loop is another example of a sentinel controlled loop.  The loop executes until the special value of 'q' is entered by the user.

OK, I hope that clears everything up for you.  If you have any other questions, feel free to ask.  I'm here to help. :)

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Joseph Moore

Expertise

I've been programming in one form or another since my brother taught me BASIC when I was 6. I've been programing professionally since I was 20, first web development with HTML, JS, DHTML, CSS, etc., then I became a video game developer, writing code in C, C++, C#, SQL, assembly, and various scripting languages. I've even written my own scripting languages, custom designed for the games I was making. I also dabble in Java, PHP, and Perl. I've worked on pretty much every aspect of game development, including graphics, audio, gameplay, tool, UI, input, animation, and physics.

Experience

I've been writing C++ code for 12 years, both on my own in my spare time and professionally.

Organizations
IGDA

Education/Credentials
Bachelor of Science in Game Design and Development, Full Sail University, Winter Park, FL

Awards and Honors
Salutatorian and Advanced Achiever Awards at Full Sail; Independent Games Festival Student Showcase winner, 2004; Featured article on Gamasutra about an experimental game developed in 2004

©2017 About.com. All rights reserved.