csci 161 lecture 7 martin van bommel. control statements statements that affect the sequence of...

27
CSCI 161 Lecture 7 Martin van Bommel

Upload: evelyn-long

Post on 18-Jan-2016

217 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: CSCI 161 Lecture 7 Martin van Bommel. Control Statements Statements that affect the sequence of execution of other statements Normal is sequential May

CSCI 161

Lecture 7

Martin van Bommel

Page 2: CSCI 161 Lecture 7 Martin van Bommel. Control Statements Statements that affect the sequence of execution of other statements Normal is sequential May

Control Statements

• Statements that affect the sequence of execution of other statements

• Normal is sequential

• May want to repeat statements - iteration

• May or may not want particular statement executed at all - conditional

Page 3: CSCI 161 Lecture 7 Martin van Bommel. Control Statements Statements that affect the sequence of execution of other statements Normal is sequential May

Control Statement Structure

• Control line - first line (e.g. while)

• Body - statements enclosed in { } that are repeated

• Body typically indented to show range of control statement

Page 4: CSCI 161 Lecture 7 Martin van Bommel. Control Statements Statements that affect the sequence of execution of other statements Normal is sequential May

Conditional Execution

• May want to execute a statement only if some condition applies

• May wish to choose between two alternative courses of actions depending on some test

• Simplest form - if statement

Page 5: CSCI 161 Lecture 7 Martin van Bommel. Control Statements Statements that affect the sequence of execution of other statements Normal is sequential May

if Statement

if (conditional-test)

{

…statements executed if test true...

}} else {

…statements executed if test false...

}

Page 6: CSCI 161 Lecture 7 Martin van Bommel. Control Statements Statements that affect the sequence of execution of other statements Normal is sequential May

Relational & Logical Operators== Equal != Not equal

> Greater < Less

>= Greater or equal

<= Less or equal• Logical operate on results of comparisons

! Not (true if operand false – one operand)

&& And (true if both true)

|| Or (true if either true)

Page 7: CSCI 161 Lecture 7 Martin van Bommel. Control Statements Statements that affect the sequence of execution of other statements Normal is sequential May

Example 1

• “x is not equal to either 2 or 3”

if (x != 2 || x != 3)• No, it should be

if (!(x == 2 || x == 3))• or

if (x != 2 && x != 3)

Page 8: CSCI 161 Lecture 7 Martin van Bommel. Control Statements Statements that affect the sequence of execution of other statements Normal is sequential May

Example 2

• “x is in the range from 0 to 10 exclusive”

if (0 < x < 10)• No, it should be

if (0 < x && x < 10)

Page 9: CSCI 161 Lecture 7 Martin van Bommel. Control Statements Statements that affect the sequence of execution of other statements Normal is sequential May

Example

• Leap year every fourth year, except centuries, then just every fourth century– year is divisible by 4 but not by 100, or– year is divisible by 400

• Try((y % 4 == 0) && (y % 100 != 0)) || (y % 400 == 0)

Page 10: CSCI 161 Lecture 7 Martin van Bommel. Control Statements Statements that affect the sequence of execution of other statements Normal is sequential May

Bad Example

if (x < 5)

cout << ”A”;

if (x > 5)

cout << ”B”;

else

cout << ”C”;

Page 11: CSCI 161 Lecture 7 Martin van Bommel. Control Statements Statements that affect the sequence of execution of other statements Normal is sequential May

If Blocking Rule

• For any if statement that requires

(1) more than a single line, or

(2) an else clause,

always use braces to enclose in a separate block the statements under the control of the if statement .

Page 12: CSCI 161 Lecture 7 Martin van Bommel. Control Statements Statements that affect the sequence of execution of other statements Normal is sequential May

Single-line if statement

if (condition) statement

where:

condition is the Boolean value to test

statement is a single statement to beexecuted if condition is true

Page 13: CSCI 161 Lecture 7 Martin van Bommel. Control Statements Statements that affect the sequence of execution of other statements Normal is sequential May

Multiline if statement

if (condition)

{ statements;

}

where

condition is the Boolean value to test

statements is a block of statements to beexecuted if condition is true

Page 14: CSCI 161 Lecture 7 Martin van Bommel. Control Statements Statements that affect the sequence of execution of other statements Normal is sequential May

if-else statements

if (condition)

{ statementsT ;

} else {

statementsF ;

}

Page 15: CSCI 161 Lecture 7 Martin van Bommel. Control Statements Statements that affect the sequence of execution of other statements Normal is sequential May

Cascading if statementif (condition1) {

statements1 ;

} else if (condition2) {

statements2;

. . .

} else {

statementsnone

}

Page 16: CSCI 161 Lecture 7 Martin van Bommel. Control Statements Statements that affect the sequence of execution of other statements Normal is sequential May

Iteration and Loops

• Iteration - process of repeating an operation

• Essential for large amounts of data - repeating statements shortens programs

• Loop - any portion of program repeated via control statement

• e.g. while loop

Page 17: CSCI 161 Lecture 7 Martin van Bommel. Control Statements Statements that affect the sequence of execution of other statements Normal is sequential May

The while statement

while (condition) {

statements

}

where

condition tested before first and every iteration

statements executed each time condition true

Page 18: CSCI 161 Lecture 7 Martin van Bommel. Control Statements Statements that affect the sequence of execution of other statements Normal is sequential May

while operation

• Condition test performed before every cycle, including first.

• If condition is initially false, body of loop not executed at all.

• Condition test performed only at beginning of each loop cycle.

• Even if condition becomes false during cycle, cycle is completed.

Page 19: CSCI 161 Lecture 7 Martin van Bommel. Control Statements Statements that affect the sequence of execution of other statements Normal is sequential May

while example

• Sum up digits of a number

sum = 0;

while (n > 0) {

sum = sum + n % 10;

n = n / 10;

}

Page 20: CSCI 161 Lecture 7 Martin van Bommel. Control Statements Statements that affect the sequence of execution of other statements Normal is sequential May

Infinite loops

• A loop cycle that never stops – infinite loop

• Condition of while statement never false

• e.g.

while (n >= 0) {

sum += n % 10;

n /= 10;

}

Page 21: CSCI 161 Lecture 7 Martin van Bommel. Control Statements Statements that affect the sequence of execution of other statements Normal is sequential May

Repeat-N-Times Idiom• for (i = 0; i < N; i++)

{statements to be repeated

}• May want to add up 10 input numbers

for (i = 0; i < 10; i++){

cin >> value;sum = sum + value;

}

Page 22: CSCI 161 Lecture 7 Martin van Bommel. Control Statements Statements that affect the sequence of execution of other statements Normal is sequential May

Index Variable in Counting Loop

for (i = 0; i < N; i++)• Variable i is called the index variable• Any variable may be used - traditionally i• Must be declared at beginning of function• Initial value of i in loop is 0 - first cycle• Next cycles value is 1, 2, etc.• On last cycle, value is N – 1• After loop, value of i is N

Page 23: CSCI 161 Lecture 7 Martin van Bommel. Control Statements Statements that affect the sequence of execution of other statements Normal is sequential May

Non-Zero Counting Loop

• It is possible to modify for loop to begin at another number

for (i = first; i <= last; i++)

• Note the <= operator instead of <

• Value of i ranges from first to last

• Used if value of i required in body of loop

Page 24: CSCI 161 Lecture 7 Martin van Bommel. Control Statements Statements that affect the sequence of execution of other statements Normal is sequential May

for statement

for (init; test; step) {

statements

}

where

init is an expression evaluated at beginning

test is a condition for continuing the loop

step is an expression to prepare next cycle

Page 25: CSCI 161 Lecture 7 Martin van Bommel. Control Statements Statements that affect the sequence of execution of other statements Normal is sequential May

for example

• Countdown from 10 to 0

for (t=10; t >= 0; t--) {

cout << t;

}

cout << ”Liftoff!\n”;

Page 26: CSCI 161 Lecture 7 Martin van Bommel. Control Statements Statements that affect the sequence of execution of other statements Normal is sequential May

for more

• Note that expressions init, test, and step are optional, but the semicolons must appear

for (;;) - infinite loop

for (;t<10;) same as while (t<10)

Page 27: CSCI 161 Lecture 7 Martin van Bommel. Control Statements Statements that affect the sequence of execution of other statements Normal is sequential May

Nested for loops

• Create a 10 x 10 multiplication table

for (row=1; row<=10; row++) {

for (col=1; col<=10; col++) {

cout << (row * col);

}

cout << endl;

}