Review for Final (Part 2) School of Business Eastern Illinois University © Abdou Illia, Spring 2003 (Week 15, Friday 5/2/2003)

Slides:



Advertisements
Similar presentations
Visual Basic Statements Chapter 5. Relational Operators  OperationSymbol  Equal  =  Less than  <  Greater than  >  Not equal   Less than.
Advertisements

Objectives Understand the software development lifecycle Perform calculations Use decision structures Perform data validation Use logical operators Use.
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.
Decisions (Conditional Programming) Chapter 5 (Sec. 5.1 & 5.2)
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.
Two-Dimensional Arrays School of Business Eastern Illinois University © Abdou Illia, Spring 2003 (Week 11, Friday 4/04/2003)
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.
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.
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)
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.
Chapter 5 – Decisions 5.1 Relational and Logical Operators 5.2 If Blocks 5.3 Select Case Blocks.
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
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.
1 CSC 1401 S1 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University
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.
Sub procedures School of Business Eastern Illinois University © Abdou Illia, Spring 2002 (Week 6, Friday 2/21/03)
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 SIX LOOPS © Prepared By: Razif Razali 1. FORMAT OR REFRESH!! WHAT HAVE WE LEARN? Differentiate between the types of selection structure? Which.
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.
CS285 Visual Basic 2 Department of Computing UniS 1 Statements in Visual Basic A statement is the fundamental syntactical element of a program smallest.
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 -
Repetition Structures
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.
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.
Visual Basic Review LBS 126. VB programming Project Form 1Form 2Form 3 Text boxButton Picture box Objects Text box Button Objects.
Control Statements: Part1  if, if…else, switch 1.
Chapter 6 - Visual Basic Schneider1 Chapter 6 Repetition.
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)
Visual Basic 6 (VB6) Data Types, And Operators
Chapter 4 - VB 2008 by Schneider
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
Scratch: selection / branching/ if / If…else / compound conditionals / error trapping by Mr. Clausen.
VB Decisions, Conditions & If
3 Control Statements:.
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
Chapter 5 - Visual Basic Schneider
VB Decisions & Conditions
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
SELECTIONS STATEMENTS
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
Chapter 5 Decisions.
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
For...Next Statements.
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
Presentation transcript:

Review for Final (Part 2) School of Business Eastern Illinois University © Abdou Illia, Spring 2003 (Week 15, Friday 5/2/2003)

- IF Blocks - Evaluating conditions

3 IF Statements n Allow a program to decide on a course of action – Based on whether a certain condition is true or false If condition Then Statement(s) Else Other Statement(s) End If Syntax n Condition is an expression that is evaluated to True or False n If condition is true, Statement(s) are executed. If condition is false, Other Statement(s) are executed. If Credits > 10 Then Tuition = 1000 Else Tuition = 100 * Credits End If Example

4 Conditions n Expressions involving: – Relational operators (, >=, =, ….) – Logical operators (And, Or, Not) – Arithmetic operations If (letterGrade = “A”) Or (letterGrade = “B”) Then picOutput.Print “Good Work” Else picOutput.Print “Grade is C or D” End If Example

5 Relational operators n Can be applied to: – numbers – strings

6 Relational operators applied to strings 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 (because of the spaces) “Pay” < “Pay “ True (because of the spaces) “Jones” <> “James” True “Hope” < “Hopeful” True Examples of 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 – ^ executed first – * and / executed second – + and – executed third – Note: If parentheses are used, expressions in parentheses are performed first according to those precedence rules. 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.

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 (Because is not less than )

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 n 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? n 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? n 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

Do Loops Select Case Blocks

17 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)

18 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

19 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

20 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)

21 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 = Do Until Population > Population = Population + (0.014 * Population) Year = Year + 1 Loop picOutput.Print " The answer is: " ; Year End Sub

22 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

23 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

24 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

25 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

26 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

27 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

28 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

- One-dimensional arrays - Two-dimensional arrays - Sequential Files

30 One-dimensional arrays n See hard copy received to prepare for exam 3 n Copy available at:

31 Two-dimensional arrays n See hard copy received to prepare for exam 3 n Copy available at:

32 Sequential Files n See hard copy received to prepare for exam 3 n Copy available at:

List box Combo box

34 List box and Combo box n See Answer to Exercises received in class. n See Quiz 3 (available at