In the header of a for loop, which expression should not end with a semicolon?

Computers are very good at performing repetitive tasks very quickly. In this section we will learn how to make computer repeat actions either a specified number of times or until some stopping condition is met.

while Loops ( Condition-Controlled Loops )

  • Both while loops and do-while loops ( see below ) are condition-controlled, meaning that they continue to loop until some condition is met.
  • Both while and do-while loops alternate between performing actions and testing for the stopping condition.
  • While loops check for the stopping condition first, and may not execute the body of the loop at all if the condition is initially false.
  • Syntax:
        while( condition ) 
            body;

where the body can be either a single statement or a block of statements within { curly braces }.

        int i = 0;
        while( i < 5 ) 
            printf( "i = %d\n", i++ );
        printf( "After loop, i = %d\n", i );

do-while Loops

  • do-while loops are exactly like while loops, except that the test is performed at the end of the loop rather than the beginning.
  • This guarantees that the loop will be performed at least once, which is useful for checking user input among other things ( see example below. )
  • Syntax:
        do { 
            body;
        } while( condition );
  • In theory the body can be either a single statement or a block of statements within { curly braces }, but in practice the curly braces are almost always used with do-whiles.
  • Example:
        int month;
        do { 
            printf( "Please enter the month of your birth > " );
            scanf( "%d", &month );
        } while ( month < 1 || month > 12 );
  • Note that the above example could be improved by reporting to the user what the problem is if month is not in the range 1 to 12, and that it could also be done using a while loop if month were initialized to a value that ensures entering the loop.
  • The following diagram shows the difference between while and do-while loops. Note that once you enter the loop, the operation is identical from that point forward:

In the header of a for loop, which expression should not end with a semicolon?

for Loops

  • for-loops are counter-controlled, meaning that they are normally used whenever the number of iterations is known in advance.
  • Syntax:

In the header of a for loop, which expression should not end with a semicolon?

where again the body can be either a single statement or a block of statements within { curly braces }.

You want to keep printing as long as first < second and after each iteration, you increment first and decrement second.

This is what you'd need to do:

  • In init, you need to initialize two variables. How can you do this?
  • In update, you need to update two variables. How can you do this?
You can't let init be int first = 0; int second = 10 because the compiler would get confused. It sees the semicolon and thinks that you are writing a condition after the semicolon. It sees a declaration, and prints an error message.

For the same reason, you can't write update as i++ ; j--. Again, the semicolon confuses the compiler.

The solution? Use the comma operator.

Personally, I don't think the comma operator is a great idea, and I suspect it was invented just for the for loop.

Comma Operator

The comma operator is like the semicolon, but not quite. Here's the syntax.
expr1, expr2
You can have two or more expressions separated by commas. The entire thing is also an expression.

Semantics

The semantics is to evaluate the expressions left to right. The final result of the evaluation is the evaluated value of the rightmost expression.

Now think about whether this is a good idea or not.

Example

Consider the following code.
   int j = 2, k = 4 ;
   int x ;

   // Assignment statement with comma operator
   x = j + 1, k ;
Recall the semantics of an assignment statement. Evaluate the RHS, then take the value and put it in the box on the LHS.

In this case, we evaluate j + 1 to 3, and then evaluate k to 4. The final result of this comma expression is 4 which is the evaluated value of the rightmost expression.

Thus, x is assigned to 4.

Here's the problem. Why did we bother writing j + 1? It never gets used. So why is it there?

Side Effects

We only write comma expressions if each expression (except possibly the rightmost) has a side effect. That is, it should change the values of the variables in the expression.

If we don't do this, it makes little sense to have the comma operator.

The following makes a little more sense:

   x = j++, k ;
At the very least, j is updated. Even here, it makes more sense to write the code without the comma operator.
   j++ ;
   x = k ;

for loops

The comma operator makes the most sense in a for loop. Let's solve the problem we posed at the beginning of this lesson.
  for ( int first = 0, second = 10 ;
        first < second ;
        first++, second-- )
  {
     System.out.println( first + " " + second ) ;
  }
The comma operator is used only in the update. The init is a declaration. We're allowed to have a declaration in init. This prevents us from declaring variables of two different types.

We could use the comma operator in init provided we didn't declare variables. For example,

  int first, second ;
  for ( first = 0, second = 10 ;
        first < second ;
        first++, second-- )
  {
     System.out.println( first + " " + second ) ;
  }
In this case, we could have variables of different types in the init, separated by comma operators.

In the example above, we actually have two assignment statements (which are also expressions), separated by commas, not a declaration. This distinction is subtle, so I hope you noticed it.

What is the major advantage of using a for loop instead of a while loop?

The main advantage of a for loop over a while loop is readability. A For loop is a lot cleaner and a lot nicer to look at. It's also much easier to get stuck in an infinite loop with a while loop.

Which executes first in a do

The do-while loop is an exit-condition loop. This means that the body of the loop is always executed first. Then, the test condition is evaluated. If the test condition is TRUE, the program executes the body of the loop again.

How many iterations will occur if the test expression of a for loop is false to begin with?

3. How many iterations will occur if the test expression of a for loop is false to begin with? If the test expression of a for loop is false to begin with, it would not perform any iteration and exits the loop.

Which of the following is a repetition structure in C++?

Repetition Statements The three loop structures in C++ are: while loops. do-while loops. for loops.