Download presentation
Presentation is loading. Please wait.
1
Control Structures: Getting Started Sequence and Selection also arithmetic operators, data types, logical operators
2
Introduction Structured Programming: techniques that bring clarity in developing and modifying programs. Algorithms: Set of instructions and the order in which they are to be executed (e.g. shower dressed)
3
Pseudocode An almost English language that helps programmers to develop algorithms and plan code Used to describe executable statements Executable statements are actions Non-executable statements include variable declarations and comments E.g. Assign 0 to counter vs. counter=0
4
Arithmetic operators Most of you know (+, -, *, / is floating point division 7 / 4 = 1.75 \ is integer division 7 \ 4 = 1 Mod is modulus ^ is exponent Rules of operator precedence (also called order of operations): ( ), ^, pos and neg, * and /, \, mod, +, -
5
Common primitive data types Boolean Byte Date Decimal String Integer Single Long Char Double
6
Control Structures A payroll company calculates the gross earnings per week of employees. Employees’ weekly salaries are based on the number of hours they worked and their hourly wages. For the first 40 hours, employees earn their hourly rate Anything above 40, employees earn 1.5* their hourly rate overtTime pay = (HoursWorked-40) * (1.5*HourlyRate)
7
Control Structures Sequence, Selection, Repetition (Iteration) Sequence –E.g. A B C D –A transfer of control occurs when an executed statement does not directly follow the previous statement
8
Control Structures Selection (IF…Then, IF…Then…Else, Select case) –If (income>expenses) THEN buy ipod ELSE wait –Returns Boolean data type (True/False) Iteration (or repetition) –While…End While, Do While…Loop, Do…Loop While, Do Until…Loop, Do…Loop Until, For…Next, and For Each…Next
9
Operators In order to evaluate a condition for a selection statement need relational and equality operators > = += -= *= /= \= ^= Control structures can be nested and stacked.
10
Selection If-then, If-then-Else, Case Select
11
Nested if then else pseudocode If student’s grade is greater than or equal to 90 Display “A” Else If student’s grade is greater than or equal to 80 Display “B” Else If student’s grade is greater than or equal to 70 Display “C” Else If student’s grade is greater than or equal to 60 Display “D” Else Display “F”
12
Back to Salary Problem Problem: Input (Hours/wages) Calculate (salary) Display (earnings) Pseudocode: -Get hours worked and hourly wages from the textbox -If hours worked are less than or equal to 40 Gross earnings equals hours worked times hourly wages -Else Gross earnings equals 40 times hourly wage plus hours above 40 times wage and a half -Display gross earnings
13
Dim wage, gross As Decimal Dim hours As Double Const HOUR_LIMIT As Integer = 40 If hours <= HOUR_LIMIT Then gross = wage * hours Else gross = HOUR_LIMIT * wage gross += (hours - HOUR_LIMIT) * wage * 1.5 End If
14
Another If-then Example Dental Office Billing Application Calculate charge based on selected procedures Calculate the total charges
15
How such an application may look
16
Dental Payment Pseudocode When user clicks calculate If “Cleaning” Checkbox is selected Add cost of a cleaning to total If “Cavity filling” Checkbox is selected Add cost of receiving filling to the total If “X-Ray” Checkbox is selected Add cost of receiving X-Ray to the total Display total
17
Dental Payment Partial Code If cleanCheckBox.Checked = True Then total += 35 End If If cavityCheckBox.Checked = True Then total += 150 End If If xrayCheckBox.Checked = True Then total += 85 End If
18
Logical Operators AndAlso = both conditions must be true OrElse = either or both are true Xor = one condition must be true and the other must be false
19
Logical Operators If gender = Female AndAlso age>= 65 then seniorFemales +=1 Adds one if individual is both female and >= 65 If gender = Female OrElse age>= 65 then seniorFemales +=1 Adds 1 if the individual female, if he or she is >= 65 or both
20
Logical Operators If gender = Female Xor age>= 65 then seniorFemales +=1 Would add 1 if individual if Individual is male but >=65 Individual is female but < 65 Would not add one if both conditions were true or both were false
21
Logical Operators Not = enables the programmer to reverse the meaning of a condition If not (grade = value) then… If (grade <> value) Don’t use this unless you really have to…it can be very confusing to follow…
22
Addition assignment operator Count += 1 Same as: count = count + 1 You may use either Also Count -= 1 decrements the value stored in count by 1
23
Another Selection option: Select Case Useful when there are many different options – it is a multiple selection statement Can take the place of layers of nested or stacked ifs. This is much preferred over too many nested ifs (more than 3 layers) The expression following the keywords Select Case is called the controlling expression
24
Case Statements Conditions are checked in the case statements Multiple values tested in a single case statement are separated by a comma
25
Select Case Example Select Case Grade Case “A” Display = “Excellent” Case “B” Display = “Very Good” Case “C” Display = “Good” Case “D” Display = “Poor” Case “F” Display = “Failure” End Select
26
Case Else Case Else can be helpful with error checking But be careful where you use it. Using before any of your Case Statements results in a syntax error
27
ElseIf vs. Select Case If grade =“A” Then displayLable.Text=“Excellent” ElseIf grade=“B” Then displayLabel.Text=“Very Good” ElseIf grade=“C” Then displayLabel.Text=“Good” ElseIf grade=“D” Then displayLabel.Text=“Poor” ElseIf grade=“F” Then displayLabel.Text=“Failure” Else displayLabel.Text=“Invalid Grade” End If Select case grade Case “A” displayLabel.Text=“Excellent” Case “B” displayLabel.Text=“Very Good” Case “C” displayLabel.Text=“Good” Case “D” displayLabel.Text=“Poor” Case “F” displayLabel.Text=“Failure” Case Else displayLabel.Text=“Invalid Grade” End Select
28
Select Case Options Case Is < 10 message = "restricted access" Case 1645 To 1689 message = "technitions" Case 8345 message = "custodians" Case 9998, 1006 To 1008 message = "scientists" Case Else message = "access denied" End Select
29
Bad Case Statement Case 51 To 30 Here the case statement is ignored and may result in a logic error
30
Try the following Write a select statement that tells the user to purchase a book if it is less than $50 otherwise keep searching Write a select statement that chooses a dollar amount to charge a customer based on an order: Hamburger = $1.50, Double Cheeseburger = 2, Fries = $1, Onion Rings = $1.5, Drink = $1. Allow the customer to order as much as he or she wants.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.