Introduction to Computing Dr. Nadeem A Khan. Lecture 4.

Slides:



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

Chapter 5 Decisions. Outline and Objectives Relational and Logical Operators If Blocks Select Case Blocks.
Introduction to Computing Dr. Nadeem A Khan. Lecture 21.
Introduction to Computing Dr. Nadeem A Khan. Lecture 9.
Introduction to Computing Dr. Nadeem A Khan. Lecture 22.
Introduction to Computing Dr. Nadeem A Khan. Lecture 8.
Introduction to Computing Dr. Nadeem A Khan. Lecture 10.
Introduction to Computing Dr. Nadeem A Khan. Lecture 7.
Introduction to Computing Dr. Nadeem A Khan. Lecture 4.
Introduction to VB prepared by Dr. I A Moneim Reference Book Schneider.
Introduction to Computing Dr. Nadeem A Khan. Lecture 4.
Introduction to Computing Dr. Nadeem A Khan. Lecture
Introduction to Computing Dr. Nadeem A Khan. Lecture 24.
Introduction to Computing Dr. Nadeem A Khan. Lecture 17.
Introduction to Computing Dr. Nadeem A Khan. Lecture 19.
Introduction to Computing Dr. Nadeem A Khan. Lecture 11.
Introduction to Computing Dr. Nadeem A Khan. Lecture 7.
Introduction to Computing Dr. Nadeem A Khan. Lecture 14.
Introduction to Computing Dr. Nadeem A Khan. Lecture 6.
Visual Basic Statements
Introduction to Computing Dr. Nadeem A Khan. Lecture 13.
Introduction to Computing Dr. Nadeem A Khan. Lecture 18.
Introduction to Computing Dr. Nadeem A Khan. Lecture 8.
Introduction to Computing Dr. Nadeem A Khan. Lecture 9.
Introduction to Computing Dr. Nadeem A Khan. Lecture 5.
Section 3.6 BUILT-IN FUNCTIONS involving numbers & strings.
Copyright © 2001 by Wiley. All rights reserved. Chapter 3: Variables, Assignment Statements, and Arithmetic Variables Assignment Statements Arithmetic.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Streams Streams –Sequences of characters organized.
Binary Real Numbers. Introduction Computers must be able to represent real numbers (numbers w/ fractions) Two different ways:  Fixed-point  Floating-point.
Chapter 9 Formatted Input/Output. Objectives In this chapter, you will learn: –To understand input and output streams. –To be able to use all print formatting.
CSCI 3327 Visual Basic Chapter 4: Control Statements in Visual Basic (Part 1A) UTPA – Fall 2011.
CS0004: Introduction to Programming Relational Operators, Logical Operators, and If Statements.
Input & Output: Console
National Diploma Unit 4 Introduction to Software Development Data types, variables and constants.
Information and Programs. Foundations of Computing Information –Binary numbers –Integers and Floating Point –Booleans (True, False) –Characters –Variables.
Copyright © 2002 W. A. Tucker1 Chapter 7 Lecture Notes Bill Tucker Austin Community College COSC 1315.
Computer Science Selection Structures.
CSC 162 Visual Basic I Programming. Randomizing and Formatting Randomizing Formatting –User-Defined Formats –Named Numeric Formats.
Chapter 5 Decisions. Outline and Objectives Relational and Logical Operators If Blocks Select Case Blocks.
7-1 Chapter 7.  Basic Arithmetic Verbs  Options Available with Arithmetic Verbs  COMPUTE Statement  Signed Numbers in Arithmetic Operations  Intrinsic.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 3 Variables, Constants, Methods, and Calculations.
Review With packagename ; Use packagename ; Procedure procedurename Is declaration of variables & constants Begin statements of program End procedurename.
Chapter 51 Decisions Relational and Logical Operators If Blocks Select Case Blocks.
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.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Review for Final (Part 2) School of Business Eastern Illinois University © Abdou Illia, Spring 2003 (Week 15, Friday 5/2/2003)
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 2A Reading, Processing and Displaying Data (Concepts)
CECS 5020 Computers in Education Visual Basic Variables and Constants.
Variables Continued In the last session we saw how variables are objects that allow us to store values in the RAM of the computer In this session we shall.
Introduction to Computing Dr. Nadeem A Khan. Lecture 7.
EGR 115 Introduction to Computing for Engineers Branching & Program Design – Part 3 Friday 03 Oct 2014 EGR 115 Introduction to Computing for Engineers.
Introduction to Computing Dr. Nadeem A Khan. Lecture 9.
Controlling Program Flow with Decision Structures.
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.
Loop and repetition. Today Passing values to and back from Sub procedures Option Buttons Do While Loops For Loops Population Growth.
Selection Part 2 Using Logical Operators, how strings are compared and random numbers.
Introduction to Computing Dr. Nadeem A Khan. Lecture 21.
1 CE 454 Computer Architecture Lecture 4 Ahmed Ezzat The Digital Logic, Ch-3.1.
A variable is a name for a value stored in memory.
BINARY CODE.
Visual Basic 6 (VB6) Data Types, And Operators
The Selection Structure
Data Types, Identifiers, and Expressions
CSI 101 Elements of Computing Spring 2009
Chapter 5 - Visual Basic Schneider
Sub Procedures and Functions
Computing in COBOL: The Arithmetic Verbs and Intrinsic Functions
VB Decisions & Conditions
Chapter 5 Decisions.
Presentation transcript:

Introduction to Computing Dr. Nadeem A Khan

Lecture 4

Local vs Form Variables

Not only +,-,*,^ But also: But also: \ opeartor e.g: 5.1\2.04= 2 \ opeartor e.g: 5.1\2.04= 2 MOD operator e.g: 15.2 MOD 6=3 MOD operator e.g: 15.2 MOD 6=3 => First round to closest integers before operation More on Operators

More on Operators (Contd.) ► Operator Precedence 1. ^ 2.- operator (indicating a negative value) 3. * and / operator 4. \ operator 5. MOD operator 6. + and - operator

Built-in Functions (Contd.) ► Format$ Format$(num, fmt) num: number, numeric expression, string of a number fmt: format string Result => String consisting of a formatted version of the number

Built-in Functions (Contd.) ► FunctionString Value Format$( , “Standard”)12, Format$( , “Currency”)$12, Format$(-1234, “Standard”)-1, Format$(-1234, “Standard”)-1, Format$(-1234, “Currency”)($1,234.00) Format$(-1234, “Currency”)($1,234.00) =>Result: two digits after decimal; Commas every three places to the left of the decimal point; leading ‘$’ sign for currency

Built-in Functions (Contd.) ► FunctionString Value Format$(1/4, “Standard”)? Format$(“.2”, “Currency”)? Format$( , “Currency”)?

Built-in Functions (Contd.) ► FunctionString Value Format$(1/4, “Standard”)0.25 Format$(“.2”, “Currency”)$0.20 Format$( , “Currency”)($1,234.80)

Built-in Functions (Contd.) Other functions Other functions ► FunctionString Value Format$( , “#,0”)12,346 Format$(-3.6, “#,0”)-4 Format$( , “Percent”) % Format$( , “Scientific”)1.23E+04 Format$(2/3/03, “Long Date”)Monday February 3, 2003 Format$(2/3/03, “Medium Date”)03-Feb-03 =>More examples on following slides

Built-in Functions (Contd.) ► FunctionString Value Format$( , “#,0”)12,346 Format$(-3.6, “#,0”)-4 =>Result: Rounded; =>Result: Rounded; Commas every three places to the left of the decimal point

Built-in Functions (Contd.) ► FunctionString Value Format$(123.82, “#,0”)? Format$(-3.2, “#,0”)?

Built-in Functions (Contd.) ► FunctionString Value Format$( , “Percent”) % =>Result: Multiplied by 100; trailing % sign; two digits after decimal

Built-in Functions (Contd.) ► FunctionString Value Format$(.06265, “Percent”)? Format$(1/8, “Percent”)?

Built-in Functions (Contd.) ► FunctionString Value Format$(.06265, “Percent”)6.27% Format$(1/8, “Percent”)12.50%

Built-in Functions (Contd.) ► FunctionString Value Format$( , “Scientific”)1.23E+04 =>Result: first number between 1 and 9.99 and of two digits after decimal; and of two digits after decimal; Exponent value preceded by E and sign; Exponent value preceded by E and sign;

Built-in Functions (Contd.) ► FunctionString Value Format$( , “Scientific”)? Format$(1/8, “Scientific”)?

Built-in Functions (Contd.) ► FunctionString Value Format$( , “Scientific”)-6.00E+02 Format$(1/8, “Scientific”)-1.25E-01

Built-in Functions (Contd.) Formatting dates: Formatting dates: ► FunctionString Value Format$(“7/4/96”, “Long Date”)Thursday, July 4, 1996 Format$(“7/4/96”, “Medium Date”) 04-Jul-96

Built-in Functions (Contd.) Fixed length string formatting: Fixed length string formatting: ► FunctionString Value Format$( , Format$(123, Format$(“ ”, Format$(“$1,234.56”, Format$(1/4,

Generating Random Numbers The function: Rnd The function: Rnd ► Generates a random number from 0 up to but not including 1 Picture1.Print Rnd‘print a different number each time Let numvar= Rnd‘a random value is assigned

Generating Random Numbers (Contd.) The function: Rnd The function: Rnd Display numbers from the set {1,2,3,4,5,6} randomly!

Generating Random Numbers (Contd.) The statement: Randomize Timer? The statement: Randomize Timer?

Generating Random Numbers (Contd.) The statement: Randomize Timer The statement: Randomize Timer Sub Command1_Click ( ) Rem Display a lottery number Picture1.Cls Randomize Timer Picture1.Print Int(10*Rnd); Picture1.Print Int(10*Rnd) End Sub

Sub Text1_KeyPress(KeyAscii as Integer) statements statements End Sub ► Text1_KeyPress event will occur when Text1 has the focus and a key is pressed Text1 has the focus and a key is pressed The Keypress Event Procedure

Sub Text1_KeyPress(KeyAscii as Integer) statements statements End Sub ► Keyascii  is a variable (of type Integer)  gets the ANSI value of the pressed key  value is used to display the corresponding character in the Text1 at the end of this procedure The Keypress Event Procedure (Contd.)

The ANSI (ASCII) Code ► A 7 bit code representing one of the 95 characters (including space) ► Normally one byte is used to store this code

What will happen in these cases? Sub Text1_KeyPress(KeyAscii as Integer) Let KeyAscii =65 Let KeyAscii =65 End Sub Sub Text1_KeyPress(KeyAscii as Integer) Let KeyAscii =0 Let KeyAscii =0 End Sub The Keypress Event Procedure (Contd.)

Read Chapter 4 completely

Decisions

Decision Structure: Flowchart Process Step (s) 2 Is Condition True Process Step (s) 1

IF BLOCKS IF condition Then action1Else action 2 action 2 End If

IF BLOCK (Contd.) ► Complete the program: Identifies and displays the larger value and its variable (assume unequal values) Sub Command1_Click Dim a As Single, b As Single, largerVal As Single Let a=4 Let b=5... Picture1. Print “Its value is:”;largerVal End Sub

IF BLOCK (Contd.) Sub Command1_Click Dim a As Single, b As Single, largerVal As Single Let a=4 Let b=5 If a>b Then Picture1.Print “a has the larger value” largerVal=aElse Picture1.Print “b has the larger value” largerVal=b End If Picture1. Print “Its value is:”;largerVal End Sub

IF BLOCK (Contd.) Result: b has the larger value Its value is: 5

IF BLOCK (Contd.) What the following program will do?

IF BLOCK (Contd.) Sub Command1_Click Dim a As Single, b As Single, largerVal As Single Let a=4 Let b=4 If (a>b) Or (a=b) Then Picture1.Print “a has the larger value” largerVal=aElse Picture1.Print “b has the larger value” largerVal=b End If Picture1. Print “Its value is:”;largerVal End Sub

IF BLOCK (Contd.) Result: a has the larger value Its value is: 4

IF BLOCK EXTENDED IF condition 1 Then action1 ElseIf condition 2 Then action 2 action 2 ElseIf condition 3 Then action 3 End If

IF BLOCK EXTENDED(Contd.) What the following program will do?

IF BLOCK EXTENDED(Contd.) Sub Command1_Click Dim a As Single, b As Single Let a=4 Let b=5 If (a>b) Then Picture1.Print “a has the larger value” ElseIf (a<b) Then Picture1.Print “b has the larger value” Else Picture1.Print “a and b have same value” End If End Sub

IF BLOCK EXTENDED (Contd.) Result: b has the larger value

Conditions

Conditions: Examples ► ExpressionTrue/False 2 < 5? -5 > -2.5? 1 < 1? 1 = 1? 3.5 <= 3.5? -9 >= -35? -2 <> -3?

Conditions: Examples (Contd.) ► ExpressionTrue/False 2 < 5True -5 > -2.5False 1 < 1False 1 = 1True 3.5 <= 3.5True -9 >= -35True -2 <> -3True

Conditions: Examples (Contd.) ► ExpressionTrue/False “cat” < “dog”? “cart” < “cat”? “cat” < “catalog”? “9W” < “bat”? “Dog” < “cat” ? “Sales-99” <= “Sales-retail”?

Conditions: Examples (Contd.) ► For strings use the ANSI (or ASCI) table CharactersANSI(ASCI) Value CharactersANSI(ASCI) Value Digits (0-9)48-57 Upper Case (A-Z)65-90 Lower Case (a-z) ► Compare two strings character by character

Conditions: Examples (Contd.) ► ExpressionTrue/False “cat” < “dog”True “cart” < “cat”True “cat” < “catalog”True “9W” < “bat”True “Dog” < “cat” True “Sales-99 <= “Sales-retail”True

Conditions: Examples (Contd.) ► Assume a= 4; b= 3; c=“hello”; d=“bye” ► ExpressionTrue/False (a + b) < 2*a? (Len(c) - b) = (a/2)? c < “good” & d?

Conditions: Examples (Contd.) ► Assume a= 4; b= 3; c=“hello”; d=“bye” ► ExpressionTrue/False (a + b) < 2*aTrue (Len(c) - b) = (a/2)True c < “good” & dFalse

Conditions (Contd.) ► Condition is an expression involving relational operators; it is either True or False ► Relational operators: =; <>; ; =

Conditions (Contd.) ► Complex Conditions: Conditions joined by Logical Operators  cond1 And cond2  cond1 Or cond2  Not cond1

Conditions: Examples (Contd.) ► Assume n= 4; answ=“Y” ► ExpressionTrue/False (2<n)And(n<6) ? Not(n<6)? (answ=“Y”) Or (answ=“y”)?

Conditions: Examples (Contd.) ► Assume n= 4; answ=“Y” ► ExpressionTrue/False (2<n)And(n<6) True Not(n<6)False (answ=“Y”) Or (answ=“y”)True

Conditions (Contd.) ► Operator hierarchy: In decreasing order of priority:  Arithemetic  Relational  Logical ► Logical operator hierarchy: In decreasing order of priority:  Not  And  Or

Conditions (Contd.) => - Tie: Left most operator is first - Use parantheses