Read Chapter 5 (Control Structures for Repetition): • Section 5.1 through Section 5.4 (while loop, increment, decrement, counters) • Section 5.7 (keeping a running total) • Section 5.11 (breaking out of a loop)

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

What is the output of for (row= 0; row < 3; row++) { for (star = 0; star < 20; star++) {cout << '*'; if (star == 10) break; } cout << endl; }

**************** **************** **************** on page 283

example: What does a=2 b=5 c=a+ a*b++; cout << a << " " << b << " " << c; output?

2, 6, 10 but if we changed te statemnt to read c = a * ++b; variable b would be incremented before it was multiplied by a, so the cout statement would display 2, 6, 12. However, you can't do c= ++(a*b) this causes an error.

Breaking out of a loop

C++ provides ways to break out of a loop or out of a loop interation early. For example, using a break statement.

Using break in a Nested Loop

In a nested loop, the break statement only interrupts the loop it is placed in.

Postfix vs Prefix ++ or -- operators

Postfix mode or ++num causes the increment to happen after the value of the variableis used in the expression. For example, 4 is displayed through the cout before num is incremented to 5. Prefix mode however causes the increment to be done first. Num will first be incremented to 5 and then cout will display 5 i.e. ++num. --> probably use more.

The continue statement

Sometimes you want to stay in a loop but cause the curent loop iteration to end immediately. When continue is encountered, all the statements in the body of the loop that appear after it are ignored, and the loop prepares for the next iteration.

Counters

a counter is a variable that is regularly incremented or decremented each time a loop iterates. i.e. having a while loop where if a condition is true something happens and then 1 is added to it or something so that it is not an infinite loop. while (num <= lastNum) { cout << num << (num * num) << endl; num++; }

Keeping a running total

a running total is a sum of numbers that accumulates with each iteration of a loop. The variable used to keep the running total is called an accumulator. Programs usually add a series of numbers using 2 elements: 1.) a loop that reads each number in the series. 2.) a variable that accumulates the total of the numbers as they are read.

The increment and decrement operators

num = num+1 num += 1; num++ num = num-1 num -= 1; num-- They can only operate on a variable not a number.

Sentinel

special value that cannot be mistaken for a member of the list and that signals that tehre are no more values to be entered. When the user enters the sentinel, the loop terminated.

Accumulator

variable used to keep the running total of the numbers. (keeps on reading until there is not another number left)

Keeping a running total process

1.) Set accumulator to 0 then salesTotal = 0.0 day = 1 while (day <= NUM_DAYS) { cout << "enter the sales for day " << day << ": "; cin >> dailySales; totalSales = totalSales + dailySales; day++; } Outputs as: Enter the sales for day 1: Enter the sales for day 2: Enter the sales for day 3: Enter the sales for day 4: Enter the sales for day 5: Enter the sales for day 6:

The While Loop

Two important parts: 1.) an expression that is tested for a true or false value and 2.) a statement or bloack that is repeated as long as the expression is true.


Kaugnay na mga set ng pag-aaral

Pulmonary system part 2: Segment 1

View Set

CHAPTER 6: LOVING AND CHOOSING A LIFE PARTNER

View Set

Nutrition and Health exam study guide 2

View Set