Upgrade to Pro — share decks privately, control downloads, hide ads and more …

ics151-hour06

 ics151-hour06

Avatar for William Albritton

William Albritton

January 08, 2016
Tweet

More Decks by William Albritton

Other Decks in Technology

Transcript

  1. Instructor: William McDaniel Albritton Slides based on Sams Teach Yourself

    ASP.NET 4 in 24 Hours, Complete Starter Kit by Scott Mitchell
  2. 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)
  3. 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)
  4. 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
  5. 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
  6. 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)
  7. Assignment  Do the Part #1 of the assignment that

    has to do with Boolean variables and Boolean operators
  8. If Statements  An if statement uses a Boolean expression

    to choose which statements to execute  Also called “conditional statement”, or “selection statement”
  9. 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
  10. 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
  11. 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
  12. 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
  13. 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
  14. 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
  15. Logical Operators  And, Or  Evaluated left to right

     Returns true or false  Operands have to be Boolean
  16. 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
  17. AND Operator  A AND B  People who like

    cats AND People who like dogs A (like cats) B (like dogs)
  18. 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
  19. OR Operator  A OR B  People who like

    cats OR People who like dogs A (like cats) B (like dogs)
  20. 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
  21. 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
  22. 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
  23. 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
  24. 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
  25. 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
  26. 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
  27. 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
  28. 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
  29. 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
  30. 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
  31. 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
  32. 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)
  33. 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
  34. 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
  35. 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
  36. 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
  37. 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
  38. 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
  39. 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
  40. 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
  41. 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
  42. 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
  43. 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
  44. 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