22
Software Development Software Development Lecture 6: Loop Statements

CMP1001 Loops 1

Embed Size (px)

DESCRIPTION

Slides on the use of while and do...while in C#

Citation preview

Page 1: CMP1001 Loops 1

Software DevelopmentSoftware Development Lecture 6: Loop Statements

Page 2: CMP1001 Loops 1

ObjectivesObjectivesWrite blocks of code that can be

repeatedIdentify how control the amount

of repetitionsIdentify how to exit a repeating

section of codeUse simple string manipulation

code.

Page 3: CMP1001 Loops 1

Repeating Code (Loops)Repeating Code (Loops)Loop statements allow us to execute

a code multiple timesLike conditional statements, they are

controlled by Boolean expressionsC# has four kinds of loop statements:

◦while

◦do

◦ for

◦ foreach

Page 4: CMP1001 Loops 1

The while statementThe while statement

Page 5: CMP1001 Loops 1

Flowchart while CommandFlowchart while Command

Page 6: CMP1001 Loops 1

Example 1Example 1

Page 7: CMP1001 Loops 1

Exercise 1: add counterExercise 1: add counter

Page 8: CMP1001 Loops 1

The do … while statementThe do … while statement

Page 9: CMP1001 Loops 1

The do … while statementThe do … while statement

Page 10: CMP1001 Loops 1

Flowchart for do … while Flowchart for do … while CommandCommand

Page 11: CMP1001 Loops 1

Example 2Example 2

Page 12: CMP1001 Loops 1

Infinite LoopsInfinite Loops

Page 13: CMP1001 Loops 1

Example 2:infinite loopExample 2:infinite loop

Page 14: CMP1001 Loops 1

Breaking out of a while Breaking out of a while StatementStatement

if(a == 12)continue;

Page 15: CMP1001 Loops 1

Example 2: break infinite Example 2: break infinite looploop

Page 16: CMP1001 Loops 1

Exercise 2: division and Exercise 2: division and modulesmodules

Page 17: CMP1001 Loops 1

Problem Analysis (Using Problem Analysis (Using while)while)

Page 18: CMP1001 Loops 1

Pseudocode for Exercise 2Pseudocode for Exercise 2

Page 19: CMP1001 Loops 1

Flowchart (Using while)Flowchart (Using while)

Page 20: CMP1001 Loops 1

Code for Exercise 2: Part 1Code for Exercise 2: Part 1

Page 21: CMP1001 Loops 1

Code for Exercise 2: Part 2Code for Exercise 2: Part 2

Page 22: CMP1001 Loops 1

Generating Random Generating Random NumbersNumbers