cs 141 computer programming 1

68
CS 141 Computer Programming 1 Teacher Assistant AlAnoud Bahomaid 1 Control Statements

Upload: charo

Post on 22-Feb-2016

46 views

Category:

Documents


0 download

DESCRIPTION

Control Statements. CS 141 Computer Programming 1. Teacher Assistant AlAnoud Bahomaid. Outline. Control structure in c++ Repetition Structure Counter control loop While repetition structure For repetition structure Optional expressions in the for statement header Common logical error - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: CS 141 Computer Programming 1

1

CS 141Computer

Programming 1

Teacher AssistantAlAnoud Bahomaid

Control Statements

Page 2: CS 141 Computer Programming 1

2Outline Control structure in c++ Repetition Structure Counter control loop While repetition structure For repetition structure Optional expressions in the for statement header Common logical error The counter variable Scope For repetition example Convert for and while Do… while repetition structure Nesting loop Nested control structure Continue Break

Page 3: CS 141 Computer Programming 1

C++ Control

Structure

Sequence structure

Selection Structure

If, if… else, switch

Repetition structure

While, do…while, for

Control structures in C++

Page 4: CS 141 Computer Programming 1

4

repetition structure

It’s a control structure that repeats a group of steps in a program

Action repeated while some condition remains true

Definition

while do.. whilefor

Page 5: CS 141 Computer Programming 1

5

repetition structure ask yourself some of the following questions

to determine whether loops will be required in your code:

Were there any steps I repeated as I solved the problem? If so, which ones?

If the answer to question 1 is yes, did I know in advance how many times to repeat the steps?

If the answer to question 2 is no, how did I know how long to keep repeating the steps?

When use repetition structure

Page 6: CS 141 Computer Programming 1

6

counter-controlled loop

Essentials of counter-controlled repetition requires:

The name of a control variable (loop counter) The initial value of the control variable. The loop-continuation condition that test for the

final value of the control variable. The increment (or decrement) by which the control

variable is modified each time through the loop

a loop whose required number of iterations can be determined before loop execution begins

Definition

Page 7: CS 141 Computer Programming 1

7

Sentinel-controlled loop

•A loop whose number of iterations is UNKNOWN. (indefinite repetition).

•Use a special value called sentinel value (signal value, dummy value or a flag value).

• Indicates the end of the loop.

Definition

Page 8: CS 141 Computer Programming 1

True

Activity Diagram

1-While Repetition Structure (1-10)

Condition

False

Action (a)

Page 9: CS 141 Computer Programming 1

9

1-While Repetition Structure (2-10)

If the controlling expression( condition) is true, the loop body is then executed before the controlling expression is evaluated once more.

If the controlling expression (condition) is false, i.e. expression evaluates to false, the program goes on to execute the statement following the while loop.

If the body of the counter-controlled repetition contains more than one statement, you should surround its body by braces { }.

Page 10: CS 141 Computer Programming 1

10

1-While Repetition Structure (2-10)

While( loop repetition condition) statement;

OR

While( loop repetition condition){

statements; . . }

Syntax

No semicolon after while statement

Page 11: CS 141 Computer Programming 1

Write a program that print numbers from 1 to 10 using (while) loop

1-While Repetition Structure (4-10)

Question

Page 12: CS 141 Computer Programming 1

12 1-While Repetition Structure (5-10)

Page 13: CS 141 Computer Programming 1

13 1-While Repetition Structure (6-10)

Page 14: CS 141 Computer Programming 1

14

1-While Repetition Structure (7-10)

In previous Example:Control variable name : counterInitial value : 1The loop continuation condition:

counter<=10The loop counter is incremented : +

+counter ;

Page 15: CS 141 Computer Programming 1

Write a program to find the average of the students grade for ten subjects in the class.

1-While Repetition Structure (8-10)

Question

Page 16: CS 141 Computer Programming 1

16

1-While Repetition Structure (9-10)

Page 17: CS 141 Computer Programming 1

17 1-While Repetition Structure (10-10)

Page 18: CS 141 Computer Programming 1

18

2 -for Repetition Structure (1-8)

for ( initialization ; LoopContinuationTest ; increment ) statement

OR

for ( initialization ; LoopContinuationTest ; increment ) { statementS

.

. }

Syntax

No semicolon after for statement

Page 19: CS 141 Computer Programming 1

19

(2 -for Repetition Structure (2-8

As we said in while, If you need to repeat more than one statement in a program loop, you must place the statements in a block marked by braces { }.

Page 20: CS 141 Computer Programming 1

Write a program that print numbers from 1 to 10 using (for) loop

2- for Repetition Structure (3-8)

Question

Page 21: CS 141 Computer Programming 1

212-for Repetition Structure (4-8)

Page 22: CS 141 Computer Programming 1

22

2-for Repetition Structure (5-8)

Page 23: CS 141 Computer Programming 1

23

2-for Repetition Structure (6-8)

Page 24: CS 141 Computer Programming 1

24

Initialization and increment: For multiple variables, use comma-separated

lists

for (int i = 0, j = 0; j + i <= 10; j++,i++ ) cout << j + i << endl;

2-for Repetition Structure (7-8)

Page 25: CS 141 Computer Programming 1

25

2-for Repetition Structure (8-8) Vary control variable from 1 to 5 in increments of 1

for ( int i = 1; i <= 5; i++ ) Vary control variable from 5 to 1 in decrements of 1

for ( int i = 5; i >= 1; i-- ) Vary control variable from 7 to 77 in steps of 7

for ( int i = 7; i <= 77; i += 7 ) Vary control variable from 20 to 2 in steps of -2

for ( int i = 20; i >= 2; i -= 2 ) Vary control variable over the sequence: 2, 5, 8, 11, 14, 17, 20 for ( int i = 2; i <= 20; i += 3 )

Vary control variable over the sequence: 99, 88, 77, 66, 55, 44, 33, 22, 11, 0 for ( int i = 99; i >= 0; i -= 11 )

Page 26: CS 141 Computer Programming 1

26

Optional expressions in the for statement header

All the three expressions in the for statement header are optional . The two semicolon are required.

Omitting the loopContinuationCondition: C++ assumes that the condition is true.

for ( int i=0 ; ; i++)

Omitting the initialization expression: The counter variable must be initialized earlier in the program.

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

Omitting increment expression: The increment is calculated by statement in the body.

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

Page 27: CS 141 Computer Programming 1

27

Optional expressions in the for statement headerExample

s:

1. Loop2. Loop3. Loop4. loop..10. loop

0123456789

Infinite loop

- initialize control variable before the loop

- increment control variable in the loop body

Page 28: CS 141 Computer Programming 1

28

Common logical Error Be sure to verify that a loop’s repetition

condition will become false (0); otherwise, an infinite loop may result.

Examplefor (int counter=1 ; counter <= 25 ; counter--)

cout << counter << endl;

This condition will never be false

int counter = 1;while (counter <= 25){ cout << counter << endl; counter -- ;}

This condition will never be false

Page 29: CS 141 Computer Programming 1

29The counter variable Scope If the initialization expression declares

the control variable , the control variable can be used only in the body of the for statements.

This is what we called variable scope.

Page 30: CS 141 Computer Programming 1

Write a program that calculate the sum of even integers from 2 through 100 .

then determine the scope of the control variable .

for Repetition Example (1-5)

Question

Page 31: CS 141 Computer Programming 1

31

for Repetition Example (2-5)

Page 32: CS 141 Computer Programming 1

32

for Repetition Example (3-5)

Page 33: CS 141 Computer Programming 1

33

for Repetition Example (4-5)

sum sum += number

number

2 0=+2 26 2=+4 4

12 6=+6 620 12=+8 830 20=+10 10..…..…..…

..…

..…

..…

..…

..…

..…

2550 2450=+100 100

Page 34: CS 141 Computer Programming 1

34

for Repetition Example (5-5)

The scope of the control variable (number) is:

Line 14 and Line 15

Page 35: CS 141 Computer Programming 1

35

Convert for and while for loops can usually be rewritten as while loops

for(initialization; loopContinuationCondition; increment) {

Statement }

Is Equivlent to:

initialization;while (loopContinuationCondition){

statement increment;

}

Page 36: CS 141 Computer Programming 1

36

3-do… while structure Repetition (1-9) The do…while repetition statement is similar to

the while statement.

In the while: The loop continuation condition test occurs at the

beginning of the loop before the body of the loop executes.

In the do … while: The loop continuation condition test occurs after

the loop body executes. The loop body always executes at least once. Recommended to use braces in the do.. While to

avoid confusing with while statements.

Page 37: CS 141 Computer Programming 1

True

Action (a)

Activity Diagram

3-do… while structure Repetition (2-9)

Condition

False

Page 38: CS 141 Computer Programming 1

38

3-do… while structure Repetition (3-9)

do{ statement

}while ( condition );

Syntax

Don’t forget semicolon after condition

Page 39: CS 141 Computer Programming 1

Write a program that print numbers from 1 to 10 using (do.. while) loop

3-do… while structure Repetition (4-9)

Question

Page 40: CS 141 Computer Programming 1

40

3-do… while Repetition Structure (5-9)

Page 41: CS 141 Computer Programming 1

41

3-do… while Repetition Structure (6-9)

Page 42: CS 141 Computer Programming 1

42

3-do… while structure Repetition (7-9)

Another solution

Notice the pre increment in loop-continuation condition.

Page 43: CS 141 Computer Programming 1

Write a program that request the user to type positive numbers (one each time),

and calculate the average when the user enter a number less than one.

3-do… while structure Repetition (8-9)Questio

n

Enter a number (less than 1 = quit): 3Enter a number (less than 1 = quit): 1Enter a number (less than 1 = quit): 4Enter a number (less than 1 = quit): 0The average is: 2Press any key to continue . . .

Enter a number (less than 1 = quit): 0No averagePress any key to continue . . .

Page 44: CS 141 Computer Programming 1

3-do… while structure Repetition (9-9)Solutio

n

Page 45: CS 141 Computer Programming 1

45

Nesting Loops (1-7)

Loops can be nested, that is, the loop body can also contain a loop.

Page 46: CS 141 Computer Programming 1

46

Nesting Loops (2-7)

Write a program that print this using Loop

Question

Page 47: CS 141 Computer Programming 1

47

Nesting Loops (3-7)

Page 48: CS 141 Computer Programming 1

48

Nesting Loops (4-7)

Page 49: CS 141 Computer Programming 1

49

Nesting Loops (5-7)

Write a program that print this using Loop

Question

Page 50: CS 141 Computer Programming 1

50

Nesting Loops (6-7)

Page 51: CS 141 Computer Programming 1

51

Nesting Loops (7-7)

Page 52: CS 141 Computer Programming 1

52

Nested Control Structures (1-5)

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

Question

Page 53: CS 141 Computer Programming 1

53

Nested Control Structures (2-5)

Page 54: CS 141 Computer Programming 1

54

Nested Control Structures (3-5)

Page 55: CS 141 Computer Programming 1

55

Nested Control Structures (4-5)

Enter result (1 = pass, 2 = fail): 1Enter result (1 = pass, 2 = fail): 2Enter result (1 = pass, 2 = fail): 2Enter result (1 = pass, 2 = fail): 1Enter result (1 = pass, 2 = fail): 1Enter result (1 = pass, 2 = fail): 1Enter result (1 = pass, 2 = fail): 2Enter result (1 = pass, 2 = fail): 1Enter result (1 = pass, 2 = fail): 1Enter result (1 = pass, 2 = fail): 2Passed 6Failed 4

output

Page 56: CS 141 Computer Programming 1

56Nested Control Structures (5-5)

Enter result (1 = pass, 2 = fail): 1Enter result (1 = pass, 2 = fail): 1Enter result (1 = pass, 2 = fail): 1Enter result (1 = pass, 2 = fail): 1Enter result (1 = pass, 2 = fail): 2Enter result (1 = pass, 2 = fail): 1Enter result (1 = pass, 2 = fail): 1Enter result (1 = pass, 2 = fail): 1Enter result (1 = pass, 2 = fail): 1Enter result (1 = pass, 2 = fail): 1Passed 9Failed 1Raise tuition

output

Page 57: CS 141 Computer Programming 1

57

Break (1-6)

The break statement exits from a switch or loop immediately.

You can use the break keyword to stop a loop for any reason.

Page 58: CS 141 Computer Programming 1

58

Break (2-6)

Page 59: CS 141 Computer Programming 1

59 Break (3-6)

Example (1):

Page 60: CS 141 Computer Programming 1

60

Break (4-6)

Output (1):

Page 61: CS 141 Computer Programming 1

61

Break (5-6)

Example(2) :

Page 62: CS 141 Computer Programming 1

62

Break (6-6)

Output (2):

Page 63: CS 141 Computer Programming 1

63

Continue (1-6)

The continue statement can be used in loops and has the opposite effect to break, that is, the next loop is begun immediately.

In while, do…while: The loop continuation condition evaluates

immediately after the continue statement executes.

In for statement: The increment expression executes, then the

loop-continuation test evaluates.

Page 64: CS 141 Computer Programming 1

64Continue (2-6)

int counter = 0;while ( counter <= 10 ) { ++counter; continue; cout << counter << endl; }

int counter= 0; do { ++counter; continue; cout << counter << endl; } while(counter <= 10);

for (int counter=1 ; counter <=10 ; counter++){

continue; cout << counter << endl;}

This Line never Excute

This Line never Excute

This Line never Excute

Page 65: CS 141 Computer Programming 1

65

Continue (3-6)

Example (1):

Page 66: CS 141 Computer Programming 1

66

Continue (4-6)

Output (1)

Page 67: CS 141 Computer Programming 1

67

Continue (5-6)

Example (2):

Page 68: CS 141 Computer Programming 1

68

Continue (6-6)

Output (2)