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
Exmaple of step and For loop-Wraps back around
For counter variable
Delcare
Set Counter =Startvalue
Start value to max value Step 1
Stament
Statment
End For
pretets vs post tests
Pretest-loop at diamond
post test-loop after diamond
Calculating running total 5 number
Delcare
Set Counter=1
Set total=o
For counter=1 to 5 Step 1(Flow chart counter<=5)
Display “Enter num
Input num
Set Counter=Counter+1
Set total=Total+num
End For
Displat total
The difference between while and For Loops
Displat number of 5 of interval 0 to 100
Review From PROG-LEC-07
* While Loops (Condition-Controlled)
* Will iterate or run indefinitely until some condition is met
* Use a While loop if it is not clear how many times a loop will need to run
* Key “clues”
* “repeat until …”
* “only stopping when…”
* “ask the user to continue/stop…”
For Loops (Counter-Controlled)
* Will iterate or run a specified and predictable number of times
* Use For loops if you have a clear start, increment, and end point
* Key “clues”
* “For each value”
* Clear start and endpoint: “From 0 to 20 in increments of 5
While Loop
Set-number=0
While numver<100
Set Number=number+5
Display numbers
End While
For Loop
Declare INterger counter
Set COunet =0
For COunter=0 to 100 Step 5
Display COunter
End For
Array
A array is a type of a variable that allows you to store groups of numbers or groups of strings togther.Arrays can hold mutiples variables at once, which is useful instroing list of infromation
-Arrays must be the same data type(can’t mix number and string/text in one array
-to create an arrays you must give it a name ad string
Size of array
The sixe of an arrys is to let you know how many items are in a list .An important note when working with arrays in pseudocode, you must have a square brackets
Elements
Items within an arrys are stored in their own location, this is called elements.
Index
The positions of an elements is called its index
-Highlevel programming index arrays start at 0 but mat lab starts at 1
if you delcare number[5], you have elemnts 0,1,2,3,4
Why does the For loop use”For Index=0 to 2 instead of For index= 0 to three
the start occurs at 0
Why does the loop use (Index+1) instead of empolyee number
(LOOK AT)
because you
Sequential Search of an arrsy
- Use For-loop to go through variables of each elements in an arrays to find what you are looking for
2.You need a sperate variable to indicate when you have found what you are looking for within your array
3.Use compound Boolean expression and decision structures to compare between information stored in each elements of an array and the information you want to find.
One dimension vs two dimensation arrays
One dimensional array-holds one set of data.For example they can hold one set of strings(names, words, letters)or one set of numbers.
Two dimensional array can hold multiple sets of data. Declaring and creating a two dimensional array is very similar but now programmer need to use 2 sizes.The two values indicate the number of rwos and the number f columns in a an array
Declare row and column
Display enter row
Input row
Display enter column
Input Column
Declare integer exam [row][column]
Set exam[3][1]