31/01/20161 4.1 Selection If selection construct.

Slides:



Advertisements
Similar presentations
CS0004: Introduction to Programming Select Case Statements and Selection Input.
Advertisements

P1PMF Split1 QBASIC. P1PMF Split2QBasic Command Prompt Will launch the emulator DOS operating system? Press Alt + Enter to display the widescreen.
5.04 Apply Decision Making Structures
30/04/ Selection Nested If structures & Complex Multiple Conditions.
CSC110 Fall Chapter 5: Decision Visual Basic.NET.
Control Structures: Getting Started Sequence and Selection also arithmetic operators, data types, logical operators.
Visual Programming w/ Visual Basic How to prepare your mind to face the endless nightmare By Williem.
3/9/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore Bostrup 1 Introduction to VB6 Week 2.
Visual Basic Fundamental Concepts. Integrated Development Enviroment Generates startup form for new project on which to place controls. Features toolbox.
MIS 3200 – Unit 6.2 Learning Objectives How to move data between pages – Using Query Strings How to control errors on web pages – Using Try-catch.
© 1999, by Que Education and Training, Chapter 5, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach.
08/09/ Arrays Defining, Declaring & Processing.
University of Toronto at Scarborough © Andria Hunter, Kersti Wain-Bantin CSCA01 VBA-3 1 Lecture Outline Variable Scope Calling another subprogram Programming.
1 Κατανεμημένες Διαδικτυακές Εφαρμογές Πολυμέσων Γιάννης Πετράκης.
06/10/ Working with Data. 206/10/2015 Learning Objectives Explain the circumstances when the following might be useful: Disabling buttons and.
08/10/ Iteration Loops For … To … Next. 208/10/2015 Learning Objectives Define a program loop. State when a loop will end. State when the For.
1 2.2 Selection Logical Operators. 2 Learning Objectives Explain how the logical operator AND Boolean statements works.
3 - Variables Lingma Acheson Department of Computer and Information Science, IUPUI CSCI N331 VB.NET Programming.
110-G1 Motivation: Within a program, may have to perform the same computation over and over Many programs share the same computation (e.g. sorting) To.
CMPS 1371 Introduction to Computing for Engineers CONDITIONAL STATEMENTS.
Input Textboxes Input Boxes Different than textboxes Good for small amount of input (form full of textboxes is not nice) X = Inputbox(“prompt message”,
26/10/ Selection Nested If structures & Complex Multiple Conditions.
CSE1222: Lecture 6The Ohio State University1. Common Mistakes with Conditions (1)  Consider the following code: int age(26); if (age = 18) { cout
30/10/ Iteration Loops Do While (condition is true) … Loop.
Saeed Ghanbartehrani Summer 2015 Lecture Notes #5: Programming Structures IE 212: Computational Methods for Industrial Engineering.
Conditional Expression One of the most useful tools for processing information in an event procedure is a conditional expression. A conditional expression.
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.
Validation "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs and the Universe trying to.
1 Week 5 More on the Selection Structure. 2 Nested, If/ElseIf/Else, and Case Selection Structures Lesson A Objectives After completing this lesson, you.
1 CS105 Discussion 5 – Variables and If Announcements MP 1 due on Monday Midterm 1 on Tuesday If you need a conflict, request it NOW!!
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.
Hungarian Notation A must in this course Every object used MUST be renamed including the form(s) using the following rules Form  frmFormName E.g. frmTemperature.
Chapter 6 Looping Structures. Do…LoopDo…Loop Statement Can operate statements repetitively Do intx=intx + 1 Loop While intx < 10 –The Loop While operates.
Chapter 4 Getting Started with VBA. Subroutines Subroutine is the logical section of code that performs a particular task. Subroutine is also called a.
Controlling Program Flow with Looping Structures
110 F-1 Decisions and Conditions Chapter 4: We can design a form We can calculate To make our programs more powerful, we need to be able to make choices.
05/02/ Records. 205/02/2016 Learning Objectives State: The difference between records and arrays. The difference between records and arrays. How.
Controlling Program Flow with Decision Structures.
Knowledge Base. Defining a Variable Dim statement Dim intXX As Integer Public in a Module Public dblNN As Double.
AVCE ICT – Unit 7 - Programming Session 12 - Debugging.
1 4.2 Selection Logical Operators. 2 Learning Objectives Explain how the logical operator AND Boolean statements works. Directly testing if text boxes.
Making Interactive Programs with Visual Basic .NET
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.
Computer Science Up Down Controls, Decisions and Random Numbers.
26/06/ Iteration Loops For … To … Next. 226/06/2016 Learning Objectives Define a program loop. State when a loop will end. State when the For.
Visual Basic Fundamental Concepts
COMPUTATIONAL CONSTRUCTS
Tutorial 10 – Class Average Application Introducing the Do…Loop While and Do…Loop Until Repetition Statements Outline Test-Driving the Class Average.
VB.Net Programming Console Application
IF statements.
3rd prep. – 2nd Term MOE Book Questions.
3rd prep. – 2nd Term MOE Book Questions.
Microsoft Visual Basic 2005 BASICS
Visual Basic..
Department Array in Visual Basic
Conditions and Ifs BIS1523 – Lecture 8.
Chapter (3) - Looping Questions.
If selection construct
Do While (condition is true) … Loop
If selection construct
Do … Loop Until (condition is true)
Selection Statements.
Text / Serial / Sequential Files
Language Constructs Construct means to build or put together. Language constructs refers to those parts which make up a high level programming language.
Programming In Lesson 4.
Text / Serial / Sequential Files
3.2 Working with Data Scope of variables 29/07/2019.
8 Records 25/07/2019.
Presentation transcript:

31/01/ Selection If selection construct

231/01/2016 Learning Objectives Explain why we may want to join strings together and how we do it Describe the If structure and its variations.

331/01/2016 What is selection? A program testing whether a condition is true or false and - depending on the answer - deciding to execute or not to execute one or more lines of code.

431/01/2016 Types of Selection in VB Two selection constructs: If If Select Case Select Case

531/01/2016 The If construct has three variations 1. If ….. Then ….. End If 2. If ….. Then ….. Else ….. End If 3. If ….. Then ….. ElseIf …. Then ….. Else ….. End If

631/01/2016 Dim Age As Integer Age = txtAge.Text If Age > 16 Then ‘ Age greater than 16? MsgBox (“You are old enough to drive.”) MsgBox (“You are old enough to drive.”) End If 1. If ….. Then ….. End If

731/01/2016 Notes The condition to test is Age > 16. If it is true the message is shown, and if false the message is skipped. If it is true the message is shown, and if false the message is skipped. Because the condition is either true or false it is called a boolean condition (Boolean is a data type). Any If statement must always have a matching End If to tell VB where the construct ends. There are two routes through this example and one condition to test.

831/01/2016 Relational / Comparative Operators = equal to < less than > more than <= smaller than or equal to >= greater than or equal to <> not equal to These relational/comparative operators return value true or false to the program.

931/01/2016 If Age > 16 Then ‘ Age greater than 16? MsgBox (“You are old enough to drive.”) MsgBox (“You are old enough to drive.”) Else ‘ Age 16 or less. MsgBox (“Sorry, you are too young to drive. You must be 17 years old.”) MsgBox (“Sorry, you are too young to drive. You must be 17 years old.”) End If 2. If ….. Then ….. Else ….. End If

1031/01/2016 Notes The Else part of the construct is executed if the boolean condition is false. There are two routes through this example and one condition to test.

1131/01/ If ….. Then ….. ElseIf ….. Else ….. End If

1231/01/2016 If Age > 16 Then ‘ Age greater than 16? MsgBox (“You are old enough to drive.”) MsgBox (“You are old enough to drive.”) ElseIf Age = 16 Then ‘ Age 16 exactly? MsgBox (“Sorry, you are too young to drive. You only have to wait less than a year though.”) MsgBox (“Sorry, you are too young to drive. You only have to wait less than a year though.”) Else ‘ Age 15 or less. MsgBox (“Sorry, you are too young to drive. You must be 17 years old.”) MsgBox (“Sorry, you are too young to drive. You must be 17 years old.”) End If

1331/01/2016 Notes There are three routes through this example and two boolean conditions to test. For example: If Age is 16: If Age is 16: The first condition Age > 16 is false. The second one, Age = 16, is tested, and since it is true the next two lines of code are executed. The Else part would be skipped. More routes are possible if you use more ElseIf statements.

1431/01/2016 Concatenation Joins strings together using the & operator. e.g. Putting a variable in a message: e.g. Putting a variable in a message: MsgBox (“My name is “ & Name & “. I am “ & Age & “ years old.”) MsgBox (“My name is “ & Name & “. I am “ & Age & “ years old.”) Will show My name is …… I am … years old.

1531/01/2016 Program 4.1 Deciding exam grades Specification: Ask the user to enter an exam mark from 0 to 100. Ask the user to enter an exam mark from 0 to 100. Display the grade it represents – Merit (60 or more), Pass (40 – 59), Fail (under 40). Display the grade it represents – Merit (60 or more), Pass (40 – 59), Fail (under 40).

1631/01/2016 Program 4.1 Deciding exam grades Create a new project named ‘Grades’. Change the form’s Text property to ‘Deciding exam grades’. txtMark butMark

1731/01/2016 Program 4.1 Deciding exam grades Double click butEnter and enter the following code in its code template: Dim Mark As Integer Dim Mark As Integer Mark = txtMark.Text Mark = txtMark.Text ‘The following If statement is after the declaring and storing lines because the Mark has to be stored before we can test it; it is before the Message boxes because we have to decide which one to display. ‘The following If statement is after the declaring and storing lines because the Mark has to be stored before we can test it; it is before the Message boxes because we have to decide which one to display. If Mark >=60 Then ‘Mark 60 or more? If Mark >=60 Then ‘Mark 60 or more? MsgBox (“Merit”) ElseIf Mark >= 40 Then ‘Mark ? ElseIf Mark >= 40 Then ‘Mark ? MsgBox (“Pass”) Else ‘Mark under 40. Else ‘Mark under 40. MsgBox (“A mark of “ & Mark & “ is a fail.”) End If End If

1831/01/2016 Program 4.1 Deciding exam grades Run the program and test each of the three routes through the If construct by entering the following marks:

Commenting on If Statements From presentations 4.1 – 4.4 I will only ask for comments to If statements Your comments MUST explain: What are you testing? Why are you testing for this? When (after and before what) are you testing for this and why does it have to be there? When in the procedure code or, if it is on its own, in which procedure (button, checkbox, textbox, etc…)? What happens if the test is true? Note that you may answer all these questions in one long comment either before or after the If statement you are commenting on; or you can answer each question with a separate comment. It is up to you.

2031/01/2016 Extension “Salesman Bonus” Program 1 Write a program for a salesman to input the total value of their sales this year and give their bonus: >= €100,000 then their bonus = €10,000. >= €100,000 then their bonus = €10,000. From €70,000 to €99, then their bonus = €7,000. From €70,000 to €99, then their bonus = €7,000. From €50,000 to €69, then their bonus = €4,000. From €50,000 to €69, then their bonus = €4,000. < then 50,000 then they receive no bonus. < then 50,000 then they receive no bonus.

Checking for errors1 There are 2 ways to form IF constructs to check for errors: Simplistic Method: If ErrorCheck is True Then MsgBox (“ Suitable Error Message. ”) lblLabelResult.Text = “” ‘Clear the “result” label. Exit Sub ‘Stop the procedure. End If ….. Code that you want executed if everything is OK. …… Without Exit Sub your program will correctly report the error but will continue and crash anyway.

Checking for errors2 More “professional” or “elegant” method: If ErrorCheck is True Then MsgBox (“ Suitable Error Message. ”) lblLabelResult.Text = “” ‘Clear the “result” label. Else ….. Code that you want executed if everything is OK. …… End If It doesn’t really matter which way you actually choose but you should attempt the more “elegant” method or at least be able to understand it, as this is the way it will probably be given to you in exams.

23 Extension “Arithmetic Error” Program 2 Write a program that will output the value of the expression: Area /(SpaceWidth * SpaceLength – EmptySpaces) What happens if the following values are used? SpaceWidth ← 7 SpaceLength ← 4 EmptySpaces ← 28 This is called an “arithmetic error”. Add code to stop this situation causing the program to crash. In your comments explain: When (after or before what) did you check for the arithmetic error? Why did you check for it there? What you are checking for? What happens if your check is positive/true?

2431/01/2016 Plenary Why would we want to join strings together and how do we do it? e.g. Putting a variable in a message: e.g. Putting a variable in a message: MsgBox (“My name is “ & Name & “. I am “ & Age & “ years old.”) MsgBox (“My name is “ & Name & “. I am “ & Age & “ years old.”)

2531/01/2016 Plenary What does an If structure test? An If structure tests a boolean condition. An If structure tests a boolean condition. What happens if this test returns True? If this test returns True then certain lines of code are executed. If this test returns True then certain lines of code are executed. What happens if this test returns False (remember to mention the variations of the If structure) ? Otherwise control passes to optional Else or ElseIf statements but ultimately the construct ends with an End If statement. Otherwise control passes to optional Else or ElseIf statements but ultimately the construct ends with an End If statement.