Upload
steves2001
View
161
Download
2
Embed Size (px)
DESCRIPTION
Slides on the use of while and do...while in C#
Citation preview
Software DevelopmentSoftware Development Lecture 6: Loop Statements
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.
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
The while statementThe while statement
Flowchart while CommandFlowchart while Command
Example 1Example 1
Exercise 1: add counterExercise 1: add counter
The do … while statementThe do … while statement
The do … while statementThe do … while statement
Flowchart for do … while Flowchart for do … while CommandCommand
Example 2Example 2
Infinite LoopsInfinite Loops
Example 2:infinite loopExample 2:infinite loop
Breaking out of a while Breaking out of a while StatementStatement
if(a == 12)continue;
Example 2: break infinite Example 2: break infinite looploop
Exercise 2: division and Exercise 2: division and modulesmodules
Problem Analysis (Using Problem Analysis (Using while)while)
Pseudocode for Exercise 2Pseudocode for Exercise 2
Flowchart (Using while)Flowchart (Using while)
Code for Exercise 2: Part 1Code for Exercise 2: Part 1
Code for Exercise 2: Part 2Code for Exercise 2: Part 2
Generating Random Generating Random NumbersNumbers