Lec 2 Flashcards
Poor design disadvantages
1.Duplicatedd code make program unnecessarily large
2.Writng long sequences of statements is time consuming
3.If part of the duplicated code needs to be changed, this affects many other parts of the code
Good code
1.Would be to write a segment of code once and to place that segment of code inside of a repetition structure loop
repetition structure loop
Have the computer repeat a segment of code as many times as needed commonly known as a loop
Their are 2 main types of loops
Condition-controlled
Count -controlled
Condition-controlled loop
Condition-controlled loop will execute statements of code based on whether a conditions is met
3 types of condition controlled loop
3 types
-While
-DO-while
-Do Until
While loops and Do-While
While loops and Do-While loops will repeat as long as a condition is TRUE, meaning that the program exit the loop
once the condition is FALSE
Do-Until loops
. On the other hand, Do-Until loops will repeat until a
condition is TRUE, meaning that the program stay in the loop as long as the condition is FALSE.
What is a pretest loop
While Loop: “While a condition is TRUE, execute these statement”
A While loop consists of: (1) a condition that is evaluated as TRUE or FALSE, and (2) One or more statements that
will be repeated while the condition is TRUE. A While loop is also called a pretests loop
because the condition is evaluated before each iteration of the loop.
Example 1:Write a program that prints all the odd integers from 1 to 11
Declare Integer counter and max value
Set counter=1
Set Maxvalue=11
While counter <maxvlaues
Display counter
Set counter=counter+2
End while
Do while
o-Until Loop (Gaddis 5.2): “Do this task UNTIL this condition is TRUE”
OR another way to say this is: do while a condition is false
A Do-Until loop consists of: (1) One or more statements that will be repeated while the condition is FALSE and (2) a
condition that is evaluated as TRUE or FALSE. A Do-Until loop is also called a post tests
Do
set
Display call
Until
.
Write a program that will ask the user to enter a value and use a loop to calculate a running total. Each time the loop
iterates, it should prompt the user if they want to continue, only showing the final total when they say ‘no’
Declare
Set total =0
Do
Display “Enter
Set total=total+num
Display”do you wat to continue”
Input Choice
While choice is “y”
Display “the total is”,total
Condtion-Cointrolled loop summary
2 parts
-some Boolean expression evaluates as true or false
-statements of code executed within a loop
-Can be post test of pretest
Types
While(Pretests)- in loop when true exit when false
Do While(post)-In loop
Do-Until(Post)-in loop is false and exits when true
Counter controlled loop summary
(LOOK AT)
Counter controlled loop will execute a ststatement of code a speific numbers of iterations For Loop, a major type is (blank)
Certain of
Counter controlled loop major parts(For Loop)
1.Initailization Counter variable is initialized to a starting value
2.Test the counter variable and compute it to a maximum value(End value)
3.Increment step:After each iteration, the counter is increments.The default increament is +1 but we use step as the increment vlaue
It is a pretets loop