# data[].attributes.body
•! (logical NOT, logical negation)

–Returns true when its condition is false, & vice versa

if ( !( grade == sentinelValue ) )
cout << "The next grade is " << grade << endl;

Alternative:

if ( grade != sentinelValue )
cout << "The next grade is " << grade << endl;

# data[].attributes.body
break statement
Immediate exit from while, for, do/while, switch
Program continues with first statement after structure
Common uses
Escape early from a loop
Skip the remainder of switch

# data[].attributes.body
•Similar to while structure

–Makes loop continuation test at end, not beginning

–Loop body executes at least once

•Format

do {

statement

} while ( condition );

•

# data[].attributes.body
•switch

–Test variable for multiple values

–Series of case labels and optional default case

switch ( variable ) {
case value1:        // taken if variable == value1

statements

break;              // necessary to exit switch

case value2:

case value3:     // taken if variable == value2 or == value3

statements

break;

default:        // taken if variable matches no other cases

statements
break;

}

# data[].attributes.body
•for loops can usually be rewritten as while loops

initialization;

while ( loopContinuationTest){

statement

increment;

}

•Initialization and increment

–For multiple variables, use comma-separated lists

for (inti = 0, j = 0;  j + i <= 10; j++, i++)

cout << j + i << endl;

# data[].attributes.body
•Counter-controlled repetition requires

–Name of control variable/loop counter

–Initial value of control variable

–Condition to test for final value

–Increment/decrement to modify control variable when looping

# data[].attributes.body
•Increment operator (++) - can be used instead of c += 1

•Decrement operator (--) - can be used instead of c -= 1

–Preincrement

•When the operator is used before the variable (++c or –c)

•Variable is changed, then the expression it is in is evaluated.

–Posincrement

•When the operator is used after the variable (c++ or c--)

•Expression the variable is in executes, then the variable is changed.

# data[].attributes.body
•Assignment expression abbreviations

–Addition assignment operator

c = c + 3; abbreviated to
c += 3;

•Statements of the form

variable = variable operator expression;

can be rewritten as

variable operator= expression;

•Other assignment operators

d -= 4     (d = d - 4)

e *= 5     (e = e * 5)

f /= 3     (f = f / 3)

g %= 9     (g = g % 9)

# data[].attributes.body
•Problem statement

A college has a list of test results (1 = pass, 2 = fail) for 10 students.  Write a program that analyzes the results.  If more than 8 students pass, print "Raise Tuition".

•Notice that

–Program processes 10 results

•Fixed number, use counter-controlled loop

–Two counters can be used

•One counts number that passed

•Another counts number that fail

–Each test result is 1 or 2

•If not 1, assume 2

# data[].attributes.body
•Counter-controlled repetition

–Loop repeated until counter reaches certain value

•Definite repetition

–Number of repetitions known

# data[].attributes.body
•Repetition structure

–Action repeated while some condition remains true

–Psuedocode

while there are more items on my shopping list

Purchase next item and cross it off my list

–while loop repeated until condition becomes false

•Example

int product = 2;

while ( product <= 1000 )

product = 2 * product;

# data[].attributes.body
•if

–Performs action if condition true

•if/else

–Different actions if conditions true or false

•Pseudocode

if student’s grade is greater than or equal to 60
print “Passed”

else

print “Failed”

•C++ code

if ( grade >= 60 )
cout << "Passed";
else
cout << "Failed";

# data[].attributes.body
•Sequential execution

–Statements executed in order

•Transfer of control

–Next statement executed not next one in sequence

•3 control structures (Bohm and Jacopini)

–Sequence structure

•Programs executed sequentially by default

–Selection structures

•if, if/else, switch

–Repetition structures

•while, do/while, for

# data[].attributes.body
•Pseudocode

–Artificial, informal language used to develop algorithms

–Similar to everyday English

•Not executed on computers

–Used to think out program before coding

•Easy to convert into C++ program

–Only executable statements

•No need to declare variables

# data[].attributes.body
•Computing problems

–Solved by executing a series of actions in a specific order

•Algorithm a procedure determining

–Actions to be executed

–Order to be executed

–Example: recipe

•Program control

–Specifies the order in which statements are executed

# data[].attributes.body
•Before writing a program

–Have a thorough understanding of problem

–Carefully plan your approach for solving it

•While writing a program

–Know what “building blocks” are available

–Use good programming principles

# data[].attributes.body
Introduction