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

Slides:



Advertisements
Similar presentations
What Does This Program Do?
Advertisements

CSI 1306 PROGRAMMING IN VISUAL BASIC PART 2. Part 2  1. Strings  2. Translating Conditional Branch Instructions  3. Translation Set 2  4. Debugging.
A number of MATLAB statements that allow us to control the order in which statements are executed in a program. There are two broad categories of control.
5.04 Apply Decision Making Structures
Chapter 5 Decisions. Outline and Objectives Relational and Logical Operators If Blocks Select Case Blocks.
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.
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.
Introduction to Computing Dr. Nadeem A Khan. Lecture 6.
Visual Basic Statements
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.
Implementing Control Logic in C# Svetlin Nakov Telerik Corporation
© 1999, by Que Education and Training, Chapter 5, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach.
Lesson 2 McManus COP  Computational ◦ problems involving some kind of mathematical processing  Logical ◦ Problems involving relational or logical.
1 Lab Session-III CSIT-120 Fall 2000 Revising Previous session Data input and output While loop Exercise Limits and Bounds Session III-B (starts on slide.
Chapter 4: The Selection Structure
Chapter 4 The If…Then Statement
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.
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.
1 © 2000 John Urrutia. All rights reserved. Qbasic Looping Statements & Formatted Output.
Flow of Control Part 1: Selection
Fall, 2006Selection1 Choices, Choices, Choices! Selection in FORTRAN Nathan Friedman Fall, 2006.
Input Textboxes Input Boxes Different than textboxes Good for small amount of input (form full of textboxes is not nice) X = Inputbox(“prompt message”,
CS285 Visual Basic 2 Department of Computing UniS 1 Statements in Visual Basic A statement is the fundamental syntactical element of a program smallest.
Numerical Functions & Tricks In today’s lesson we will look at: why we might want to use mathematical techniques some useful functions other mathematical.
1 Example: Solution of Quadratic Equations We want to solve for real values of x, for given values of a, b, and c. The value of x can be determined from.
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.
Introduction to Programming Lecture Note - 2 Visual Basic Programming Fundamentals.
Review for Final (Part 2) School of Business Eastern Illinois University © Abdou Illia, Spring 2003 (Week 15, Friday 5/2/2003)
Pay Example (PFirst98) Please use speaker notes for additional information!
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.
CIVIL AND GEOMATIC ENGINEERING FT Okyere. CIV 257- COMPUTER PROGRAMMING Lecture 3.
110 E-1 Variables, Constants and Calculations(2) Chapter 3: Operations on variables, scope of a variable, formatting data Doing Arithmetic.
5.1 Introduction Problem Solving –Requires understanding of: Building blocks Program-construction principles BZUPAGES.COM.
Controlling Program Flow with Decision Structures.
Visual Basic Review LBS 126. VB programming Project Form 1Form 2Form 3 Text boxButton Picture box Objects Text box Button Objects.
 2002 Prentice Hall. All rights reserved. 1 Chapter 5 – Control Structures: Part 2 Outline 5.1Introduction 5.2 Essentials of Counter-Controlled Repetition.
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.
CSC 162 Visual Basic I Programming. String Functions LTrim( string ) –Removes leading spaces from the left side of string RTrim( string ) –Removes trailing.
Chapter 4.  Variables – named memory location that stores a value.  Variables allows the use of meaningful names which makes the code easier to read.
Slide 1 Chapter 3 Variables  A variable is a name for a value stored in memory.  Variables are used in programs so that values can be represented with.
© 2006 Lawrenceville Press Slide 1 Chapter 4 Variables  A variable is a name for a value stored in memory.  Variables are created using a declaration.
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.
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.
VISUAL BASIC 6.0 Designed by Mrinal Kanti Nath.
A variable is a name for a value stored in memory.
Visual Basic 6 (VB6) Data Types, And Operators
Complex integers? Here a and b are integers.
The Selection Structure
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.
3rd prep. – 2nd Term MOE Book Questions.
3rd prep. – 2nd Term MOE Book Questions.
Relational Operators Operator Meaning < Less than > Greater than
Selection Statements Chapter 4 Attaway MATLAB 4E.
Chapter (3) - Looping Questions.
VB Decisions, Conditions & If
VB Decisions & Conditions
Chapter 5 Decisions.
Selection Statements Chapter 4 Attaway MATLAB 5E.
Presentation transcript:

Visual Basic Statements Chapter 5

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

Logical Expressions ExpressionValueCondition A < BTrueA is less than B FalseA = B or A > B A > BTrueA is greater than B FalseA = B or A < B A <> BTrueA ≠ B FalseA = B Let A = x^ Let B = x*(x-70)

Logical Expressions  String comparisons 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 = False

Logical Expressions  String comparisons Less than  Comes before Greater than  Comes after  Uppercase precedes lower case  “ANIMAL” precedes “aNIMAL” (ANIMAL < aNIMAL)  Blank spaces precede nonblank characters  “An imal” precedes “Animal” (An imal < Animal)  String formed by appending characters to another string follows the original string  “Car” precedes “Cart” (Car < Cart)

Logical Operators  Not This  Not  This And That  And  This, That Or Both  Or  Either This or That  Xor

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

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

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

Xor (Exclusive Or) ABA Xor B False True FalseTrue False (X > 10) Xor (X < 20) (X 20) will be False when 10 < X < 20 will be False when 10 ≤ X ≤ 20

If…Then…End If If expression Then Statement(s) End If

Quadratic Equation Roots Private Sub Command1_Click() Dim A as Single, B As Single, C As Single Dim D As Single, Message As String A = Val(Text1.Text)... D = B ^ * A * C If D < 0 Then Message = "Roots are complex" End If If D >= 0 Then Message = "Roots are real" End If Picture1.Print Message End Sub

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

Quadratic Equation Roots Private Sub Command1_Click() Dim A as Single, B As Single, C As Single Dim D As Single, Message As String A = Val(Text1.Text)... D = B ^ * A * C If D < 0 Then Message = "Roots are complex" Else Message = "Roots are real" End If Picture1.Print Message End Sub

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

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

Use of If…Then…Else If… Private Sub Command1_Click() Dim A as Single, B As Single, C As Single Dim D As Single, Message As String... D = B ^ * 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 Picture1.Print Message End Sub

If…Then…  Read username from Text1 and write (Username) in (case) letters is (USERNAME) in Text3 where case is read from Text2 and is either Capital or Small.

If…Then… UserName=Text1.Text Opt = Text2.Text If Opt = “Capital” Then OutStr=UserName+” in “+Opt+” letters is “+Ucase(Username) Else OutStr=UserName+” in “+Opt+” letters is “+Lcase(Username) End If Text3.Text=OutStr  What if the user typed capital in Text2?  What if the user entered Urdu in Text2?

If…Then… Private Sub Command1_Click() Dim Username As String, Opt As String, OutStr As String Username = Text1.Text Opt = Text2.Text If UCase(Opt) = "CAPITAL" Then OutStr = Username + " in " + Opt + " letters is " + UCase(Username) ElseIf UCase(Opt) = "SMALL" Then OutStr = Username + " in " + Opt + " letters is " + LCase(Username) Else MsgBox "Please enter “”Capital”” or “”Small”” ",, "ERROR" End If Text3.Text = OutStr End Sub

Select Case Select Case expression Case value set 1 Statement Set 1 Case value set 2.. 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

Select Case Private Sub Command1_Click() Username = Text1.Text Opt = Text2.Text Select Case LCase(Opt) Case “capital" OutStr = Username + " in " + Opt + " letters is " + UCase(Username) Case “small" OutStr = Username + " in " + Opt + " letters is " + LCase(Username) Case Else MsgBox "Please enter ""Capital"" or ""Small""",, "ERROR" End Select Text3.Text = OutStr End Sub

Using Select Case Private Sub Command1_Click() Dim A as Single, B As Single, C As Single Dim D As Single, Message As String D = B ^ * A * C Select Case Sgn(D) Case -1 Message = "Complex roots" Case 0 Message = "Real repeated roots" Case 1 Message = "Real distinct roots" End Select Picture1.Print Message End Sub

Using Select Case D = B ^ * 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 from Alphabet" Case "0" To "9" Message = "Character is Numeric" Case Else Message = "Character isn’t Alphanumeric" End Select

Cost of a phone call  Read CallDuration in seconds  Billing in minutes  Each minute costs Rs 5 up to 10 minutes  Longer than 10 minute call has a flat rate of Rs. 200 per call  second call costs Rs. 15  601 second call costs Rs. 200

Phone call cost Private Sub Command1_Click() Dim Duration As Integer, Minutes As Single, cost As Integer Duration = Val(Text1.Text) Minutes = Duration / 60 If Minutes - Int(Minutes) > 0 Then Minutes = Int(Minutes) + 1 End If If Minutes > 10 Then cost = 200 Else cost = 5 * Minutes End If Text2.Text = Str(cost) End Sub

Phone call cost Private Sub Command1_Click() Dim Duration As Integer, Minutes As Single, cost As Integer Duration = Val(Text1.Text) Minutes = Duration / 60 If Minutes - Int(Minutes) > 0 Then Minutes = Int(Minutes) + 1 End If Select Case Minutes Case Is > 10 cost = 200 Case Else cost = 5 * Minutes End Select Text2.Text = Str(cost) End Sub Minutes = -Int(-Duration / 60)

Roman Numbers  Write a program that takes decimal numbers (1 – 9) as input, and writes their Roman equivalent. 1  I 2  II 4  IV Extend this program to numbers 1 – 1000 Writing 1000 case or elseif are not good ideas.

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 ^ * 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 Y =Abs( Sqr(Abs(D)) / (2 * A)) X = -B / (2 * A) Ans1 = Str(X) & " + i " & Str(Y) Ans2 = Str(X) & " - i " & Str(Y) 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

Problem  Convert a decimal number into a binary number. Assume that the decimal number is from 0 to 255. Always represent a number in 8 bits We need to find remainders 8 times Repeat the following set of operations 8 times Remainder = DecimalNo Mod 2 DecimalNo = DecimalNo \ 2 BinaryNo = Str(R) + BinaryNo

Decimal to Binary Conversion Private Sub Command1_Click() Dim DecimalNo As Integer Dim Remainder As Integer Dim BinaryNo As String DecimalNo = Val(Text1.Text) Remainder = DecimalNo Mod 2 DecimalNo = DecimalNo \ 2 BinaryNo = Str(Remainder) + BinaryNo Remainder = DecimalNo Mod 2 DecimalNo = DecimalNo \ 2 BinaryNo = Str(Remainder) + BinaryNo Remainder = DecimalNo Mod 2 DecimalNo = DecimalNo \ 2 BinaryNo = Str(Remainder) + BinaryNo Remainder = DecimalNo Mod 2 DecimalNo = DecimalNo \ 2 BinaryNo = Str(Remainder) + BinaryNo Remainder = DecimalNo Mod 2 DecimalNo = DecimalNo \ 2 BinaryNo = Str(Remainder) + BinaryNo Remainder = DecimalNo Mod 2 DecimalNo = DecimalNo \ 2 BinaryNo = Str(Remainder) + BinaryNo Remainder = DecimalNo Mod 2 DecimalNo = DecimalNo \ 2 BinaryNo = Str(Remainder) + BinaryNo Remainder = DecimalNo Mod 2 DecimalNo = DecimalNo \ 2 BinaryNo = Str(Remainder) + BinaryNo Picture1.Print BinaryNo End Sub

Decimal to Binary : Loop Private Sub Command1_Click() Dim DecimalNo As Integer, Remainder As Integer Dim BinaryNo As String DecimalNo = Val(Text1.Text) For Bit = 1 To 8 Remainder = DecimalNo Mod 2 DecimalNo = DecimalNo \ 2 BinaryNo = Str(Remainder) + BinaryNo Next Bit Picture1.Print BinaryNo End Sub

Loops  In several problems we need to execute same or similar set of statements many times. Re-type the statements as many times  Use loops

For…Next : Syntax For index = value1 To value2 Executable statement(s) Next index For index = value1 To value2 Step value3 Executable statement(s) Next index  value3 = 1 when Step is missing

For..Next; Flow chart value2 > value1; value3>0 value2 < value1; value3<0

For…Next: Rules & Cases  Altering the value of the index is not good programming practice.  Control can be transferred-out, but not in  If (value1=value2) AND (value3<>0)  The loop is executed exactly once  The loop will not be executed if  (value1>value2) And (value3 >=0)  (value1<value2) And (value3<0)

For…Next : Loop execution Value3Loop Executes if Positive or 0Index <= value2 NegativeIndex >= value2

For…Next : Examples For k = 1 To 10 Picture1.Print k Next k For k = 5 To 10 Picture1.Print k Next k

For…Next : Examples For k = -10 To 0 Picture1.Print k Next k For k = 5 To 1 Step -1 Picture1.Print k Next k

For…Next : Index example Private Sub Command1_Click() Dim value1 As Integer, Value2 As Integer, Value3 As Integer Dim Index As Integer value1 = Val(Text1.Text) value2 = Val(Text2.Text) value3 = Val(Text3.Text) Picture1.Cls For Index = value1 To value2 Step value3 Picture1.Print Str(Index) + ", "; Next Index Picture1.Print "Index on loop exit " + Str(Index) End Sub

For…Next : Word Reversal Private Sub Command1_Click() Dim Word As String, Letter As String Dim Index As Integer Picture1.Cls Word = Text1.Text For Index = Len(Word) To 1 Step -1 Letter = Mid(Word, Index, 1) Picture1.Print Letter; Next Index End Sub Private Sub Command2_Click() Dim Word As String, Letter As String Dim Index As Integer Picture1.Cls Word = Text1.Text For Index = Len(Word) To 1 Step -1 Letter = LCase(Mid(Word, Index, 1)) If Index = Len(Word) Then Letter = UCase(Letter) End If Picture1.Print Letter; Next Index End Sub

For…Next : Nested Loops  Write a program to display multiplication tables 1  4.

Nested Loops: Tables Private Sub Tables_Click() Dim i As Integer, j As Integer For i = 1 To 10 For j = 1 To 4 Picture1.Print Str(j) + " x " + Str(i) + " = " + Str(j * i) + " "; Next j Picture1.Print Next i End Sub