Upload
yadid
View
29
Download
1
Embed Size (px)
DESCRIPTION
REPETITION (IN EVERYDAY LIFE). 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.) - PowerPoint PPT Presentation
Citation preview
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
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
CONTROL STRUCTURES
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
DOWHILE examples
I smile WHILE I
am happy.
I yell WHILE I am mad.
I sleep WHILE I am tired.
I rest WHILE I am sick.
We listen WHILE the boss is talking.
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.
COPYRIGHT 2010: Dr. David Scanlan, CSUS REPETITION (DOWHILE) IN EVERYDAY LIFE
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..
STARTPROGRAM
Increment Clock
TRUE
FALSE
EXITPROGRAM
Do WhileClock < 5:00 pm
Set Clock = 8:00 am
DOWHILE Loop
Body of the Loop
Condition Pretest
COPYRIGHT 2010: Dr. David Scanlan, CSUS REPETITION (DOUNTIL) IN EVERYDAY LIFE
DOUNTIL examples
I smile UNTIL I am sad.
I yell UNTIL I
am calm.
I sleep UNTIL I am
rested.
I rest UNTIL I am well.
We listen UNTIL the boss is silent.
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.
COPYRIGHT 2010: Dr. David Scanlan, CSUS REPETITION (DOUNTIL) IN EVERYDAY LIFE
More DOUNTIL examples
I work UNTIL
5:00 pm
I cook UNTIL the food is done
I work UNTIL
the house is finished.
COPYRIGHT 2010: Dr. David Scanlan, CSUS
STARTPROGRAM
Increment Clock
FALSE
TRUE
EXITPROGRAM
Do UntilClock = 5:00 pm
Set Clock = 8:00 am
REPETITION (DOUNTIL) IN EVERYDAY LIFE
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..
DOUNTIL Loop
Body of the Loop
Condition Pretest
I work UNTIL
5:00 pm
COPYRIGHT 2010: Dr. David Scanlan, CSUS REPETITION (DOWHILE vs. DOUNTIL)
Figs_1-140
STARTPROGRAM
ProgrammingStatements
EXITPROGRAM
Test theCondition
Initialize theCondition
STARTPROGRAM
ProgrammingStatements
EXITPROGRAM
Test theCondition
Initialize theCondition
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
TRUE
TRUEFALSE
FALSE
Major Difference
COPYRIGHT 2010: Dr. David Scanlan, CSUS
STARTPROGRAM
ProgrammingStatements
EXITPROGRAM
Test theCondition
Initialize theCondition
REPETITION (Pretest vs. Posttest)Figs_1-140/1-150
STARTPROGRAM
ProgrammingStatements
EXITPROGRAM
Test theCondition
Initialize theCondition
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)
FALSE
Pretest (Before)
FALSE
TRUE
TRUE
Posttest (After)
Major Difference
COPYRIGHT 2010: Dr. David Scanlan, CSUS
STARTPROGRAM
Drive Car
Use Gas
EXITPROGRAM
Gas > Empty
Set GasGuage
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) FALSE
TRUE
I check my gas gauge (Condition) before I start driving (Body of Loop).
DOWHILE (with Pretest)
Condition (Pretest)
Body of loop.
COPYRIGHT 2010: Dr. David Scanlan, CSUS
STARTPROGRAM
Drive Car
Use Gas
EXITPROGRAM
Gas > Empty
Set Gas Gauge
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)
FALSE
TRUE
I check my gas gauge (Condition) after I start driving (Body of Loop).
DOWHILE (with Posttest)
Condition (Posttest)
Body of loop.
COPYRIGHT 2010: Dr. David Scanlan, CSUS FOUR REPETITION LOOPS
STARTPROGRAM
Statements inLoop
EXITPROGRAM
Test LoopCondition
InitializeCondition
STARTPROGRAM
EXITPROGRAM
Test LoopCondition
InitializeCondition
Statements inLoop
DOWHILE condition is TRUE
STARTPROGRAM
Statements inLoop
EXITPROGRAM
Test LoopCondition
InitializeCondition
STARTPROGRAM
EXITPROGRAM
Test LoopCondition
InitializeCondition
Statements inLoop
DOUNTIL condition is TRUE
TRUE
TRUETRUE
TRUE
COPYRIGHT 2010: Dr. David Scanlan, CSUS SOLVE ANY PROBLEM WITH THESE TWO LOOPS
STARTPROGRAM
EXITPROGRAM
Test LoopCondition
InitializeCondition
Statements inLoop
DOWHILE condition is TRUE
STARTPROGRAM
Statements inLoop
EXITPROGRAM
Test LoopCondition
InitializeCondition
DOUNTIL condition is TRUE
TRUE
TRUE
These are the two most common loop variations.Learn them well.
Pretest
Posttest
COPYRIGHT 2010: Dr. David Scanlan, CSUS APPENDEX (FLOWCHART SYMBOLS USED IN THIS COURSE)
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 symbol - This symbol represents an input or output process in an algorithm, such as reading input or writing output.
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 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.
Terminal
Input/Output
Process
Predefined Process
Decision
Preparation Preparation symbol - In this course, use it for initializing loops.
COPYRIGHT 2010: Dr. David Scanlan, CSUS
Flowlines - Connects various symbols in a flowchart, and contain an arrowhead. Use arrowhead when needed for clarity.
APPENDEX (FLOWCHART SYMBOLS USED IN THIS COURSE)
COPYRIGHT 2010: Dr. David Scanlan, CSUS
OBJECTIVES:• To understand “For Loops”• To understand “Do Loops”• To apply DoWhile and DoUntil Control Structure logic to “For Loops”and “Do Loops”.
Repitition – Loops
COPYRIGHT 2010: Dr. David Scanlan, CSUS For Loops
For Loop – GENERAL FORMAT
For LoopCounter = InitialValue To TerminalValue‘Program statement(s)
Next LoopCounter
CODE EXAMPLE:
Dim I As IntegerFor I = 1 To 5
lblDisplay.Text = CStr(I) Next I
RESULT:12345
Start
I = 1
I <= 5
Stop
Display I
I = I + 1
True
False
You can use variables here.
This control structure logic is Do While the condition is True with a Pretest
COPYRIGHT 2010: Dr. David Scanlan, CSUS For Loops (Nested Loops)
For Loop – GENERAL FORMAT
For LoopCounter = InitialValue To TerminalValue‘Program statement(s)
Next LoopCounter
CODE EXAMPLE (Nested Loops):
Dim I As IntegerDim J As IntegerFor J = 1 to 2
For I = 1 To 5lblDisplay.Text = CStr(I)
Next IlblDisplay.Text = CStr(J)
Next J
RESULT:1 2 3 4 5 1 1 2 3 4 5 2
You can use variables here.
These control structures’ logic is Do While the condition is True with a Pretest
Start
I = 1
I <= 5
Stop
Display I
I = I + 1
True
False
J = 1
J <= 2
Display J
J = J + 1
False
True
COPYRIGHT 2010: Dr. David Scanlan, CSUS For Loops
For Loop with Step – GENERAL FORMAT
For LoopCounter = InitialValue To TerminalValue Step StepValue‘Program statement(s)
Next LoopCounter
CODE EXAMPLE:
Dim I As IntegerFor I = 1 To 5 Step 3
lblDisplay.Text = CStr(I) Next I
RESULT:14
Start
I = 1
I <= 5
Stop
Display I
I = I + 3
True
False
You can use variables here.
This control structure logic is Do While the condition is True with a Pretest
Note: If “Step” is not used, the default Step is + 1.
COPYRIGHT 2010: Dr. David Scanlan, CSUS For Loops
CODE EXAMPLE:
Dim I As IntegerFor I = 5 To 1 Step - 1lblDisplay.Text = CStr(I) Next I
RESULT:54321
Start
I = 5
I >= 1
Stop
Display I
I = I - 1
True
False
For Loop with Step – GENERAL FORMAT
For LoopCounter = InitialValue To TerminalValue Step StepValue‘Program statement(s)
Next LoopCounter
You can use variables here.
This control structure logic is Do While the condition is True with a Pretest
COPYRIGHT 2010: Dr. David Scanlan, CSUS Do Loops
CODE EXAMPLE:
Dim I As IntegerI = 1Do While I <= 5lblDisplay.Text = CStr(I)
I = I + 1Loop
RESULT:12345
Do Loop – GENERAL FORMAT
Do While (Condition)‘Program statement(s)
Loop
You can use a variable here.
Start
I = 1
I <= 5
Stop
Display I
I = I + 1
True
False
This control structure logic is Do While the condition is True with a Pretest
COPYRIGHT 2010: Dr. David Scanlan, CSUS Do Loops
CODE EXAMPLE:
Dim I As IntegerI = 0Do
lblDisplay.Text = CStr(I) I = I + 1
Loop Until (I = 5)
RESULT:12345
This control structure logic is Do Until the condition is True with a Posttest.
Do Loop – GENERAL FORMAT
Do‘Program statement(s)
Loop Until (Condition)
You can use a variable here.
Start
I = 1
I = 5
Stop
Display I
I = I + 1
True
False
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