Iteration

C++ provides three iteration structures.

  1. while while ( loopCondition ) { loopBody }
    1. Evaluate loopCondition first.
    2. Execute loopBody repeatedly until loopCondition is false.
  2. do..while: do { loopBody } while ( loopCondition ) ;
    1. Execute loopBody first.
    2. Evaluate loopCondition.
    3. Execute loopBody repeatedly until loopCondition is false.
  3. for loop: for ( initStatement; loopCondition; incrStmt ) { loopBody }
    1. Execute initStatement first.
    2. Execute loopBody repeatedly until loopCondition is false.
    3. After each execution of loopBody, execute incrStmt.

With each of these iteration structures, the loopBody code gets repeated as long as loopCondition evaluates to boolean true. The do loop differs from the other two in that its loopCondition gets checked at the bottom of the loop, so its loopBody is always executed at least once.

A common programming error is to place a semicolon after the while.

while (notFinished());
 doSomething();

The first semicolon terminates the while statement entirely and produces a loop with an empty loopBody. Even though doSomething() is indented, it does not get executed inside the loop. The loopBody is responsible for changing the loopCondition. If notFinished() is initially true, then the empty loopBody will cause an infinite loop. If notFinished() is initially false, then the loop terminates immediately and doSomething() gets executed exactly once.

C++ provides break and continue for finer control over code executed inside loops.

while ( moreWorkToDo ) {
 statement1;
 if ( specialCase ) continue;
 statement2;
 if ( noMoreInput ) break;
 statement3;
// continue jumps here
}
// break jumps here
  • break jumps out of the current control structure, whether it is a switch, for, while, or do..while.
  • continue only operates inside loops, and it skips the rest of the current iteration to the check if there is moreWorkToDo.

Exercises: Iteration

1.

Write the function isPrime(int n) that returns true if n is prime and false otherwise. Supply an interactive main() to test your function.

2.

Write the function primesBetween(int min, int max) that displays on the screen all the prime numbers that are between min and max. Supply an interactive main() to test your function.

3.

Write the function power2(int n) that computes and returns the value of 2 raised to the power n. Supply an interactive main() to test your function.

4.

Write a binary logarithm function binLog(int n) that computes and returns an int equal to the integral part of log2(n), where n is positive. This is equivalent to finding the exponent of the largest power of 2 that is less than or equal to n. For example, binLog(25) is 4. There are at least two simple, iterative ways to do this computation. Supply an interactive main() to test your function.


Part I: Introduction to C++ and Qt 4

C++ Introduction

Classes

Introduction to Qt

Lists

Functions

Inheritance and Polymorphism

Part II: Higher-Level Programming

Libraries

Introduction to Design Patterns

QObject

Generics and Containers

Qt GUI Widgets

Concurrency

Validation and Regular Expressions

Parsing XML

Meta Objects, Properties, and Reflective Programming

More Design Patterns

Models and Views

Qt SQL Classes

Part III: C++ Language Reference

Types and Expressions

Scope and Storage Class

Statements and Control Structures

Memory Access

Chapter Summary

Inheritance in Detail

Miscellaneous Topics

Part IV: Programming Assignments

MP3 Jukebox Assignments

Part V: Appendices

MP3 Jukebox Assignments

Bibliography

MP3 Jukebox Assignments



An Introduction to Design Patterns in C++ with Qt 4
An Introduction to Design Patterns in C++ with Qt 4
ISBN: 0131879057
EAN: 2147483647
Year: 2004
Pages: 268

Flylib.com © 2008-2020.
If you may any questions please contact us: flylib@qtcs.net