Post on 25-Feb-2016
description
CSC115 Introduction to Computer Programming
Zhen JiangDept. of Computer Science
West Chester UniversityWest Chester, PA 19383
zjiang@wcupa.edu
Table of Contents Structure
Selection (Decision) Loop Array and Function
Price is right. Sample execution (click on this link to
try)
Selection (Decision)
ConditionYes
Action 1 Action 2
No
Action 3
Boolean Expression
Yes
Action 1If controlled
Action 2else controlled
No
Action 3
04/22/23 5
If block if condition then
action 1 (statements 1) elseaction 2 (statements 2) end if
action 3 (statement 3)
Legal age to have driver’s license
Condition Simple condition
Format <Value> <relational operators> <Value>
Number value relational operators =, <>, <, >, <=, >=
String value relational operators=, <>
Complex condition And, or, not Truth table, table 4, p121
Relational operators have lower precedence than math operators.
(7 - 1) * 5 + 3 > 7* 5 6 * 5 + 3 > 35 30 + 3 > 35
33 > 35 False
Relational operators cannot be chained (unlike math operators)
2 <= x <= 10error!
04/22/23 9
Identify two exclusive options Implement each handling in different action parts Identify the situation (values) for option selection Make a condition so that all the situation value for
option part 1 will lead to this condition true. Verify all the situation value for option part 2 will lead
to this condition false, otherwise, revise the above condition!
Development Process
Multiple selection Nested if block Example: letter grade
Comments: Nested if block for multiple section
problemIf then
case 1Else
if thencase 2else…end if
End if
If block, extension without else if condition then
action 1 (statements 1)
end if action 3 (statement 3)
Dim x as integer, y as integerx = text1.texty=0if x > 3 then
y =1if x >10 then
y = 2else
y = 3End ifEnd ifListbox1.Items.Add( y )
Are they different, how much?
Dim x as integer, y as integerx = text1.texty=0if x > 3 then
y =1if x > 10 then
y = 2End ifelse
y = 3End ifListbox1.Items.Add( y )
Dim x as integer, y as integerx = text1.texty=0if x > 3 then
y =1if x >10 then
y = 2else
y = 3End if
End ifListbox1.Items.Add( y )
Dim x as integer, y as integerx = text1.texty=0if x > 3 then
y =1if x > 10 then
y = 2End if
elsey = 3
End ifListbox1.Items.Add( y )
Try 0, 4, 11 and see the results!
Dim x as integer, y as integerx = text1.texty=0if x > 3 then
y =1if x <10 then
y = 2else
y = 3End if
End ifListbox1.Items.Add( y )
Dim x as integer, y as integerx = text1.texty=0if x > 3 then
y =1if x < 10 then
y = 2End if
elsey = 3
End ifListbox1.Items.Add( y )
Try 0, 4, 11 and see a worse situation!
My point One line in wrong place
Could create the program 99.99% different Structural procedure
Ask yourself where (the next program segment line) the computer execution goes
Before that, ask yourself if you can find the “if” for each “else”
Ex 6
Select block Select case
A constant A variable An expression Is To range Else And and or
Loop Do while Loop
do while conditionstatements
loop statements2
Yes
statements1 Action 2
No
Action 3
condition
Yes
Statements1
No
Statements2
condition
Other loops
Clock
Function and procedure
Array