IS437: Fall 2004 Instructor: Dr. Boris Jukic Program Flow Control: Decisions and Conditions (Branching) Controlled Repetition (Looping)
Structure of computer languages data types (variables & constants) assignment flow control - branching - looping input/output
If-then-else If condition Then …. End If If condition Then …. Else …. End If If condition1 Then …. ElseIf condition2 Then …. Else …. End If
Relational Operators = equality > greater than < less than not equals >= greater than or equal to <= less than or equal to
Logical operators and (both are true) - e.g., a and b or (at least one is true) - e.g., a or b xor (exclusive or, only one is true) - e.g., a xor b not (negation) e.g., (a or b) and (not c)
If (asset > 5000) and (debt <= 5000) then label1.text = “I can pay off my debt” End if Dim taxexempt as Boolean Dim age as integer …. If (taxexempt) xor (age < = 18) then …. ‘code that applies to people are either taxexempt or younger than 18 …. End if Logical operators: examples
Select-Case Statement Dim Name as string …. Select Case Name Case “Fred” label1.text = “Hello Fred” Case “Jane” label1.text = “Hello Jane” Case Else label1.text = “Hello! Anyone Home?” End Select
Dim x as integer = 50 Select Case Integer.Parse(guessNumberTextBox.text) Case Is < 1 label1.text = “Way too low!” Case Is > 100 label1.text = “Way too high!” Case Is > x label1.text = “Too high!” Case Is < x label1.text = “Too low!” Case x label1.text = “You got it!” End Select If more than one case matches the test expression, only the statement block associated with the first matching case will execute. Select-Case Statement
Select Case Choice Case 1, 3, 7 …. Case 2, 4, 5, 6, 8 …. Case Else …. End Select Select-Case Statement
Select Case Choice Case 1 to 3, 7 to 10 …. Case 4 to 6 …. Case Else …. End Select Select-Case Statement
Loops For-Next Loops Do-Loops While-Loops
For - Next Loops For I = 1 to 5 …. Next I For J = 3 to 7 Step 2 …. Next J For I = 10 to 1 Step -2 …. Next I For M = 1 to N …. If x > y then Exit For End if Next M
Nested For-Next Loops For I = x to y …. For J = m to n …. Next J …. Next I For two dimensional data structures
Do-Loops Condition pre-tested Do-While loop Do-Until loop Condition post-tested Do-loop While Do-loop Until
Dim hungry as Boolean …. Do While hungry …. Loop Dim hungry as Boolean …. Do Until Not hungry …. Loop Dim hungry as Boolean …. Do …. Loop While hungry ‘this loop is executed as least once Dim hungry as Boolean …. Do …. Loop Until not hungry ‘this loop is executed as least once Do-Loops: pre-test vs. post-test
Exit-Do Do While hungry ….. if tired then exit do end if …. Loop