1 VB-04-Control Structures 16 March 2016 Visual Basic Control Structures - Selection.

Slides:



Advertisements
Similar presentations
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 4 Making Decisions in a Program.
Advertisements

Dr. Yang, Qingxiong (with slides borrowed from Dr. Yuen, Joe) LT4: Control Flow - Loop CS2311 Computer Programming.
Microsoft Visual Basic: Reloaded Chapter Six Repeating Program Instructions.
IS437: Fall 2004 Instructor: Dr. Boris Jukic Program Flow Control: Decisions and Conditions (Branching) Controlled Repetition (Looping)
Chapter 4 Repetitive Execution. 2 Types of Repetition There are two basic types of repetition: 1) Repetition controlled by a counter; The body of the.
Topic 6 – Repetition and Loops. CISC 105 – Topic 6 Program Repetition Repetition refers to the repeats of certain program statements within a program.
Control Structures: Part 2. Introduction Essentials of Counter-Controlled Repetition For / Next Repetition Structure Examples Using the For / Next Structure.
Chapter 6 - Visual Basic Schneider
Chapter 7 – Control Structures A payroll company calculates the gross earnings per week of employees. Employees’ weekly salaries are based on the number.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Looping Exercises Deciding Which Loop to Use At this.
110-K1 Iterations (1) Up to now, need to call the procedure again (e.g. click again on a command button) To repeat a piece of code: Can also use loops:
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Interest Calculator Application Introducing the For...Next Repetition Statements.
CIS 115 Lecture 8. There are 3 control structures common to most computer languages that determine the flow, or path of execution, of the code:  Sequential.
Chapter 12: How Long Can This Go On?
1 CC111 Lec9 : Visual Basic Visual Basic (3) Lecture 9.
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.
ENGR 112 Decision Structures.
 2002 Prentice Hall. All rights reserved. 1 Chapter 5 – Control Structures: Part 2 Outline 5.1Introduction 5.2 Essentials of Counter-Controlled Repetition.
CSCI 3327 Visual Basic Chapter 4: Control Statements in Visual Basic (Part 2) UTPA – Fall 2011 Part of the slides is from Dr. John Abraham’s previous.
Looping II (for statement). CSCE 1062 Outline  for statement  Nested loops  Compound assignment operators  Increment and decrement operators.
ISAT 252 Visual Basic Repetition. Assignment Should have read Chapter 5 ( ) on loops Do tutorial 5-4.
Compunet Corporation1 Programming with Visual Basic.NET While, Do and For – Next Loops Week 5 Tariq Ibn Aziz.
Statements That Repeat. For...Next Loop Structure For counter = start To end Step increment statements Next counter Where Counter is tested to see if.
CSC 162 Visual Basic I Programming. Repetition Structures Pretest Loop –Exit condition is tested before the body of code is executed Posttest Loop –Exit.
CHAPTER SIX LOOPS © Prepared By: Razif Razali 1. FORMAT OR REFRESH!! WHAT HAVE WE LEARN? Differentiate between the types of selection structure? Which.
Tutorial 6 The Repetition Structure
Control Structures II Repetition (Loops). Why Is Repetition Needed? How can you solve the following problem: What is the sum of all the numbers from 1.
CS285 Visual Basic 2 Department of Computing UniS 1 Statements in Visual Basic A statement is the fundamental syntactical element of a program smallest.
Saeed Ghanbartehrani Summer 2015 Lecture Notes #5: Programming Structures IE 212: Computational Methods for Industrial Engineering.
Lab 4 Range Review, Control Logic and Loops ► Range Review ► Control Logic and Loops ► Exercise.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Class Average Application Introducing the Do...Loop While and Do...Loop Until.
Repetition Control Structures Simple Program Design Third Edition A Step-by-Step Approach 5.
Developing Software Applications Iteration in Visual Basic (Loops)
CS161 Topic #16 1 Today in CS161 Lecture #16 Prepare for the Final Reviewing all Topics this term Variables If Statements Loops (do while, while, for)
Introduction to Problem Solving and Control Statements.
CW-V1 SDD 0901 Principals of Software Design and Development Loops Starter: Water JugsWater Jugs.
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
Control Structures, Blocks and Compound Statements A good programming language allows you to control the flow of your program in three ways: - execute.
Week 4 Program Control Structure
31/01/ Selection If selection construct.
CSC 162 Visual Basic I Programming. Repetition Structures Pretest Loop –Exit condition is tested before the body of code is executed Posttest Loop –Exit.
Controlling Program Flow with Looping Structures
5.1 Introduction Problem Solving –Requires understanding of: Building blocks Program-construction principles BZUPAGES.COM.
Topic: Control Statements. Recap of Sequence Control Structure Write a program that accepts the basic salary and allowance amount for an employee and.
Lab 6 (1) Range Review, Control Logic and Loops ► Control Logic and Loops ► Exercise.
If statement.  It is made up of three main components:  The keyword itself,  an expression that is tested for its truth value,  and a code suite to.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 4: Introduction to C: Control Flow.
 2002 Prentice Hall. All rights reserved. 1 Chapter 5 – Control Structures: Part 2 Outline 5.1Introduction 5.2 Essentials of Counter-Controlled Repetition.
Fourth Quarter.  Involves loops or cycles ◦ Loops: means that a process may be repeated as long as certain condition remains true or remains false. ◦
Loop Blocks Chapter 6 Part A. Program Blocks 1.Actions- commands, messages, methods 2.Branches- decisions to be made 3.Loops- actions to be repeated.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
Dani Vainstein1 VBScript Session 5. Dani Vainstein2 What we learn last session? Branching Branching using If … Then … Else statement. Branching using.
Chapter 4 Repetition Statements Program Development and Design Using C++, Third Edition.
Visual Basic Declaring Variables Dim x as Integer = 0 In the statement above, x is being declared as an Integer (whole number) and is initialised.
Loops ISYS 350. Write a Program that asks user to enter any numbers and displays the largest number Process: Largest = the first number Get the next number.
Chapter 6 Controlling Program Flow with Looping Structures.
Selection Using IF THEN ELSE CASE Introducing Loops.
PHP Condtions and Loops Prepared by Dr. Maher Abuhamdeh.
UNIT 5 Lesson 15 Looping.
Chapter 5- Control Structures: Part 2
Visual Basic 6 (VB6) Data Types, And Operators
Control Structures: Part 2
Ch 7: JavaScript Control Statements I.
Chapter 5 – Control Structures: Part 2
Control Structures: Part 1
Control Structure Senior Lecturer
حلقات التكرار.
Fundamentals of visual basic
Microsoft Visual Basic 2005: Reloaded Second Edition
Case & Repetitive Statements
Presentation transcript:

1 VB-04-Control Structures 16 March 2016 Visual Basic Control Structures - Selection

2 VB-04-Control Structures 16 March 2016 Learning Outcomes At the end of this lecture you will be able to : 1. Compare types of relational operators 2. Write programs using If..Else Construct 3. Use the Truth Table 4. Write programs using Select Case Construct 5. Use Nested If Statements

3 VB-04-Control Structures 16 March 2016 Key Words 1. If …Then..Else 2. AND 3. OR 4. NOT 5. Select Case

4 VB-04-Control Structures 16 March 2016 Relational Operators > greater than <less than <=less than equal >=bigger than or equal =equal to <>not equal to

5 VB-04-Control Structures 16 March 2016 IF Blocks If grade >= 60 then if a > b then MsgBox “Credit” MsgBox “a > b” Else else MsgBox “Pass” MsgBox “b > a” End If end if If a > b then if a b then MsgBox “a is larger” MsgBox “a not equal to b”end if

6 VB-04-Control Structures 16 March 2016 Group Exercise Write a program to accept two numbers and determine the largest number Use two text boxes to accept the numbers and a label to display the answer e.g “6 is larger than 5”

7 VB-04-Control Structures 16 March 2016 Truth Table

8 VB-04-Control Structures 16 March 2016 Truth Table - Examples Assume a = 2 and b = 3 Condition Answer (a < b) or (b < a) true (a < b) and (b < a) false Not (b < a) true

9 VB-04-Control Structures 16 March 2016 Suppose n = 4 and answ = “Y” Determine whether the following conditions are true or false 1. ((n > 2 ) And ( n < 6 )) Or (answ = “Y”) 2. ((2 < n) And (n = 5 + 1)) Or (answ = “No”) 3. Not (4 < 6) 4. (n = 2) And ((n = 7) Or (answ = “Y”)) 5. Not (answ = “y”) AND OR and NOT Operator

10 VB-04-Control Structures 16 March 2016 Group Exercise n Write a program using the constructs that you have learned to determine a students grade based on the following grading scale. n Accept the mark using an InputBox ScaleGrade A B C D F

11 VB-04-Control Structures 16 March 2016 Solution If grade >=75 and grade <= 100 then MsgBox “A” else if grade >= 60 and grade <= 64 then MsgBox “B” else if grade >=50 and grade <= 59 then MsgBox “C” else if grade >=40 and grade <= 49 then MsgBox “D” else MsgBox “F” end if

12 VB-04-Control Structures 16 March 2016 Using If…. Else If If condition1 then action1 Else If condition2 then action2 Else If condition3 then action3 Else action4 End if

13 VB-04-Control Structures 16 March 2016 Example grade = val(text1.text) If grade >=75 and grade <= 100 then MsgBox “A” elseif grade >= 60 and grade <= 64 then MsgBox “B” elseif grade >=50 and grade <= 59 then MsgBox “C” elseif grade >=40 and grade <= 49 then MsgBox “D” else MsgBox “F” end if

14 VB-04-Control Structures 16 March 2016 Select Case Blocks Select Case selector Case valuelist1 action1 Case valuelist2 action2 Case Else action of last resort End Select

15 VB-04-Control Structures 16 March 2016 Select Case - using To and IS number = val(txtinput.text) Select case number Case 1 to 4 MsgBox “Admin” Case 5 to 7 MsgBox “Accounts” Case 8,9 MsgBox “Staff Room” Case 10 MsgBox “Computer Labs” Case is < 1 MsgBox “Error” Case else MsgBox “Invalid Number” End select

16 VB-04-Control Structures 16 March 2016 Write a program using the select case construct to determine a traffic message based on a color code. Accept a colour code from a text box and display the following message : ColourMessage Red “STOP” Orange “GET READY” Green “GO” other color “Invalid” Group Exercise

17 VB-04-Control Structures 16 March 2016 Nested IF - Example If b < 5 Then If a > 2 Then MsgBox “Red" Else MsgBox “Blue" End If Else If a > 2 Then MsgBox “Green" Else MsgBox “Yellow" End If

18 VB-04-Control Structures 16 March 2016 Home Work n Write a program using Select Case to determine whether a person is a teenager or an adult. Use a Textbox for input. n Age 13 – 19 Teenager n Age > 19 Adult

19 VB-04-Control Structures 16 March 2016 Homework Develop an algorithm to accept a customer’s name, a purchase amount and a tax code. The tax code has been validated and will be one of the following: 0 tax exempt 1 state sales tax only (3%) 2 federal and state sales tax (5%) 3 special sales tax (7%) The program must then compute the sales tax and the total amount due and display the customer’s name, purchase amount, sales tax and the total amount due.

20 VB-04-Control Structures 16 March 2016 Homework n Develop a program that will determine the gross pay for an employee. The company pays “straight-time” for the first 40 hours worked by each employee and pays “time and a half” for all hours worked in excess of 40 hours. n You program should accept the number of hours each employee worked and the hourly rate and display the employee’s gross pay. Use Textboxes for input and a Label to display your answer.

21 VB-04-Control Structures 16 March 2016 Control Structures Repetition

22 VB-04-Control Structures 16 March 2016 Learning Outcomes At the end of this lecture you should be able to : 1. Write a While..Wend iteration 2. Write a Do..While iteration 3. Write a Do..Until Loop 4. Use a For.. Next iterative construct 5. Write a Nested For Loop

23 VB-04-Control Structures 16 March 2016 Key words used in this lecture 1. While..Wend 2. Do..While/Loop 3. Do..Until/Loop 4. For..Next 5. ListBox 6. ComboBox

24 VB-04-Control Structures 16 March 2016 Comparing Do While and Do Until n While … Wend and Do.. While loop will execute statements in the loop when the condition is True n Do until loop will execute statements in the loop when the condition is False

25 VB-04-Control Structures 16 March 2016 While/Wend While condition is True execute statement(s) Wend

26 VB-04-Control Structures 16 March 2016 Flowchart – While/Wend Ctr <=10 ctr = ctr + 1 True False

27 VB-04-Control Structures 16 March 2016 Display numbers from 1 to 10 n = 1 While n <=10 List1.Additem n n = n + 1 Wend While/Wend

28 VB-04-Control Structures 16 March 2016 Group Exercise n Write a program and draw a flowchart to display the sum of digits from 1 to 10 i.e n Show your answer using the While..Wend Loop n Use a MsgBox to display the Sum

29 VB-04-Control Structures 16 March 2016 While/Wend Repetition Solution Dim ctr, sum as integer ctr = 1 While ctr <=10 sum = sum + ctr ctr = ctr + 1 Wend MsgBox “The sum is” & sum

30 VB-04-Control Structures 16 March 2016 Do While/Loop Do While condition is True execute statement(s) Loop

31 VB-04-Control Structures 16 March 2016 Flowchart – Do While/Loop Ctr <=10 ctr = ctr + 1 True False Ctr = 1

32 VB-04-Control Structures 16 March 2016 Do While n = 1 Do while n <=10 list1.additem n n = n + 1 Loop

33 VB-04-Control Structures 16 March 2016 Group Exercise n Write a program that will accept 15 Fahrenheit temperatures using an InputBox and convert it to Celsius. n Display all the converted temperature in Celsius using a list box. Formula : FtoC = (fahrenheit - 32) * 5/9 n Use a Do While Loop

34 VB-04-Control Structures 16 March 2016 Control Structures - Repetition

35 VB-04-Control Structures 16 March 2016 Learning Outcomes At the end of this lecture you should be able to : 1. Write a Do..Until Loop 2. Use a For..Next Loop 3. Write a Nested For..Next Loop

36 VB-04-Control Structures 16 March 2016 Keywords 1. Do..Until/Loop 2. For..Next 3. ListBox 4. ComboBox

37 VB-04-Control Structures 16 March 2016 Do Until/Loop Do Until condition is False execute statement(s) Loop

38 VB-04-Control Structures 16 March 2016 Flowchart – Do Until/Loop ctr > 10 ctr = ctr + 1 False True ctr = 1

39 VB-04-Control Structures 16 March 2016 Do Until/Loop Dim ctr, sum as Integer ctr = 1 Do until ctr > 10 combo1.additem ctr ctr = ctr + 1 Loop

40 VB-04-Control Structures 16 March 2016 Exercise n Write a program to display the following on a list box using a Do…Until Loop

41 VB-04-Control Structures 16 March 2016 Answer Private Sub Command1_Click() ctr = 1 Do Until ctr > 5 List1.AddItem ctr & " " & ctr ^ 2 ctr = ctr + 1 Loop End Sub

42 VB-04-Control Structures 16 March 2016 Group Exercise n Write a program to determine the Factorial of N Example N = 5 5! = 5 * 4 * 3 * 2 * 1 = 120 Use a Do Until Loop

43 VB-04-Control Structures 16 March 2016 Solution Private Sub Command1_Click() Dim n, x, f As Integer n = Val(Text1.Text) x = n f = 1 Do Until x < 1 f = f * x x = x - 1 Loop MsgBox "Factorial of " & n & "is" & f End Sub

44 VB-04-Control Structures 16 March 2016 For…Next Loops n Is used when we know how many times a loop should be executed. Format For x = initialvalue to endvalue Step increment execute statement (s) Next x Example For x = m to n step 1 statement(s) Next x

45 VB-04-Control Structures 16 March 2016 For…Next Loops ‘Display numbers from 1 to 10 in a ComboBox Private sub cmdbutton_click() Dim n as integer For n = 1 to 10 step 1 Combo1.Additem n Next n End Sub

46 VB-04-Control Structures 16 March 2016 Group Exercise Use for.. next loop to determine the following 1. Find the sum of digits from 1 to Display the following number sequence a b c

47 VB-04-Control Structures 16 March 2016 Nested For…Next n Two For..Next statements which are running together n Consist of an Outer Loop and an Inner Loop Private Sub Command1_Click() List1.Clear For i = 1 To 5 For j = 1 To 3 pro = i * j List1.AddItem i & " * " & j & " = " & pro Next j Print ‘Leave a New Line Next i End Sub Outer Loop Inner Loop

48 VB-04-Control Structures 16 March 2016 Do/Loop While n The Do Loop While structure tests the loop condition after the loop body is performed n It executes the loop at least once

49 VB-04-Control Structures 16 March 2016 Flowchart – Do/Loop While action condition False True

50 VB-04-Control Structures 16 March 2016 Do/Loop While Dim ctr as integer Ctr = 1 Do MsgBox ctr ctr = ctr + 1 Loop while ctr <=10

51 VB-04-Control Structures 16 March 2016 Summary n Do While Wend execute statements when condition is true n Do Until Loop executes statements when condition is false n Do Loop While executes a loop at least once

52 VB-04-Control Structures 16 March 2016 Homework Print out the the following sequence in a list box Print the following stars pattern in a list box ***** **** *** ** *