Visual Basic Statements

Slides:



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

Chapter 4: The Selection Structure Programming with Microsoft Visual Basic.NET, Second Edition.
Compunet Corporation1 Programming with Visual Basic.NET Selection Structure If-Else Week 4 Tariq Ibn Aziz.
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.
Slide 1 VB Program Flow Control. Slide 2 Making Decisions v Decision Statement: control the execution of parts of the program based on conditions. v The.
Basic Elements of Programming A VB program is built from statements, statements from expressions, expressions from operators and operands, and operands.
Introduction to Computing Dr. Nadeem A Khan. Lecture 4.
Introduction to Computing Dr. Nadeem A Khan. Lecture 7.
Comparing Numeric Values If Val(Text1.Text) = MaxPrice Then (Is the current numeric value stored in the Text property of Text1 equal to the value stored.
CSC110 Fall Chapter 5: Decision Visual Basic.NET.
Introduction to Computing Dr. Nadeem A Khan. Lecture 6.
Chapter 4: Control Structures: Selection
C++ for Engineers and Scientists Third Edition
A variable (X) is declared as an integer Data Type, meaning it can only accept numeric values The Text Box is named BoxContents The Variable X is assigned.
IS 1181 IS 118 Introduction to Development Tools VB Chapter 03.
For…Next : Nested Loop X= 0 For i = 0 To 4 For j = 0 To i-1 X=X+(i+j-1) Next j Next i How many times the inner loop is executed? What is x at the end of.
Computer Science 1000 Spreadsheets II Permission to redistribute these slides is strictly prohibited without permission.
Lesson 2 McManus COP  Computational ◦ problems involving some kind of mathematical processing  Logical ◦ Problems involving relational or logical.
Chapter 4: The Selection Structure
CS0004: Introduction to Programming Relational Operators, Logical Operators, and If Statements.
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.
Announcements 1st homework is due on July 16, next Wednesday, at 19:00 Submit to SUCourse About the homework: Add the following at the end of your code.
Unit 2 – Week 4 Reasoning with Linear Equations and Inequalities Lesson 1.
Lecture 8 Visual Basic (2).
1 Chapter 4: Selection Structures. In this chapter, you will learn about: – Selection criteria – The if-else statement – Nested if statements – The switch.
Chapter 4 Controlling the Flow Adapted From: Starting Out with Visual Basic 2012 (Pearson)
Lecture Set 5 Control Structures Part A - Decisions Structures.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 5 – Dental Payment Application: Introducing.
1 © 2000 John Urrutia. All rights reserved. Qbasic Looping Statements & Formatted Output.
Review of lecture 3 Data type and declaration INTEGER E.g., a, b, c REAL E.g., x, y, z, w LOGICAL COMPLEX CHARACTER Examples: INTEGER::a=1,b=-5,c=5 REAL::x=2.0.
Chapter 8 High-Level Programming Languages. 8-2 Chapter Goals Describe the translation process and distinguish between assembly, compilation, interpretation,
Flow of Control Part 1: Selection
Fall, 2006Selection1 Choices, Choices, Choices! Selection in FORTRAN Nathan Friedman Fall, 2006.
1 Week 2: Variables and Assignment Statements READING: 1.4 – 1.6 EECS Introduction to Computing for the Physical Sciences.
Lesson 5: Working with Formulas and Functions Logical and 3D Formulas.
Lecture #6 OPERATORS AND ITS TYPES By Shahid Naseem (Lecturer)
Conditional Expression One of the most useful tools for processing information in an event procedure is a conditional expression. A conditional expression.
Beginning Problem-Solving Concepts for the Computer
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.1.
CSE123 Lecture 3 Files and File ManagementScripts.
Chapter 51 Decisions Relational and Logical Operators If Blocks Select Case Blocks.
1 Scripting Languages VBScript - Recognized mainly by Internet Explorer only - Netscape does have a plug-in JavaScript - Recognized by Internet Explorer.
Review for Final (Part 2) School of Business Eastern Illinois University © Abdou Illia, Spring 2003 (Week 15, Friday 5/2/2003)
COMPUTER PROGRAMMING I 5.04 Apply Decision Making Structures.
Controlling Program Flow with Decision Structures.
Interduction to MATLAB (part 2) Manal Alotaibi Mathematics department College of science King saud university.
Visual Basic Review LBS 126. VB programming Project Form 1Form 2Form 3 Text boxButton Picture box Objects Text box Button Objects.
Loop and repetition. Today Passing values to and back from Sub procedures Option Buttons Do While Loops For Loops Population Growth.
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 11 So Many Paths … So Little Time.
Knowledge Base. Defining a Variable Dim statement Dim intXX As Integer Public in a Module Public dblNN As Double.
CSCI 3327 Visual Basic Chapter 4: Control Statements in Visual Basic (Part 2B) UTPA – Fall 2011 Part of the slides is from Dr. John Abraham’s previous.
1 VB-06-String Manipulation Mar 03, 2002 String Function VISUAL BASIC.
An Introduction to Programming with C++ Sixth Edition Chapter 5 The Selection Structure.
Slide 1 Chapter 3 Variables  A variable is a name for a value stored in memory.  Variables are created using a declaration statement. For example: Dim.
Relational and Logical Operators EE 201 1C7-2 Spring 2012.
OPERATORS IN C CHAPTER 3. Expressions can be built up from literals, variables and operators. The operators define how the variables and literals in the.
Computer Science Up Down Controls, Decisions and Random Numbers.
TUTORIAL 4 Visual Basic 6.0 Mr. Crone. Pseudocode Pseudocode is written language that is part-code part- English and helps a programmer to plan the layout.
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
A variable is a name for a value stored in memory.
Sequence, Selection, Iteration The IF Statement
Complex integers? Here a and b are integers.
The Selection Structure
Chapter 4: The Selection Structure
MATLAB (Lecture 2) BY:MAHA ALMOUSA.
VB Decisions, Conditions & If
VB Decisions & Conditions
Chapter 5 Decisions.
MATLAB (Lecture 2) BY:MAHA ALMOUSA.
OUTPUT DESIGN PRINT K, expression list K FORMAT (specification list)
Presentation transcript:

Visual Basic Statements

Relational Operators Equal  = Less than  < Greater than  > Not equal  <> Less than or equal  <= Greater than or equal  >=

Logical Expressions Let A = x^2 -50 & B = x *(x – 70) A < B True if A is less than B False if A is not less than B A > B A <> B True if A is not equal to B False if A is equal to B A <= B; A >= B; A = B

Logical Expressions String comparisons Equality FirstName = ”Allama” SecondName = “Muhammad” LastName = “Iqbal” Equality If FirstName=LastName Then… If LastName <> SecondName Then... Inequality (Alphabetic ordering) AFlag = FirstName > Last Name Same as AFlag = True

Logical Expressions String comparisons Less than  Comes before Greater than  Comes after Uppercase precedes lower case Blank spaces precede nonblank characters Shorter string precedes longer string Car precedes Cart

Logical Operators Not This  Not This And That  And This, That Or Both  Or Either This or That  Xor A Equivalent to B  Eqv A Implies B  Imp

Not A Not A X=3 Y=5 W = X > Y Z = Not X>Y W = False Z = True

And A B A And B False True (X > 10) And (X < 20) is True only when 10 < X <20

Or A B A Or B False True (X > 10) Or (X < 20) will always be True (X < 10) Or (X > 20) will be False when 10 ≤ X ≤ 20

Xor (Exclusive Or) A B A Xor B False True (X > 10) Xor (X < 20) will be False only when 11 ≤ X ≤ 19 (X < 10) Xor (X > 20) will be False when 10 ≤ X ≤ 20

Equivalent A B A Eqv B False True

Implies A B A Imp B False True

If…Then…End If If expression Then End If Statement(s) True Expression False

If…Then…Else…End If Else If expression Then End If Statement(s) I Statement(s) II End If Expression Statement(s) I True False Statement(s) II

Solution of Quadratic Equation Private Sub Command1_Click() Dim A As Integer, B As Integer, C As Integer Dim D As Single, X As Single, Y As Single Dim Ans1 As String, Ans2 As String Dim X1 As Single, X2 As Single ‘ Read Coefficients of the quadratic equation A = Val(Text1.Text) 'Coefficient of x^2 B = Val(Text2.Text) 'Coefficient of x C = Val(Text3.Text) 'Constant ‘ Evaluate Discriminant as D D = B ^ 2 - 4 * A * C

Solution of Quadratic Equation If D >= 0 Then ' Roots are real. Equal or unequal X1 = ( -B + Sqr(D) ) / (2 * A) X2 = ( -B - Sqr(D) ) / (2 * A) Ans1 = Str(X1) Ans2 = Str(X2) Else ' Roots are complex. Just need the absolute value of the imag. part X =Abs( Sqr(Abs(D)) / (2 * A)) Y = -B / (2 * A) Ans1 = Str(Y) & " + i " & Str(X) Ans2 = Str(Y) & " - i " & Str(X) End If

Solution of Quadratic Equation Text4.Text = Ans1 Text5.Text = Ans2 End Sub Assuming that 5 text boxes are created. Text1Text3 are used to input A, B & C Text4 & Text5 are used to display answers

If…Then…Else If…Else…End If If expressionA Then Statement set A Else If expressionB Statement Set B Else If expressionC Then . Else Statement Set N End If

If…Then…Else If…Else…End If False False Expression A Expression B Expression k False True True True Statement Set A Statement Set B Statement Set k Statement Set N

Use of If…Then…Else If… D = B ^ 2 - 4 * A * C If D < 0 Then Message = "Roots are complex" ElseIf D = 0 Then Message = "Roots are real & repeated" ElseIf D > 0 Then Message = "Roots are real & distinct" End If

Select Case . Select Case expression Case value set 1 Statement Set 1 Case Else Statement Set N End Select List separated by commas Values; 1, 3, 5 String(s); “a”, “5” Range using To Numbers; 6 To 8 String; “A” To “Z” , “0” To “9” Logical expression using Is Numbers: Is >= 9 String: Is < “h” Not necessary

Using Select Case D = B ^ 2 - 4 * A * C n = Sgn(D) Select Case n Message = "Complex roots" Case 0 Message = "Real repeated roots" Case 1 Message = "Real distinct roots" End Select

Using Select Case D = B ^ 2 - 4 * A * C Select Case D Case Is < 0 Message = "Complex roots" Case Is = 0 Message = "Real repeated roots" Case Is > 0 Message = "Real distinct roots" End Select

Using Select Case Dim Character As String Select Case Character Case "A" To "Z", "a" To "z" Message = "Character is Alphabet" Case "0" To "9" Message = "Character is Numeric" Case Else Message = "Character is not Alphanumeric" End Select

Format Library function Y = Format(x, “formatstring”) Returns the value of x in a format designated by “formatstring” Some built-in format functions Standard Currency Percent Scientific Long Date Medium Date

Format Examples Y = Format(1450.0365, “Standard”) Y = “1,450.04” Y = Format(1450.0365, “Currency”) Y = “$1,450.04” Y = Format(0.03657, “Percent”) Y = “3.66%” Y = Format(1450.0365, “Scientific”) Y = “1.45E+03”

Format Examples Y = Format(Now, “Long Date”) Y = “Wednesday, October 1, 2003” Y = Format(Now, “Medium Date”) Y = “01-Oct-03” Y = Format(Now, “hh:mm:ss am/pm”) Y = “09:07:23 am”

Format Examples Y = Format(“CS101”, “@@@@@@@@”) Y = “bbbCS101” b stands for blank. Or CS101 is right-justified. Y = Format(“CS101”, “&&&&&&&&”) Y = “CS101”  Right blanks are truncated b stands for blank. Or CS101 is left-justified. CS101

File Handling Files are used for large data input/output Create a .txt file called CarData by using Notepad (or ASCII file using other software) with following contents “Suzuki”, 3, 4 “Toyota”, 5, 7 “Honda”, 4, 15

File Handling Files must be Opened before any read/write operation. Files must be Closed after any read/write operation Syntax Open “CarData” For Input As #138 Opens a file called CarData.txt for read-only and this file will be called #138 The file reference number can be from 1 to 255

File Handling Close #138 Input #138, Make, NoOfModels, NoOfColors Assumes that the 1st line of “CarData.txt” contains 3 values corresponding to the three variables. Make should be a string variable, while NoOfModels and NoOfColors is a numeric type (Integer etc) After this Input statement Make = “Suzuki” NoOfModels = 3 NoOfColors = 4 Close #138

File Handling Dim Make As String, NoOfModels As Integer Dim NoOfColors As Integer Open "C:\CarData.txt" For Input As #1 Input #1, Make, NoOfModels, NoOfColors Open "C:\DataOut.txt" For Output As #2 Write #2, Make, NoOfColors, NoOfModels Close #1 Close #2

File Handling "Suzuki", 3, 4 "Toyota", 5, 7 "Honda", 4, 12 Contents of CarData.txt Contents of DataOut.txt

File Handling Dim Make As String, NoOfModels As Integer Dim NoOfColors As Integer, FMake As String Open "c:\CarData.txt" For Input As #1 Open "c:\DataOut.txt" For Output As #2 Input #1, Make, NoOfModels, NoOfColors FMake = Format(Make, "@@@@@@@@") Write #2, FMake, NoOfColors, NoOfModels Close #1 Close #2

File Handling "Suzuki", 3, 4 "Toyota", 5, 7 "Honda", 4, 12 Contents of CarData.txt Contents of DataOut.txt

File Handling Dim Make As String, NoOfModels As Integer Dim NoOfColors As Integer, FMake As String Open "c:\CarData.txt" For Input As #1 Open "c:\DataOut.txt" For Output As #2 Input #1, Make, NoOfModels, NoOfColors FMake = Format(Make, "@@@@@@@@") Write #2, FMake, NoOfColors, NoOfModels FMake = Format(Make, "&&&&&&&&") Close #1 Close #2

File Handling "Suzuki", 3, 4 "Toyota", 5, 7 "Honda", 4, 12 Contents of CarData.txt Contents of DataOut.txt