Share
Embed
Annual Pro sale · Save $30
Have presentations of your own?
Share with private links, password protection, and more control over your work.
Transcript
48 slides
-
Instructor: William McDaniel Albritton Slides based on Sams Teach Yourself
ASP.NET 4 in 24 Hours, Complete Starter Kit by Scott Mitchell -
Control Structures Program control is how a program executes
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) -
Data Type Boolean The Boolean data type is used
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) -
Boolean Expression A boolean expression is a combination of
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 -
Conditional Operators Equal (=) or not equal (<>)
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 -
Conditional Operators Greater than (>), greater than or equal
(>=), 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) -
Logical Operators For example code, see the BooleanExamples.aspx and
BooleanExamples.aspx.vb files -
Assignment Do the Part #1 of the assignment that
has to do with Boolean variables and Boolean operators -
If Statements An if statement uses a Boolean expression
to choose which statements to execute Also called “conditional statement”, or “selection statement” -
If Statements Syntax If (Boolean expression) Then 'code
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 -
If Statement Example Syntax If (Boolean expression) Then
'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 -
If Statement Example Syntax If (Boolean expression) Then
'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 -
If Statement Example Syntax If (Boolean expression) Then
'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 -
If Statements Example See IfStatements.aspx and IfStatements.aspx.vb on the
class web page After checking out the example, do Part #2 of today’s assignment that has to do with if-statements -
Boolean Expression A boolean expression is a combination of
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 -
Logical Operators And, Or Evaluated left to right
Returns true or false Operands have to be Boolean -
Logical Operators And Has 2 operands Returns
true if both operands are true, otherwise false X Y X and Y True True True True False False False True False False False False -
AND Operator A AND B People who like
cats AND People who like dogs A (like cats) B (like dogs) -
Logical Operators Or Has 2 operands Returns
true if at least one operands is true, otherwise false X Y X or Y True True True True False True False True True False False False -
OR Operator A OR B People who like
cats OR People who like dogs A (like cats) B (like dogs) -
If Statements Syntax For multiple Boolean expressions, use this
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 -
If Statements Syntax For multiple Boolean expressions, use this
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 -
If-Else Statements Syntax If (Boolean expression) Then 'code
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 -
If-Else Statement Example Variable Message will contain the 1st
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 -
If-Else Statement Example This will determine if a number
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 -
If Statement Example This will compare two numbers to
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 -
If-Else Statement Example See example code at IfStatement2.aspx
IfStatement2.aspx.vb -
Multiple if-else Statements Syntax for multiple selection 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 -
Multiple if-else Statements So how does multiple selection work?
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 -
Multiple if-else Example Dim Letter As String = Input.Text
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 -
Multiple if-else Bug You must have a new line
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 -
Multiple if-else Example This will determine if a number
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 -
Example Code Check out the example code in IfStatements3.aspx
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 -
Control Structures Program control is how a program executes
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) -
Looping A loop causes the computer to execute a
section of code repeatedly One kind of loop structure is a for loop A for loop is used to execute a loop body a specific number of times -
For Loop Syntax for a for loop For
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 -
For Loop How does it work? The code
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 -
For Loop Example The following loop creates a string
that says “Aloha!” five times Dim Message As String = "The message is: " For index = 1 To 5 Message = Message & "Aloha!" Next -
Example Code Check out the following code on the
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 -
Troubleshooting Always use the "&" (concatenation operator) to add
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 -
Example Code Check out the following code on the
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 -
For-loops and Integers We can use for-loops to do
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 -
Example Code See the following examples LoopExamples2.aspx
LoopExamples2.aspx.vb This adds the numbers from 1 to 5 using the Page Load method, and displays the result on the web page -
Assignment Do the 1st half of today’s assignment
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 -
Example Code See the following examples LoopExamples3.aspx
LoopExamples3.aspx.vb This adds the numbers from 1 to the user’s number using the Button Click method, and displays the result on the web page -
Assignment Do the 2nd half of today’s assignment
Make sure that you add some simple formatting to your web page and feedback to the user