Review for Exam 2 School of Business Eastern Illinois University © Abdou Illia, Spring 2003 (Week 10, Friday 3/21/2003) - IF Blocks - Do Loops - Select.

Slides:



Advertisements
Similar presentations
Objectives Understand the software development lifecycle Perform calculations Use decision structures Perform data validation Use logical operators Use.
Advertisements

CS0004: Introduction to Programming Repetition – Do Loops.
Chapter 5 Decisions. Outline and Objectives Relational and Logical Operators If Blocks Select Case Blocks.
Val Function A Function performs an action and returns a value The expression to operate upon, known as the argument, (or multiple arguments), must be.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 5, 2005.
Basic Elements of Programming A VB program is built from statements, statements from expressions, expressions from operators and operands, and operands.
Types of LOOP Structures Do While ……. Loop Do Until …… Loop For …… Next loop.
Chapter 5 - VB.Net by Schneider
CSC110 Fall Chapter 5: Decision Visual Basic.NET.
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
Chapter 6 - Visual Basic Schneider1 Chapter 6 Repetition.
Chapter 5 - Visual Basic Schneider1 Chapter 5 Decisions.
Chapter 6 - Visual Basic Schneider
Chapter 6 - Visual Basic Schneider1 Chapter 6 Repetition.
IS 1181 IS 118 Introduction to Development Tools VB Chapter 03.
1 Chapter 6 Repetition. 2 Outline & Objectives Loop Structure Loop Structure Elements of a Loop Structure Elements of a Loop Structure Processing Lists.
COMP 110 Introduction to Programming Mr. Joshua Stough September 24, 2007.
Chapter 51 Select Case block A decision-making structure that simplifies choosing among several actions. Avoids complex nested If constructs. If blocks.
Presented by Joaquin Vila Prepared by Sally Scott ACS 168 Problem Solving Using the Computer Week 12 Boolean Expressions, Switches, For-Loops Chapter 7.
Chapter 4: The Selection Structure
1 Chapter 6 – Repetition 6.1 Do Loops 6.2 For...Next Loops 6.3 List Boxes and Loops.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
Array Processing: Exercises School of Business Eastern Illinois University © Abdou Illia, Spring 2002 (Week 10, Friday 3/28/2003)
Chapter 5 – Decisions 5.1 Relational and Logical Operators 5.2 If Blocks 5.3 Select Case Blocks.
Chapter 4 - VB 2008 by Schneider1 Chapter 4 – Decisions 4.1 Relational and Logical Operators 4.2 If Blocks 4.3 Select Case Blocks.
Chapter 5 - VB 2005 by Schneider1 Chapter 5 – Decisions 5.1 Relational and Logical Operators 5.2 If Blocks 5.3 Select Case Blocks 5.4 A Case Study: Weekly.
Chapter 6 - VB 2005 by Schneider1 Chapter 6 – Repetition 6.1 Do While and Do Until Loops 6.2 Processing Lists of Data with Do Loops 6.3 For...Next Loops.
Decision II. CSCE 1062 Outline  Boolean expressions  switch statement (section 4.8)
Chapter 5 Decisions. Outline and Objectives Relational and Logical Operators If Blocks Select Case Blocks.
Flow of Control Part 1: Selection
Chapter 5 - VB 2005 by Schneider1 Chapter 5 – Decisions 5.1 Relational and Logical Operators 5.2 If Blocks.
Chapter 6 - Visual Basic Schneider 1 Chapter 6 Repetition.
Chapter 71 Repetition - Do Loops n A Loops, is used to repeat a sequence of statements a number of time n There are two loops commands in Visual Basic.
PROBLEM SOLVING & ALGORITHMS CHAPTER 5: CONTROL STRUCTURES - SELECTION.
22/11/ Selection If selection construct.
1 Flow Control Ifs, loops. 2 Data Type At the lowest level, all data in a computer is written in 1’s and 0’s (binary) How the data gets interpreted, what.
Repetition Chapter 6 - Visual Basic Schneider 1  Loop Structure  Elements of a Loop Structure  Processing Lists of Data with Do Loops Chapter 6 -
Chapter 3 Control Structures. The If…Then Statement The If…Then statement is a Decision statement = that executes a set of statements when a condition.
Java Programming Fifth Edition Chapter 5 Making Decisions.
Chapter 5: Making Decisions. Objectives Plan decision-making logic Make decisions with the if and if…else structures Use multiple statements in if and.
Review for Final (Part 2) School of Business Eastern Illinois University © Abdou Illia, Spring 2003 (Week 15, Friday 5/2/2003)
31/01/ Selection If selection construct.
Chapter81 For....Next Loops n For i = m To n statement (s) Next i n For statement designates a numeric variable, called control variable. n It is initialized.
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Conditions - compare the values of variables, constants and literals using one or more relational.
Control Statements: Part1  if, if…else, switch 1.
While loops. Iteration We’ve seen many places where repetition is necessary in a problem. We’ve been using the for loop for that purpose For loops are.
Chapter 6 - Visual Basic Schneider1 Chapter 6 Repetition.
An Introduction to Programming with C++ Sixth Edition Chapter 5 The Selection Structure.
Chapter 4 Select … Case Multiple-Selection Statement & Logical Operators 1 © by Pearson Education, Inc. All Rights Reserved. -Edited By Maysoon.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Chapter 4 - VB 2008 by Schneider1 Chapter 4 – Decisions 4.1 Relational and Logical Operators 4.2 If Blocks 4.3 Select Case Blocks.
1 Chapter 5 – Decisions 5.1 Relational and Logical Operators 5.2 If Blocks 5.3 Select Case Blocks 5.4 A Case Study: Weekly Payroll.
Week 4 Relational and Logical Operators Dr. Jennifer Cunningham.
Select Case Blocks School of Business Eastern Illinois University © Abdou Illia, Spring 2003 (Week 8, Monday 3/03/2003)
Chapter 4 – C Program Control
Visual Basic 6 (VB6) Data Types, And Operators
Chapter 4: Making Decisions.
Chapter 4: Making Decisions.
Chapter 7 Arrays.
Scratch: selection / branching/ if / If…else / compound conditionals / error trapping by Mr. Clausen.
VB Decisions, Conditions & If
3 Control Statements:.
Chapter 5 - Visual Basic Schneider
If selection construct
VB Decisions & Conditions
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
Boolean Expressions to Make Comparisons
Chapter 5 Decisions.
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
Lecture 9: Implementing Complex Logic
Presentation transcript:

Review for Exam 2 School of Business Eastern Illinois University © Abdou Illia, Spring 2003 (Week 10, Friday 3/21/2003) - IF Blocks - Do Loops - Select Case Blocks

2 IF Blocks n Represent computers abilities to make decisions n Allow a program to decide on a course of action – Based on whether a certain condition is true or false n Simplest form (Single-alternative decision): If Condition Then Statement(s) End If Syntax If Credits < 10 Then Tuition = 100 * Credits End If Example n Condition is an expression that is evaluated by the computer n If Condition is true, Statement(s) are executed;otherwise Statement(s) are skipped

3 Nested IF Blocks n Nested IF Blocks (IF Blocks contained inside others): If condition1 Then [Statement(s)] If condition2 Then Statement(s) End If Syntax If Credits > 10 Then Tuition = 1000 If Credits > 15 Then Message = "Over load" End If Example n Note: In the nested If Blocks, each If must have its own End If statement CreditsTuitionMessage Fill in this table indicating the values of variables Tuition and Message for each value of Credits

4 Conditions n Expression involving: – Relational operators (, >=, ….) – Logical operators (And, Or, Not) letterGrade = txtGrade.Text If ( letterGrade = "A") Or (letterGrade = "B") Then picOutput.Print "Good Work" ElseIf (letterGrade = "C") Then picOutput.Print " Average Work" Else picOutput.Print "Poor Work" End If

5 Relational operators n Applied to numbers. Also applied to strings

6 Relational operators n Computer uses a special coding system to compare character strings n String1 is said to be less than String2 if String1 precede String2 alphabetically according to ANSI table ( See Appendix A ). "Chase" < "Chaz" True " Cat" < "Cat" True "Pay" < "Pay " True "Jones" <> "James" True "Hope" < "Hopeful" True Example: Comparing character strings

7 Logical operators n The three Logical Operators are: Not, And, Or n Not: Negates a single expression. Example: Suppose answer = "Y" Not (answer = "y") is true n And: Combines two expressions; each expression must be True for the entire expression to be True. Example: Suppose answer = "Y" (answer = "Y") And (answer = "y") is False n Or: Combines two expressions; either expression (or both expressions) must be True for the entire expression to be True. Example: Suppose answer = "Y" (answer = "Y") Or (answer = "y") is True

8 VB operators hierarchy n First, Arithmetic operations (^, *, /, +, -) are carried out n Second, Expressions involving relational operators (>, <, =, …) are evaluated to True or False n Third, Logical operators are applied in the order Not, And, Or. n In the event of a tie, the leftmost operator is applied. Note: Expressions in parentheses evaluated first

9 Determine whether the following conditions are true or false? n 1 <= 1 Answer: True n "car" < "cat" Answer: True (because alphabetically, "car" precedes "cat") n "Dog" < "dog" Answer: True (because Uppercase precedes lowercase) n ("Y" "X") And ( < ) Answer: False

10 Determine whether the following conditions are true or false? n (0 = 14) Or (6 ^ <= 4 / 2 + 8) Answer: False n Not (6 = 7) And ( 44 > 33) Answer: True In the next two cases, suppose that n = 4 n (2 < n) And (n < 6) Answer: True n (2 < n) Or (n = 6) Answer: true

11 Determine whether the following conditions are true or false? In the next cases, assume a = 2 and b = 3 n 3 * a = 2 * b Answer: True n (5 – a) * b < 7 Answer: False n b <= 3 Answer: True n a ^ b = b ^ a Answer: false

12 Determine whether the following conditions are true or false? In the next cases, assume a = 2 and b = 3 n a ^ (5 – 2) > 7 Answer: True n (a < b) Or (b < a) Answer: True n (a * a < b) Or Not (a * a < a) Answer: True n Not (a < b) Or Not (a < (b + a)) Answer: False

13 Determine the output displayed in the picture box when the command button is clicked. Private Sub cmdDisplay_Click() Dim a As Single a = 5 If 3 * a – 4 < 9 Then picOutput.Print "Remember, " End If picOutput.Print "Tomorrow is another day." End Sub Answer: Tomorrow is another day.

14 Determine the output displayed in the picture box when the command button is clicked. Private Sub cmdDisplay_Click() Dim Gpa As Single Gpa = 3.49 If Gpa >= 3.5 Then picOutput.Print "Honors"; End If picOutput.Print "Student" End Sub Answer: Student

15 Determine the output displayed in the picture box when the command button is clicked. Private Sub cmdDisplay_Click() Dim a As Single a = 5 If (a > 2) And (a = 3 Or a <7) Then picOutput.Print "Hi" End If End Sub Answer: Hi

16 Do Loops (using While) n Used to repeat a sequence of statements while a condition is True Do While Condition Statement(s) Loop Syntax 1 Private Sub cmdDisplay_Click() Dim Num As Integer ' Display the numbers from 1 to 10 Num = 1 Do While Num <= 10 picNumbers.Print Num; Num = Num + 1 Loop End Sub Example Do Statement(s) Loop While Condition Syntax 2 n Condition involve: – Relational operators (, >=, ….) – Logical operators (And, Or, Not) Same rules than Condition in IF Blocks (seen before Spring Break)

17 Do Loops (using Until) n Used to repeat a sequence of statements until a condition becomes True Do Statement(s) Loop Until condition Syntax 4 Private Sub cmdDisplay_Click() Dim Num As Integer ' Display the numbers from 1 to 10 Num = 1 Do Until Num > 10 picNumbers.Print Num; Num = Num + 1 Loop End Sub Example Do Until conditon Statement(s) Loop Syntax 3 n Note: To get the same result using While and Until, Conditions should be opposites. For instance, While Num 10

18 Do Loops n Used to display all or selected items from a file – Using EOF function Private Sub cmdDisplay_Click() Dim Name As String, Phone As String picPhones.Cls Open " A:\Phone.txt " For Input As #1 Do While Not EOF(1) Input #1, Name, Phone picPhones.Print Name, Phone Loop Close #1 End Sub Example " Bert ", " " " Ernie ", " " " Grover ", " " " Oscar ", " " Phone.txt

19 Replace each statement containing Until with an equivalent statement containing a While, and vice versa. n Loop Until AccountBalance >= 1200 Answer: Loop While AccountBalance < 1200 n Do While Amount = 100 Answer: Do Until Amount 100 n Do Until Name = "Bob" Answer: Do While Name "Bob" n Do While (a > 1) And (a < 3) Answer: Do Until (a = 3) n Do Until (Answer "Y") Or (n >= 7) Answer: Do While (Answer = "Y") And (n < 7)

20 Identify the errors n Private Sub cmdDisplay_Click() Dim Num As Integer ' Display the numbers from 1 to 10 Do While Num >= 10 Num = 1 picOutut.Print Num; Num = Num + 1 Loop End Sub Answer: Program never stops. Statement Num = 1 should appear before Do Loop n Private Sub cmdDisplay_Click() Dim q As Single q = 1 Do While q > 0 q = 3 * q - 1 picOutput.Print q; Loop End Sub Answer: Program never stops because q = 2 within the loop all time.

21 Identify the errors n Private Sub cmdDisplay_Click() Dim Num As Single Open “DATA.TXT” For Input As #1 Do While (Not EOF (1)) And (Num > 0) Input #1, Num picOutut.Print Num; Close #1 End Sub Note: Assume that DATA.TXT contains: 7, 6, 0, -1, 2 Answer: Loop statement is missing. Also, loop cannot be entered because the (default) value of Num is 0 n Private Sub cmdDisplay_Click() Dim Num As Integer ' Display numbers Do While 1 < Num < 5 Num = 1 picOutut.Print Num; Num = Num + 1 Loop End Sub Answer: Invalid condition. Condition should be (1 < Num) And (Num < 5)

22 Simple program using a Do loop n The world population reached 6 billion people in 1999 and was growing at the rate of 1.4 percent each year. Assuming that the population will continue to grow at the same rate, write a program to determine when the population will exceed 10 billion. Private Sub cmdCalculate_Click( ) Dim Year As Integer, Population As Single Year = 1999 Population = 6 Do Until Population > 10 Population = Population + (0.014 * Population) Year = Year + 1 Loop picOutput.Print " The answer is: " ; Year End Sub

23 For …Next n Repeats a group of statements a specified number of times n For…Next are used when we know exactly how many times a loop should be executed. Private Sub cmdDisplay_Click() Dim i As integer picTable.Cls For i = 1 To 5 picTable.Print i; i^2 Next i End Sub Example For counter = start To end Statement(s) Next [counter] Where: counter is a numeric variable used as a loop counter start is the initial value of counter end is the final value of counter Private Sub cmdDisplay_Click() Dim i As integer picTable.Cls i = 1 Do While i <= 5 picTable.Print i; i^2 i = i + 1 Loop End Sub Same example using Do loop

24 Select Case Blocks n Very efficient decision-making structures: – Simplifies choosing among several actions n Avoid using Nested IF and ElseIf statements n Actions based on the value of an expression called selector Select Case Selector Case ValueList1 Action1 Case ValueList2 Action2 : Case Else Action of last resort End Select Syntax Age = Val(txtAge.Text) Select Case Age Case 5 Category = "5-year old" Case 13 To 19 Category = "Teenager" Case Is > 65 Category = "Senior" Case Else Category = "Everyone else" End Select Example

25 Select Case Statements n Selectors can be: – Numeric or string variables. Example: Select Case FirstName – Expressions. Example: Select Case Left(EmployeeName, 1) Select Case Selector Case ValueList1 Action1 Case ValueList2 Action2 : Case Else Action of last resort End Select Syntax n ValueList items must be separated by commas n ValueList can be: – A constant – An expression – An inequality sign preceded by Is and followed by a constant, a variable, or an expression – A range expressed in the form a To b. x = 2 : y = 3 Num = Val(txtNumber.Text) Select Case Num Case y – x, x picOutput.Print "Buckle my shoes" End Select Grade = Val(txtBox.Text) Select Case Grade Case Is >= 90 picOutput.Print "A" End Select Grade = Val(txtBox.Text) Select Case Grade Case 60 To 89 picOutput.Print "Pass" End Select

26 Problem 1: Assigning letter grades (To be done in class) n Write a Click event procedure associated to the command button cmdShow that asks the user to type in an exam score and: – Assigns a letter grade based on the scale in the table below. The variable to be assigned the letter grade as value should be named Grade – Displays the score typed by the user and the letter grade in picBox. ScoreLetter grade A 80-89B 70-79C 60-69D 0-59F txtScore cmdShow picBox

27 Problem 1: Assigning letter grades Private Sub cmdShow_Click() Dim Score As Single Score = Val(txtScore.Text) Select Case Score Case 90 To 100 Grade = "A" Case 80 To 89 Grade = "B" Case 70 To 79 Grade = "C" Case 60 To 69 Grade = "D" Case 0 To 59 Grade = "F" End Select picBox.Print Score; Grade End Sub ScoreLetter grade A 80-89B 70-79C 60-69D 0-59F txtScore cmdShow picBox

28 Problem 2: IRS n IRS informants are paid cash rewards based on the value of the money recovered. n Write a Click event procedure associated to the command button cmdReward that asks the user to type in the Amount recovered and: – Determine the Reward based on the following rule: 10% for up to $75000 recovered, 15% for a recovered amount greater than but less than $100000; and 18% for more than $ – Displays the calculated reward in picReward. txtAmount cmdShow picReward

29 Problem 2: IRS Private Sub cmdShowReward_Click() Dim Amount As Single Amount = Val(txtAmount.Text) Select Case Amount Case Is <= Reward = 0.1 * Amount Case Is <= Reward = 0.15 * Amount Case Is > Reward = 0.18 * Amount End Select picReward.Print "The reward is "; Reward End Sub txtAmount cmdShow picReward

30 Identify the errors. Private Sub cmdDisplay_Click() Dim num As Single num = 2 Select Case num picOutput.Print "Two" End Select End Sub Answer: Case statement is missing.

31 Identify the errors. Private Sub cmdDisplay_Click() Dim num As Single num = 5 Select Case num Case 5, Is <>5 picOutput.Print "Five" Case Is > 5 picOutput.Print "Greater than 5" End Sub Answer: - First Case statement is executed every time, no matter the value of num - Should have an End Select.

32 Suppose the selector of a Select Case statement, word, evaluates to a string value. Determine whether the Case clause is valid. n Case Left("abc", 1) Answer: Valid n Case word "No" Answer: Invalid. Should be Case Is "No" n Case 0 To 9 Answer: Invalid (because 0 To 9 doesn’t refer to a string value) n Case "Hello", Is < "goodbye" Answer: Valid n Case "un" & "til" Answer: Valid (because "un" & "til" will give until)