one statement at a time from top to bottom within each method We can change the program control by using control structures Most programming languages have 3 different control structures that are used to make choices on what statements to execute 1. If statements (make choices) 2. Loops (repeat) 3. Methods (group related code together)
to model truth values, so the value is true or false Example Boolean statements: Dim FreshSushi As Boolean = True FreshSushi = False Dim ipodIsOn As Boolean = False ipodIsOn = True The Boolean data type is named in honor of British logician George Boole (1815-1864)
operators and operands that returns true or false Used in if statements and loops Evaluated from left to right Operands are what the operators “operate” upon 2 Kinds of Boolean operators 1.Conditional operators: =, <>, >, >=, <, <= 2.Logical operators: and, or
Evaluated left to right Returns true or false Operands can be any data type Example code: Dim EqualStrings As Boolean = ("abc" = "xyz") So EqualStrings is False EqualStrings = ("abc" <> "xyz") So EqualStrings is True
(>=), less than (<), less than or equal (<=) Evaluated left to right Returns true or false Operands can be any data type Example code: Dim Greater As Boolean = (1 > 2) Dim GreaterOrEqual As Boolean = (2 >= 2) Dim Lesser As Boolean = (1 < 2) Dim LesserOrEqual As Boolean = (2 <= 2)
to be executed End If How does it work? First, the Boolean expression is evaluated If the Boolean expression is true, then the body (code between “If(…)Then” and “End If”) executes If the Boolean expression is false, then the body DOES NOT executes
'code to be executed End If Example code Dim Number As Integer = Input.Text If (Number > 0) Then Output.Text = "Your entered a positive number! " End If
'code to be executed End If Example code Dim Number As Integer = Input.Text If (Number < 0) Then Output.Text = "Your entered a negative number! " End If
'code to be executed End If Example code Dim Number1 As Integer = Input1.Text Dim Number2 As Integer = Input2.Text If (Number1 = Number2 ) Then Output.Text = "Your entered two equal numbers! " End If
operators and operands that returns true or false Used in if statements and loops Evaluated from left to right Operands are what the operators “operate” upon 2 Kinds of Boolean operators 1.Conditional operators: =, <>, >, >=, <, <= 2.Logical operators: and, or
pattern: If (expr1) Or (expr2) And (expr3) Then 'code to be executed End If For example Dim Number As Integer = Input.Text If (Number >=10) And (Number<100) Then Output.Text = "Your entered a two-digit number! " End If
pattern: If (expr1) Or (expr2) And (expr3) Then 'code to be executed End If For example Dim Letter As String = Input.Text If (Letter="a") Or (Letter="b") Or (Letter="c") Then Output.Text = "Your entered a, b, or c" End If
to be executed if true Else 'code to be executed if false End If How does it work? If the Boolean expression is true, then only the code in the if statement body executes If Boolean expression is false, then only the code in the else statement body executes
string or the 2nd string, depending on the value for variable Letter Dim Letter As String = Input.Text Dim Message As String = " " If (Letter = "A") Then Message = "You entered the letter A." Else Message = "You did NOT enter the letter A." End If Output.Text = Message
is positive (greater than zero), or zero or negative Dim Number As Integer = Input.Text If (Number > 0) Then Output.Text = "Your number is positive." Else Output.Text = "Your number is zero or negative." End If
see if they are the same or different Dim Number1 As Integer = Input1.Text Dim Number2 As Integer = Input2.Text If (Number1 = Number2 ) Then Output.Text = "Your numbers are equal." Else Output.Text = "Your numbers are NOT equal." End If
(Boolean expression 1) Then 'code to be executed if true ElseIf (Boolean expression 2) Then 'code to be executed if true . . . ElseIf (Boolean expression N) Then 'code to be executed if true Else 'executes only if all false End If
If the first Boolean expression is true, then only the code in the first if statement body executes If the first Boolean expression is false, then the program checks to see if the next if statement is true or false The program keeps going, until it reaches a true Boolean expression, or reaches the last else statement
Dim Message As String = " " If (Letter = "A") Then Message = "You entered the letter A." ElseIf (Letter = "B") Then Message = "You entered the letter B." ElseIf (Letter = "C") Then Message = "You entered the letter C." Else Message = "You did NOT enter A, B or C." End If Output.Text = Message
after “Then” You need to put a new line between the words in bold If (Letter = "A") Then Message = "You entered the letter A." ElseIf (Letter = "B") Then Message = "You entered the letter B." Else Message = "You did NOT enter the letter A or B." End If
is positive (greater than zero), or zero, or negative Dim Number As Integer = Input.Text If (Number > 0) Then Output.Text = "Your number is positive." ElseIf (Number = 0) Then Output.Text = "Your number is zero." Else Output.Text = "Your number is negative." End If
and IfStatements3.aspx.vb After checking out the example, do today’s assignment using if statements and/or if-else statements and/or multiple if-else statements
one statement at a time from top to bottom within each method We can change the program control by using control structures Most programming languages have 3 different control structures that are used to make choices on what statements to execute 1. If statements (make choices) 2. Loops (repeat) 3. Methods (group related code together)
index = Begin To End 'loop body Next Where Begin is an Integer that is the first number in the loop Where End is an Integer that is the last number in the loop Where index is an Integer that changes in value from Begin to End, incremented by one (1) for each loop
inside the loop body (the part between “For index As Integer = Begin To End” to “Next”) is executed End-Begin+1 number of times Note that Begin has to be less than End
web page This code will repeat the same message five times This example uses the Page Load method (NOT the Button Click method) LoopExamples.aspx LoopExamples.aspx.vb
to the end of a String Always use the "+" (plus sign) to add an Integer value (or Decimal value) to an Integer (or Decimal) Dim Word As String = "This" Dim Number As Integer = 0 Word = Word & " is " & Number 'good code Word = Word & " a " + Number 'bad code Number = Number + 1 + 2 'good code Number = Number + 1 & 2 'bad code
web page This code will repeat the same message five times This example uses the Button Click method (NOT the Page Load method) TheCount.aspx TheCount.aspx.vb
mathematical calculations As a general rule, you need the following in your program 1. Before the for-loop, create an Integer variable to store the results of your calculations (for example, a “results” variable) 2. Inside the for-loop, use the “results” variable to make the calculations 3. After the for-loop, use the Label.Text and the “results” variable to display the results on your web page
Make sure that you add some simple formatting to your web page and feedback to the user We will do the 2nd part of the assignment today in class after the next example