CSC115 Introduction to Computer Programming Zhen Jiang Dept. of Computer Science West Chester University West Chester, PA
Table of Contents Structure Selection (Decision) Loop Array and Function
Price is right. Sample execution (click on this link to try)this link Selection (Decision)
Condition Yes Action 1Action 2 No Action 3
Boolean Expression Yes Action 1 If controlled Action 2 else controlled No Action 3 11/21/20155
If block if condition then action 1 (statements 1) else action 2 (statements 2) end if action 3 (statement 3)
Legal age to have driver’s license
Condition Simple condition Format 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. 5 * 7 >= * (7 - 1) 5 * 7 >= * 6 35 >= >= 33 true Relational operators cannot be chained (unlike math operators) 2 <= x <= 10 error! 11/21/201510
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 problem If then case 1 Else if then case 2 else … 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 integer x = text1.text y=0 if x > 3 then y =1 if x >10 then y = 2 else y = 3 End if Listbox1.Items.Add( y ) Try 2 Dim x as integer, y as integer x = text1.text y=0 if x > 3 then y =1 if x > 10 then y = 2 End if else y = 3 End if Listbox1.Items.Add( y )
Dim x as integer, y as integer x = text1.text y=0 if x > 3 then y =1 if x >10 then y = 2 else y = 3 End if Listbox1.Items.Add( y ) Dim x as integer, y as integer x = text1.text y=0 if x > 3 then y =1 if x > 10 then y = 2 End if else y = 3 End if Listbox1.Items.Add( y )
Select block Select case A constant A variable An expression Is To range Else And and or
Loop Do while Loop do while condition statements loop statements2
Yes statements1Action 2 No Action 3 condition
Yes Statements1 No Statements2 condition
Other loops
Clock
Function and procedure
Array