Download For Loop

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Structured programming wikipedia , lookup

Monitor (synchronization) wikipedia , lookup

For loop wikipedia , lookup

Transcript
COPYRIGHT 2010: Dr. David Scanlan, CSUS
REPETITION
(IN EVERYDAY LIFE)
COPYRIGHT 2010: Dr. David Scanlan, CSUS
CONTROL STRUCTURES
OBJECTIVES
• To FULLY understand the following control structures:
•REPEPITION
•SELECTION (To be covered in another set of slices.)
SEQUENCE (To be covered in another set of slices.)
• To see how control structures are used every second in our
daily lives.
• To understand that a FULL comprehension of these control
structures is the KEY to problem solving when writing computer
programs.
IMPORTANT: You MUST learn these control structures PERFECTLY in
order to succeed in this course. These are the most important
concepts the course.
COPYRIGHT 2010: Dr. David Scanlan, CSUS
CONTROL STRUCTURES
Three Control Structures:
1. Repetition (Also called Iteration or Looping)
• DO-WHILE
• DO-UNTIL
2. Selection (Also called Decision)
• IF-THEN-ELSE
• CASE
3. Sequence
IMPORTANT: You MUST learn these control structures PERFECTLY in order to succeed in
this course. You can't solve computer programming problems without control structures.
These are the most important concepts the course. Do this throughout the course.
COPYRIGHT 2010: Dr. David Scanlan, CSUS
REPETITION (DOWHILE) IN EVERYDAY LIFE
Everyday Life:
• Every second, we are engaged in one or more control structures.
• DOWHILE
• DO something WHILE a certain condition is TRUE.
• All MIS 15 students MUST identify 25 situations where they are using
the DOWHILE control structure in their everyday life before the next
class. Do this throughout the course.
DOWHILE examples
I smile
WHILE I
am happy.
We listen WHILE the
boss is talking.
I yell
WHILE I
am mad.
I sleep
WHILE I
am tired.
I rest WHILE
I am sick.
COPYRIGHT 2010: Dr. David Scanlan, CSUS
REPETITION (DOWHILE) IN EVERYDAY LIFE
START
PROGRAM
Set Clock = 8:00 am
WHILE time is
less than 5:00 pm,
continue working.
DOWHILE CONTROL STRUCTURE
• DO the loop WHILE the condition is TRUE.
• With pretest:
• Condition is tested BEFORE the body of
the loop is executed.
• With posttest:
• Condition is tested AFTER the body of
the loop is executed.
• The red symbols and lines make up the
D0WHILE control structure below.
• In Visual Basic.NET there is a loop called
DOWHILE and it functions exactly as
this one..
Condition Pretest
Do While
Clock < 5:00 pm
TRUE
DOWHILE
Loop
FALSE
Increment Clock
EXIT
PROGRAM
Body of the Loop
COPYRIGHT 2010: Dr. David Scanlan, CSUS
REPETITION (DOUNTIL) IN EVERYDAY LIFE
Everyday Life:
• Every second, we are engaged in one or more control structures.
• DOUNTIL
• DO something UNTIL a certain condition is TRUE.
• All MIS 15 students MUST identify 25 situations where they are using
the DOUNTIL control structure in their everyday before the next
class.
DOUNTIL examples
We listen UNTIL the
boss is silent.
I smile
UNTIL I
am sad.
I yell
UNTIL I
am calm.
I sleep
UNTIL I am
rested.
I rest UNTIL
I am well.
COPYRIGHT 2010: Dr. David Scanlan, CSUS
REPETITION (DOUNTIL) IN EVERYDAY LIFE
More DOUNTIL examples
I cook UNTIL the
food is done
I work
UNTIL
5:00 pm
I work
UNTIL
the house
is finished.
COPYRIGHT 2010: Dr. David Scanlan, CSUS
REPETITION (DOUNTIL) IN EVERYDAY LIFE
START
PROGRAM
I work
UNTIL
5:00 pm
Set Clock = 8:00 am
DOUNTIL CONTROL STRUCTURE
• DO the loop UNTIL the condition is TRUE.
• With pretest:
• Condition is tested BEFORE the body of
the loop is executed.
• With posttest:
• Condition is tested AFTER the body of
the loop is executed.
• The red symbols and lines make up the
D0UNTIL control structure below.
• In Visual Basic.NET there is a loop called
DOUNTIL and it functions exactly as
this one..
Condition Pretest
Do Until
Clock = 5:00 pm
FALSE
DOUNTIL
Loop
TRUE
Increment Clock
EXIT
PROGRAM
Body of the Loop
REPETITION (DOWHILE vs. DOUNTIL)
COPYRIGHT 2010: Dr. David Scanlan, CSUS
DIFFERENCE BETWEEN DOWHILE AND DOUNTIL
• DOWHILE: DO the body of the loop WHILE the condition is TRUE.
• If the condition is TRUE, the body of the loop is executed.
• DOUNTIL: DO the body of the loop UNTIL the condition is TRUE.
• If the condition is FALSE, the body of the loop is executed.
DOWHILE
DOUNTIL
START
PROGRAM
START
PROGRAM
Initialize the
Condition
Major Difference
Initialize the
Condition
TRUE
Test the
Condition
Test the
Condition
FALSE
FALSE
Programming
Statements
EXIT
PROGRAM
TRUE
EXIT
PROGRAM
Programming
Statements
Figs_1-140
REPETITION (Pretest vs. Posttest)
COPYRIGHT 2010: Dr. David Scanlan, CSUS
Figs_1-140/1-150
DIFFERENCE BETWEEN DOWHILE PRETEST AND POSTEST
• PRETEST: The BODY of the loop is executed AFTER the condition is checked.
• POSTTEST: The BODY of the loop is executed BEFORE the condition is checked.
• IMPORTANT: The Posttest GUARANTEES that the body of the loop will be executed
at least ONCE.
DOWHILE (with Pretest)
DOWHILE (with Posttest)
START
PROGRAM
START
PROGRAM
Major Difference
Initialize the
Condition
Pretest (Before)
Test the
Condition
Initialize the
Condition
Programming
Statements
TRUE
FALSE
Programming
Statements
Test the
Condition
FALSE
EXIT
PROGRAM
EXIT
PROGRAM
TRUE
Posttest (After)
COPYRIGHT 2010: Dr. David Scanlan, CSUS
REPETITION (Pretest vs. Posttest)
Figs_1-140/1-150
DOWHILE CONDITION IS TRUE WITH PRETEST
• PRETEST: The BODY of the loop is executed AFTER the condition is checked.
DOWHILE (with Pretest)
START
PROGRAM
I check my gas gauge
(Condition) before I start
driving (Body of Loop).
Set Gas
Guage
Condition (Pretest)
TRUE
Gas > Empty
DOWHILE (with Pretest)
FALSE
Drive Car
Use Gas
EXIT
PROGRAM
Body of loop.
COPYRIGHT 2010: Dr. David Scanlan, CSUS
REPETITION (Pretest vs. Posttest)
Figs_1-140/1-150
DOWHILE CONDITION IS TRUE WITH POSTTEST
• POSTTEST: The BODY of the loop is executed AFTER the condition is checked.
DOWHILE (with Posttest)
START
PROGRAM
I check my gas gauge
(Condition) after I start
driving (Body of Loop).
Set Gas
Gauge
Body of loop.
Drive Car
Use Gas
Condition (Posttest)
DOWHILE (with Posttest)
Gas > Empty
FALSE
EXIT
PROGRAM
TRUE
COPYRIGHT 2010: Dr. David Scanlan, CSUS
FOUR REPETITION LOOPS
DOUNTIL condition is TRUE
DOWHILE condition is TRUE
START
PROGRAM
START
PROGRAM
Initialize
Condition
Initialize
Condition
START
PROGRAM
START
PROGRAM
Initialize
Condition
Initialize
Condition
Statements in
Loop
Test Loop
Condition
Statements in
Loop
TRUE
Test Loop
Condition
Test Loop
Condition
Statements in
Loop
TRUE
TRUE
Test Loop
Condition
Statements in
Loop
TRUE
EXIT
PROGRAM
EXIT
PROGRAM
EXIT
PROGRAM
EXIT
PROGRAM
COPYRIGHT 2010: Dr. David Scanlan, CSUS
SOLVE ANY PROBLEM WITH THESE TWO LOOPS
These are the two most common loop variations.
Learn them well.
DOWHILE condition is TRUE
DOUNTIL condition is TRUE
START
PROGRAM
START
PROGRAM
Initialize
Condition
Initialize
Condition
Statements in
Loop
Pretest
Test Loop
Condition
TRUE
Posttest
Test Loop
Condition
Statements in
Loop
TRUE
EXIT
PROGRAM
EXIT
PROGRAM
APPENDEX (FLOWCHART SYMBOLS USED IN THIS COURSE)
COPYRIGHT 2010: Dr. David Scanlan, CSUS
Terminal
Terminal symbol - This symbol indicates the starting or stopping point in the logic. Every
flowchart should begin and end with a terminal symbol.
Input/Output
Input/Output symbol - This symbol represents an input or output process in an algorithm, such
as reading input or writing output.
Process
Process symbol - Represents any single process in an algorithm, such as assigning a value or
performing a calculation. The flow of control is sequential.
Predefined
Process
Decision
Preparation
Predefined process symbol - Represents a module in an algorithm; that is, a predefined
process that has its own flowchart, and code.
Decision symbol - Represent a decision in the logic involving the comparison of two values.
Alternative paths are followed depending on whether the decision symbol is true or false.
Preparation symbol - In this course, use it for initializing loops.
APPENDEX (FLOWCHART SYMBOLS USED IN THIS COURSE)
COPYRIGHT 2010: Dr. David Scanlan, CSUS
Flowlines - Connects various symbols in a flowchart, and contain an arrowhead. Use
arrowhead when needed for clarity.
COPYRIGHT 2010: Dr. David Scanlan, CSUS
Repitition – Loops
OBJECTIVES:
• To understand “For Loops”
• To understand “Do Loops”
• To apply DoWhile and DoUntil Control Structure logic to “For Loops” and
“Do Loops”.
COPYRIGHT 2010: Dr. David Scanlan, CSUS
For Loop – GENERAL FORMAT
For Loops
You can use variables here.
For LoopCounter = InitialValue To TerminalValue
‘Program statement(s)
Next LoopCounter
Start
I=1
CODE EXAMPLE:
Dim I As Integer
For I = 1 To 5
lblDisplay.Text = CStr(I)
Next I
RESULT:
1
2
3
4
5
I <= 5
This control structure
logic is Do While the
condition is True with
a Pretest
True
False
Display I
I=I+1
Stop
For Loops (Nested Loops)
COPYRIGHT 2010: Dr. David Scanlan, CSUS
For Loop – GENERAL FORMAT
You can use variables here.
For LoopCounter = InitialValue To TerminalValue
‘Program statement(s)
Next LoopCounter
These control
structures’ logic is Do
While the condition is
True with a Pretest
Start
J=1
CODE EXAMPLE (Nested Loops):
Dim I As Integer
Dim J As Integer
For J = 1 to 2
For I = 1 To 5
lblDisplay.Text = CStr(I)
Next I
lblDisplay.Text = CStr(J)
Next J
J <= 2
True
False
I=1
I <= 5
True
False
RESULT:
1 2 3 4
Display I
5
1
1
2
3
4
5
2
Display J
I=I+1
J=J+1
Stop
COPYRIGHT 2010: Dr. David Scanlan, CSUS
For Loops
For Loop with Step – GENERAL FORMAT
You can use variables here.
For LoopCounter = InitialValue To TerminalValue Step StepValue
‘Program statement(s)
Start
Next LoopCounter
I=1
CODE EXAMPLE:
Dim I As Integer
For I = 1 To 5 Step 3
lblDisplay.Text = CStr(I)
Next I
RESULT:
1
4
I <= 5
This control structure
logic is Do While the
condition is True with
a Pretest
True
False
Display I
I=I+3
Note: If “Step” is not used, the default Step is + 1.
Stop
COPYRIGHT 2010: Dr. David Scanlan, CSUS
For Loop with Step – GENERAL FORMAT
For Loops
You can use variables here.
For LoopCounter = InitialValue To TerminalValue Step StepValue
‘Program statement(s)
Start
Next LoopCounter
I=5
CODE EXAMPLE:
Dim I As Integer
For I = 5 To 1 Step - 1
lblDisplay.Text = CStr(I)
Next I
RESULT:
5
4
3
2
1
I >= 1
This control structure
logic is Do While the
condition is True with
a Pretest
True
False
Display I
I=I-1
Stop
COPYRIGHT 2010: Dr. David Scanlan, CSUS
Do Loops
Do Loop – GENERAL FORMAT
Do While (Condition)
‘Program statement(s)
Loop
You can use a variable here.
Start
I=1
This control structure
logic is Do While the
condition is True with
a Pretest
CODE EXAMPLE:
Dim I As Integer
I=1
Do While I <= 5
lblDisplay.Text = CStr(I)
I=I+1
Loop
RESULT:
1
2
3
4
5
I <= 5
True
False
Display I
I=I +1
Stop
COPYRIGHT 2010: Dr. David Scanlan, CSUS
Do Loops
Do Loop – GENERAL FORMAT
Start
Do
‘Program statement(s)
Loop Until (Condition)
I=1
This control structure
logic is Do Until the
condition is True with a
Posttest.
CODE EXAMPLE:
Dim I As Integer
I=0
Do
lblDisplay.Text = CStr(I)
I=I+1
Loop Until (I = 5)
RESULT:
1
2
3
4
5
Display I
I=I +1
False
You can use a variable here.
I = 5
True
Stop
Note: A Do Until
control structure
with a Posttest
guarantees that
the loop body
will always be
executed at
least once.
COPYRIGHT 2010: Dr. David Scanlan, CSUS
You must know the previous loop
slides extremely well.
These loops are fundamental to
problem solving in programming.
With these fundamental loop
structures, you can solve any
algorithm requiring repetition.
COPYRIGHT 2010: Dr. David Scanlan, CSUS
REPETITION IN EVERYDAY LIFE