Input Textboxes Input Boxes Different than textboxes Good for small amount of input (form full of textboxes is not nice) X = Inputbox(“prompt message”,

Slides:



Advertisements
Similar presentations
Chapter 4 - Control Statements
Advertisements

Objectives Understand the software development lifecycle Perform calculations Use decision structures Perform data validation Use logical operators Use.
Tutorial 12: Enhancing Excel with Visual Basic for Applications
VB PROJECT “PROJECT SAMPLES”. For Next Loops Design a VB program that displays in a picture box the first N multiples of an input integer Input 3 exam.
Example 2.
Loops Counting down from N to 1 in assembly we used JUMP statements to repeat previous instructions thus looping READ LOOP: WRITE SUB decrement JPOSLOOP.
Iteration (Looping Constructs in VB) Iteration: Groups of statements which are repeatedly executed until a certain test is satisfied Carrying out Iteration.
Input Validation Check the values entered into a text box before beginning any calculations Validation is a form of ‘self-protection’, rejecting bad data.
Computer Science 1620 Programming & Problem Solving.
5.05 Apply Looping Structures
Presented by Joaquin Vila Prepared by Sally Scott ACS 168 Problem Solving Using the Computer Week 12 Boolean Expressions, Switches, For-Loops Chapter 7.
Creating Embedded Formative Assessment Dr. Steve Broskoske Misericordia University EDU 533 Computer-based Education.
Intrinsic Functions Pre-coded Functions Used to improve developer productivity Broad Range of Activities Math calculations Time/Date functions String.
Copyright © 2008 Pearson Prentice Hall. All rights reserved. 1 Microsoft Office Excel Copyright © 2008 Pearson Prentice Hall. All rights reserved
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
Chapter 4: The Selection Process in Visual Basic.
Conditions. Objectives  Understanding what altering the flow of control does on programs and being able to apply thee to design code  Look at why indentation.
Making Decisions. 4.1 Relational Operators Used to compare numbers to determine relative order Operators: > Greater than < Less than >= Greater than.
30/10/ Iteration Loops Do While (condition is true) … Loop.
COMPUTER PROGRAMMING I 5.05 Apply Looping Structures.
CS285 Visual Basic 2 Department of Computing UniS 1 Statements in Visual Basic A statement is the fundamental syntactical element of a program smallest.
Chapter 16: Programming Structures Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University Name.
Chapter 8 - Visual Basic Schneider
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.
Agenda Basic Logic Purpose if statement if / else statement
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.1.
Chapter 16: Programming Structures Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University Name.
Variables & Function Calls. Overview u Variables  Programmer Defined & Intrinsic  Data Types  Calculation issues u Using Functions  The val() function.
Two Forms Please use speaker notes for additional information!
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
Chapter Making Decisions 4. Relational Operators 4.1.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.1.
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.
VB CSCI130 Instructor: Dr. Imad Rahal LAYEROrder Application SW: Excel & Access2 High-order P.L.: Visual Basic1 Low-order P.L.: Assembly3 System Software:
Structured Programming II: If Statements By the end of this class you should be able to: implement branching in a program describe and use an “if” statement.
Chapter 4 Getting Started with VBA. Subroutines Subroutine is the logical section of code that performs a particular task. Subroutine is also called a.
31/01/ Selection If selection construct.
110 E-1 Variables, Constants and Calculations(2) Chapter 3: Operations on variables, scope of a variable, formatting data Doing Arithmetic.
Controlling Program Flow with Looping Structures
5.1 Introduction Problem Solving –Requires understanding of: Building blocks Program-construction principles BZUPAGES.COM.
Variables and Expressions Programming Right from the Start with Visual Basic.NET 1/e 7.
Controlling Program Flow with 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.
Chapter 2: Fundamental Programming Structures in Java Adapted from MIT AITI Slides Control Structures.
Copyright © Don Kussee 1410-Ch5 #1031 CNS 1120 Chapter 5 Selection statements 1120-Ch5.PPT.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Creation of Variables with Numeric, alphanumeric, date, picture, memo data types Constant - A quantity that does not change during the execution of a program.
Lecture 6 – Selection FTMK, UTeM – Sem /2014.
CIS 338: VB Variables Dr. Ralph D. Westfall April, 2011.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Subroutines (PrArith, Math,projCP1, PrAdrProc, PrAdrProcFunc) Please use speaker notes for additional information!
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.
Chapter 6 Controlling Program Flow with Looping Structures.
Visual Basic 6 (VB6) Data Types, And Operators
The Selection Structure
Chapter 3: Introduction to Problem Solving and Control Statements
Iteration: Beyond the Basic PERFORM
If selection construct
Do While (condition is true) … Loop
Input and Output.
If selection construct
Language Constructs Construct means to build or put together. Language constructs refers to those parts which make up a high level programming language.
Chapter 3: Selection Structures: Making Decisions
Chapter 3: Selection Structures: Making Decisions
Input and Output.
Input and Output Chapter 3.5
Presentation transcript:

Input Textboxes Input Boxes Different than textboxes Good for small amount of input (form full of textboxes is not nice) X = Inputbox(“prompt message”, “title message”) The input is assigned to variable X when the use hits OK A = Inputbox(“Enter an exam score”,”Exam 1”)

Output Pictureboxes Message Boxes For output specially in situations of errors different than pictureboxes MsgBox “prompt message”,,“title message” MsgBox “Sorry but you must enter a positive number”,, “Error”

Output Display (pictureboxes) Spacing, (comma) … ALIGN OUTPUT Every picture box is divided into 14-space print zones Jumps to the next zone (does not jump 14 spaces necessarily!) From its current location (its current 14-space zone) Results.Print 5,30,2 Tab function: Tab(X) Followed by a ; (automatically inserted by VB) leaves X spaces from start Pushes any overlap to new lines Results.Print Tab(2); “Hi!” Results.Print Tab(2); “Hi!”; Tab(2); “Bi!”; What should the second 2 be to print on the same line A comma or semi-colon at the end of a print statement prints next print statement on the same line

Output Display FormatCurrency(x,y) --- y is 2 by default x is the decimal number to be formatted y is the number of digits allowed after the decimal point (rounding) Extra 0s are appended Adds a $ sign and commas if needed FormatCurrency( ,2) = $1, FormatCurrency(1234.2,2) = ? FormatNumber(x,y) --- y is 2 by default Rounds x to the nearest number of digits after the decimal point specified by y FormatNumber(0.5235) = 0.52 FormatNumber(0.5235,3) = 0.524

Variable Scope Where we declare a variable defines its scope i.e. where it is known Determines what subroutines can access it Subroutine-level: Within a subroutine  only in that subroutine Form-level: At top of the form (before the code for the first subroutine under Option Explicit) Not within any subroutine Accessible by all SUBROUTINES for that form

Conditional Statements So far, we’ve been executing all the instructions in a program PC is always increment by 1 What if we want to do things under certain conditions only! We used JUMP commands to achieve that in machine/assembly language Loop if AC is positive (JPOS) In VB we have the If AND Select statements for this purpose

Program to print an input number back on screen if it is positive READ STOR X JPOS PRINT_LABEL HALT PRINT_LABEL: LOAD X WRITE HALT X:0 Input X X > 0 Output X HALT YesNo Flow Chart (Algorithm) vs. Pseudocode

If Statements We want to execute something only when a certain condition is met If (condition) then Statements to execute End If Read student grade using an inputbox If 90 or more, print the following in a message box “Congratulations on your hard work!” True? Do action NO YES

If Statements Private Sub cmdGrade_Click() Dim grade As Single grade = Inputbox (“Please enter a score”) If grade >= 90 then MsgBox “Congratulations on your hard work!” End If End Sub

Allowed Comparison operations Equals= Greater than> Less than< Greater than or equal>= Less than or equal<= Not equal (i.e. different)<>

If Statements What if grade is less than 90? Nothing happens Not very good! If statement with the else clause If condition then Statements to execute if condition is met Else Statements to execute if condition is NOT met End If If grade>=90 then display congrats message; otherwise, display support message True? Do YES action NO YES Do NO action

If Statements Private Sub Grade_Click() Dim grade As Single grade = Inputbox (“Please enter a score”,”Score”) If grade >= 90 then MsgBox “Congratulations on your hard work!”,,”YES” Else MsgBox “DUDE!!! Try harder next time!”,,”NO” End If End Sub Notice the indentation Private Sub/End Sub IF/End If/Else

If Statements Compound statements for compound conditions More than one condition can appear in a statement which can be joined by either AND  both must be true OR  at least one must be true AND take precedence over OR Just like in Circuits Detect whether input grade is valid or invalid (i.e. outside [0,100]) Cel/Fah conversion but limit input to -100,+100 If not valid, inform user

Find Output Dim X As Integer, Y as Integer, Z as String X = 5 Y = 2*X Z = “CSCI-130” If Z = “PHIL-130” OR X<>4 AND Y>9 Then Msgbox “Good Job” Else “Even Better” End If

1.Create a VB project to perform simple bank withdrawals 2.If the withdrawn amount is larger than the beginning balance, the user is warned and nothing happens to the balance … display warning in a message box 3.Otherwise, the new balance is shown in the picture box like the following: The new balance is $ Also, if the new balance is below $150.00, the user is informed The new balance is $89.12 New balance is below $150.00

Select Statements Select Case grade Cases Actions End Select Equalities Case 100 Results.Print “A + ” Lists Case 100, 99, 98, 97 Results.Print “A + ” Ranges (e.g. 60 to 69) Case 60 to 69 Results.Print “D” Inequalities Case Is >=90 Results.Print “A” Is only here Order of cases is very important in Select Statements First match first Select Case grade Case Is >=90 Results.Print “A” Case 100 Results.Print “A+” … Would we ever print an A+?

Select Statements The Select Case statement Useful when we have a single variable with several options each requiring a different action Select Case grade Case 90 to 100 Results.Print “A” Case 80 to 89 Results.Print “B” Case 70 to 79 Results.Print “C” Case 60 to 69 Results.Print “D” Case 0 to 60 Results.Print “F” Case Else Results.Print “Sorry. You entered an unacceptable number for a grade” End Select E.g. of an unacceptable number Fix to handle floating-point grades