Top Banner
Control Statements (2) Dr. Hakem Beitollahi Computer Engineering Department Soran University Lecture 4
81
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Lecture 4

Control Statements (2)

Dr. Hakem Beitollahi

Computer Engineering DepartmentSoran University

Lecture 4

Page 2: Lecture 4

Objectives of this lecture

In this chapter you will learn: Iteration statements (Loop statements) for repetition statement while statement do…while repetition statement break and continue statements

Control Statements (2)— 2

Page 3: Lecture 4

The for loop

Control Statements (2)— 3

Page 4: Lecture 4

General form of the for statement for ( initialization; loopContinuationCondition; increment or decrement)

statement; The initialization is an assignment statement that is used to set the loop

control variable. The condition is a relational expression that determines

when the loop exits. The increment/decrement defines how the loop control

variable changes each time the loop is repeated. You must separate these three major sections by

semicolons. The for loop continues to execute as long as the

condition is true. Once the condition becomes false, program execution

resumes on the statement following the for.

For repetition statement (I)

Control Statements (2)— 4

Page 5: Lecture 4

For repetition statement (II) for statement examples

Vary control variable from 1 to 100 in increments of 1o for ( int i = 1; i <= 100; i++ )

Vary control variable from 100 to 1 in increments of -1o for ( int i = 100; i >= 1; i-- )

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

Vary control variable from 20 to 2 in steps of -2o for ( int i = 20; i >= 2; i -= 2 )

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

Vary control variable over the sequence: 99, 88, 77, 66, 55, 44, 33, 22, 11, 0

o for ( int i = 99; i >= 0; i -= 11 )

Control Statements (2)— 5

Page 6: Lecture 4

Execution Tracefor (int i = 0; i < 3; ++i) {

Console.WriteLine("i is“+i);

}

Console.WriteLine("all done“);

i 0

Control Statements (2)— 6

Page 7: Lecture 4

Execution Tracefor (int i = 0; i < 3; ++i) { Console.WriteLine("i is“+i);

}

Console.WriteLine("all done“);

i 0

Control Statements (2)— 7

Page 8: Lecture 4

Execution Tracefor (int i = 0; i < 3; ++i) {

Console.WriteLine("i is“+i);

}

Console.WriteLine("all done“);

i is 0

i 0

Control Statements (2)— 8

Page 9: Lecture 4

Execution Tracefor (int i = 0; i < 3; ++i) {

Console.WriteLine("i is“+i);

}

Console.WriteLine("all done“);

i is 0

i 0

Control Statements (2)— 9

Page 10: Lecture 4

Execution Tracefor (int i = 0; i < 3; ++i) {

Console.WriteLine("i is“+i);

}

Console.WriteLine("all done“);

i is 0

i 1

Control Statements (2)— 10

Page 11: Lecture 4

Execution Tracefor (int i = 0; i < 3; ++i) {

Console.WriteLine("i is“+i);

}

Console.WriteLine("all done“);

i 1

Control Statements (2)— 11

Page 12: Lecture 4

Execution Tracefor (int i = 0; i < 3; ++i) {

Console.WriteLine("i is“+i);

}

Console.WriteLine("all done“);

i is 0

i is 1

i 1

Control Statements (2)— 12

Page 13: Lecture 4

Execution Tracefor (int i = 0; i < 3; ++i) {

Console.WriteLine("i is“+i);

}

Console.WriteLine("all done“);

i is 0

i is 1

i 1

Control Statements (2)— 13

Page 14: Lecture 4

Execution Tracefor (int i = 0; i < 3; ++i) {

Console.WriteLine("i is“+i);

}

Console.WriteLine("all done“);

i is 0

i is 1

i 2

Control Statements (2)— 14

Page 15: Lecture 4

Execution Tracefor (int i = 0; i < 3; ++i) {

Console.WriteLine("i is“+i);

}

Console.WriteLine("all done“);

i is 0

i is 1

i 2

Control Statements (2)— 15

Page 16: Lecture 4

Execution Tracefor (int i = 0; i < 3; ++i) {

Console.WriteLine("i is“+i);

}

Console.WriteLine("all done“);

i is 0

i is 1

i is 2

i 2

Control Statements (2)— 16

Page 17: Lecture 4

Execution Tracefor (int i = 0; i < 3; ++i) {

Console.WriteLine("i is“+i);

}

Console.WriteLine("all done“);

i is 0

i is 1

i is 2

i 2

Control Statements (2)— 17

Page 18: Lecture 4

Execution Tracefor (int i = 0; i < 3; ++i) {

Console.WriteLine("i is“+i);

}

Console.WriteLine("all done“);

i is 0

i is 1

i is 2

i 3

Control Statements (2)— 18

Page 19: Lecture 4

Execution Tracefor (int i = 0; i < 3; ++i) {

Console.WriteLine("i is“+i);

}

Console.WriteLine("all done“);

i is 0

i is 1

i is 2

i 3

Control Statements (2)— 19

Page 20: Lecture 4

Execution Tracefor (int i = 0; i < 3; ++i) {

Console.WriteLine("i is“+i);

}

Console.WriteLine("all done“);

i is 0

i is 1

i is 2

all done

i 3

1

Control Statements (2)— 20

Page 21: Lecture 4

for repetition statement (III) Example

Control Statements (2)— 21

Page 22: Lecture 4

for repetition statement (IV) In for loops, the conditional test is always

performed at the top of the loop. This means that the code inside the loop

may not be executed at all if the condition is false to begin with.

Control Statements (2)— 22

Page 23: Lecture 4

for repetition statement (V) for Loop Variations

One of the most common variations uses the comma operator to allow two or more variables to control the loop.

Example:

Control Statements (2)— 23

Page 24: Lecture 4

for repetition statement (VI) The Infinite Loop with for

for loop is traditionally used for this purpose Since none of the three expressions that form

the for loop are required, you can make an endless loop by leaving the conditional expression empty:

Control Statements (2)— 24

Page 25: Lecture 4

for repetition statement (VII) You can exit from an infinite using break

statement Example

Control Statements (2)— 25

Page 26: Lecture 4

for repetition statement (VIII) for Loops with no Bodies

A statement may be empty This means that the body of the for loop (or

any other loop) may also be empty. You can use this fact to improve the efficiency

of certain algorithms and to create time delay loops.

Example

for(i=0; i<10000; i++);

Control Statements (2)— 26

Page 27: Lecture 4

Good Programming Practice 5.1

Control counting loops with integer values. Place a blank line before and after each

major control structure to make it stand out in the program.

Control Statements (2)— 27

Page 28: Lecture 4

Common Programming Error 5.1

Floating-point values may be approximate, so controlling counting loops with floating-point variables can result in imprecise counter values and inaccurate tests for termination.

Control Statements (2)— 28

Page 29: Lecture 4

Common Programming Error 5.3

When a for structure declares its control variable in the initialization section of the for structure header, using the control variable after the for structure’s body is a compiler error.

Example:

Control Statements (2)— 29

Page 30: Lecture 4

Common Programming Error 5.4

Using commas in a for structure header instead of the two required semicolons is a syntax error.

Placing a semicolon immediately to the right of a for structure header’s right parenthesis makes the body of that for structure an empty statement. This is normally a logic error.

Control Statements (2)— 30

Page 31: Lecture 4

Other math operations in for portions

The initialization, loop-continuation condition and increment or decrement portions of a for structure can contain arithmetic expressions.

Assume x = 2 and y = 10;

is equivalent to the statement

Control Statements (2)— 31

Page 32: Lecture 4

Common Programming Error 5.6

Not using the proper relational operator in the loop-continuation condition of a loop that counts downward (e.g., using i <= 1 in a loop counting down to 1) is usually a logic error that will yield incorrect results when the program runs.

Control Statements (2)— 32

Page 33: Lecture 4

The while loop

Control Statements (2)— 33

Page 34: Lecture 4

The while loop (I) The second popular loop available in C/C++/C#

is the while loop. The general form is

while(condition)

statement; Example

Control Statements (2)— 34

Page 35: Lecture 4

Example: calculate an average

Control Statements (2)— 35

Page 36: Lecture 4

Execution Trace Suppose input contains: 1 5 3 1 6

listSize 4

Control Statements (2)— 36

Page 37: Lecture 4

Execution Trace Suppose input contains: 1 5 3 1 6

listSize 4

numberProcessed 0

Control Statements (2)— 37

Page 38: Lecture 4

Execution Trace Suppose input contains: 1 5 3 1 6

listSize 4

numberProcessed 0

sum 0

Control Statements (2)— 38

Page 39: Lecture 4

Execution Trace Suppose input contains: 1 5 3 1 6

listSize 4

numberProcessed 0

sum 0

Control Statements (2)— 39

Page 40: Lecture 4

Execution Trace Suppose input contains: 1 5 3 1 6

listSize 4

numberProcessed 0

sum 0

value --

Control Statements (2)— 40

Page 41: Lecture 4

Execution Trace Suppose input contains: 1 5 3 1 6

listSize 4

numberProcessed 0

sum 0

value 1

Control Statements (2)— 41

Page 42: Lecture 4

Execution Trace Suppose input contains: 1 5 3 1 6

listSize 4

42/77

numberProcessed 0

sum 1

value 1

Page 43: Lecture 4

Execution Trace Suppose input contains: 1 5 3 1 6

listSize 4

43/77

numberProcessed 1

sum 1

value 1

Page 44: Lecture 4

Execution Trace Suppose input contains: 1 5 3 1 6

listSize 4

numberProcessed 1

sum 1

value 1

Control Statements (2)— 44

Page 45: Lecture 4

Execution Trace Suppose input contains: 1 5 3 1 6

listSize 4

numberProcessed 1

sum 1

value 1

Control Statements (2)— 45

Page 46: Lecture 4

Execution Trace Suppose input contains: 1 5 3 1 6

listSize 4

numberProcessed 1

sum 1

value 5

Control Statements (2)— 46

Page 47: Lecture 4

Execution Trace Suppose input contains: 1 5 3 1 6

listSize 4

numberProcessed 1

sum 6

value 5

Control Statements (2)— 47

Page 48: Lecture 4

Execution Trace Suppose input contains: 1 5 3 1 6

listSize 4

numberProcessed 2

sum 6

value 5

Control Statements (2)— 48

Page 49: Lecture 4

Execution Trace Suppose input contains: 1 5 3 1 6

listSize 4

numberProcessed 2

sum 6

value 5

Control Statements (2)— 49

Page 50: Lecture 4

Execution Trace Suppose input contains: 1 5 3 1 6

listSize 4

numberProcessed 2

sum 6

value 5

Control Statements (2)— 50

Page 51: Lecture 4

Execution Trace Suppose input contains: 1 5 3 1 6

listSize 4

numberProcessed 2

sum 6

value 3

Control Statements (2)— 51

Page 52: Lecture 4

Execution Trace Suppose input contains: 1 5 3 1 6

listSize 4

numberProcessed 2

sum 9

value 3

Control Statements (2)— 52

Page 53: Lecture 4

Execution Trace Suppose input contains: 1 5 3 1 6

listSize 4

numberProcessed 3

sum 9

value 3

Control Statements (2)— 53

Page 54: Lecture 4

Execution Trace Suppose input contains: 1 5 3 1 6

listSize 4

numberProcessed 3

sum 9

value 3

Control Statements (2)— 54

Page 55: Lecture 4

Execution Trace Suppose input contains: 1 5 3 1 6

listSize 4

numberProcessed 3

sum 9

value 3

Control Statements (2)— 55

Page 56: Lecture 4

Execution Trace Suppose input contains: 1 5 3 1 6

listSize 4

numberProcessed 3

sum 9

value 1

Control Statements (2)— 56

Page 57: Lecture 4

Execution Trace Suppose input contains: 1 5 3 1 6

listSize 4

numberProcessed 3

sum 10

value 1

Control Statements (2)— 57

Page 58: Lecture 4

Execution Trace Suppose input contains: 1 5 3 1 6

listSize 4

numberProcessed 4

sum 10

value 1

Control Statements (2)— 58

Page 59: Lecture 4

Execution Trace Suppose input contains: 1 5 3 1 6

listSize 4

numberProcessed 4

sum 10

value 1

Control Statements (2)— 59

Page 60: Lecture 4

Execution Trace Suppose input contains: 1 5 3 1 6

listSize 4

numberProcessed 4

sum 10

value 1

average 2.5

Control Statements (2)— 60

Page 61: Lecture 4

Execution Trace Suppose input contains: 1 5 3 1 6

listSize 4

numberProcessed 4

sum 10

value 1

average 2.5

Control Statements (2)— 61

Page 62: Lecture 4

Another Example with while loop

Control Statements (2)— 62

Class Math include all mathematic functions. Pow(a,b) = ab

Increment x by 1, which causes x to exceed 10 eventually

Continue looping as long as x’s value is less than or equal to 10

Initialize counter variable x to 1

The condition is true (x=1<=10)

Page 63: Lecture 4

While loop

Control Statements (2)— 63

Page 64: Lecture 4

Common Programming Error

Forgetting initializing the control value of the while loop is a logical error.

Example:

Control Statements (2)— 64

Page 65: Lecture 4

The do-while loop

Control Statements (2)— 65

Page 66: Lecture 4

The do-while loop (I) Unlike for and while loops, which test the loop

condition at the top of the loop, the do-while loop checks its condition at the bottom of the loop

This means that a do-while loop always executes at least once.

The general form of the do-while loop isdo {

statement;

} while(condition);

Control Statements (2)— 66

Page 67: Lecture 4

The do-while loop (II)

Control Statements (2)— 67

do…while loop displays counter’s value before testing for counter’s

Declare and initialize control variable counter

Page 68: Lecture 4

Action

true

false

Expression

Control Statements (2)— 68

The do-while loop (III)

Page 69: Lecture 4

Loop iterations Declaring Variables within Selection and Iteration

Statements it is possible to declare a variable within if, switch, for,

while, do..while loops A variable declared in one of these places has its

scope limited to the block of code controlled by that statement

/* i is local to for loop; j is known outside loop. */int j;for(int i = 0; i<10; i++) j = i * i;

/* i = 10; // *** Error *** -- i not known here! */

Control Statements (2)— 69

Page 70: Lecture 4

Jump Statements

Control Statements (2)— 70

Page 71: Lecture 4

The return statement The break statement The exit() statement The continue statement

Control Statements (2)— 71

Page 72: Lecture 4

The return statement The return statement is used to return from a

function It is categorized as a jump statement because it

causes execution to return (jump back) to the point at which the call to the function was made.

Control Statements (2)— 72

Page 73: Lecture 4

The break Statement (I) Causes immediate exit from control

structure Used in while, for, do…while or switch

statements The break statement has two uses:

You can use it to terminate a case in the switch statement

You can also use it to force immediate termination of a loop, bypassing the normal loop conditional test

Control Statements (2)— 73

Page 74: Lecture 4

Control Statements (2)— 74

prints the numbers 0 through 10 on the screen. Then the loop terminates because breakcauses immediate exit from the loop, overriding the conditional test t<100.

Page 75: Lecture 4

Programming Tip

Programmers often use the break statement in loops in which a special condition can cause immediate termination.

Control Statements (2)— 75

Page 76: Lecture 4

The exit() Function (I)

Control Statements (2)— 76

Page 77: Lecture 4

Control Statements (2)— 77

Page 78: Lecture 4

The continue Statement (I) Instead of forcing termination, however, continue forces

the next iteration of the loop to take place, skipping any code in between. For the for loop, continue causes the conditional test

and increment portions of the loop to execute. For the while and do-while loops, program control

passes to the conditional tests.

Control Statements (2)— 78

Page 79: Lecture 4

Control Statements (2)— 79

Page 80: Lecture 4

Control Statements (2)— 80

Page 81: Lecture 4

Control Statements (2)— 81