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

Slides:



Advertisements
Similar presentations
Ch. 3 Variables VB.Net Programming. Data Types Boolean – True or False values Short – Small integers (+/- 32,767) Integer – Medium-size integers (+/-
Advertisements

Proyek Queri-queri Lanjut dan Sub-subqueri. Variables dan Data.
Introduction to Computing Dr. Nadeem A Khan. Lecture 22.
Introduction to Computing Dr. Nadeem A Khan. Lecture 8.
1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf 9.4Printing Integers 9.5Printing Floating-Point.
Introduction to Computing Dr. Nadeem A Khan. Lecture 10.
Built-In Functions.
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 4.
1 9/20/06CS150 Introduction to Computer Science 1 Review: Exam 1.
Introduction to Computing Dr. Nadeem A Khan. Lecture 24.
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.
Chapter 9 Formatted Input/Output Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Introduction to Computing Dr. Nadeem A Khan. Lecture 6.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction.
Data types and variables
Introduction to Computing Dr. Nadeem A Khan. Lecture 13.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf.
Chapter 2 Data Types, Declarations, and Displays
Introduction to Computing Dr. Nadeem A Khan. Lecture 9.
On to… string operations & functions. Concatenation (&) §When we want to combine two character strings into one new (longer) string, we can concatenate.
Representation and Conversion of Numeric Types 4 We have seen multiple data types that C provides for numbers: int and double 4 What differences are there.
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.
Objectives You should be able to describe: Data Types
Dani Vainstein1 VBScript Session 9. Dani Vainstein2 What we learn last session? VBScript coding conventions. Code convention usage for constants, variables,
Variables and Constants
 2000 Prentice Hall, Inc. All rights reserved. Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Streams Streams –Sequences of characters organized.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction.
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.
Chapter 9 Formatted Input/Output Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
 Pearson Education, Inc. All rights reserved Formatted Output.
EPSII 59:006 Spring Introduction In this lecture  Formatted Input/Output scanf and printf  Streams (input and output) gets, puts, getchar, putchar.
 2005 Pearson Education, Inc. All rights reserved Formatted Output.
Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf 9.4Printing Integers 9.5Printing Floating-Point.
Input & Output: Console
National Diploma Unit 4 Introduction to Software Development Data types, variables and constants.
CSC 162 Visual Basic I Programming. Randomizing and Formatting Randomizing Formatting –User-Defined Formats –Named Numeric Formats.
Lecture 8 Visual Basic (2).
Chapter 2: Using Data.
Lecture #5 Introduction to C++
Variables & Function Calls. Overview u Variables  Programmer Defined & Intrinsic  Data Types  Calculation issues u Using Functions  The val() function.
Chapter 51 Decisions Relational and Logical Operators If Blocks Select Case Blocks.
COMP Primitive and Class Types Yi Hong May 14, 2015.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 2A Reading, Processing and Displaying Data (Concepts)
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.
CECS 5020 Computers in Education Visual Basic Variables and Constants.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
Numbers1 Working with Numbers There are times that we have to work with numerical values. When we count things, we need Integers or whole numbers. When.
Introduction to Computing Dr. Nadeem A Khan. Lecture 9.
Visual Basic Review LBS 126. VB programming Project Form 1Form 2Form 3 Text boxButton Picture box Objects Text box Button Objects.
12 February 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
Dr. Sajib Datta Jan 21,  Declare a variable ◦ int height; [note that no value is still assigned]  Assign a variable a value ◦ height =
1 VB-06-String Manipulation Mar 03, 2002 String Function VISUAL BASIC.
Chapter 4.  Variables – named memory location that stores a value.  Variables allows the use of meaningful names which makes the code easier to read.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
28 Formatted Output.
A variable is a name for a value stored in memory.
Numbers.
Sub Procedures and Functions
VBScript Session 10.
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
Presentation transcript:

Introduction to Computing Dr. Nadeem A Khan

Lecture 7

Lab on Thursday!

► Already known:  String  Single  Integer Data Types

► Strings Storage: 10 bytes + string length Range: 0 to app. 2 billions chars. Declaration: Dim strVarLen As String (declares a string of a variable length) (declares a string of a variable length) Data Types (Contd.)

► Fixed-Length Strings Storage: Length of string Range: 1 to app. 65, 400 chars. Declaration: Dim strFixLen As String * 2 (declares a string of a fix size of 2 chars.) Data Types (Contd.)

► Fixed-Length Strings Usage:Example Dim strText As String * 5 Let strText = “Hello” Picture1.Print strText Let strText = “H” Picture1.Print strText Let strText = “HelloWorld” Picture1.Print strText Data Types

► Fixed-Length Strings Usage:Result: Hello(Complete string) H…. (H followed 4 spaces) Hello(First 5 characters only) => The length is fix Data Types

► Integers Storage: 2 bytes Range: -32,768 to 32,767 Declaration: Dim intExample As Integer (declares intExample as an Integer variable) (declares intExample as an Integer variable) Data Types (Contd.)

► Integers Usage:Example Dim count As Integer Let count= 6 Picture1.Print count Let count= count+1 Picture1.Print count Let count= 6/5 Picture1.Print count Let count= * 2 Picture1.Print count Data Types

► Integer Usage:Result67 1 (rounding to lower value) 5(rounding to higher value) => takes only whole number values => takes only whole number values Data Types

► Long (Integer) Storage: 4 bytes Range: -2,147,483,648 to 2,147,483,647 Declaration: Dim lngExample As Long (declares lntExample as a long variable) (declares lntExample as a long variable) Data Types (Contd.)

► Long (Integer) Usage: Same as Integer Type except the range is much larger much larger Data Types (Contd.)

► Byte Storage: 1 byte Range: 0 to 255 Declaration: Dim bytExample As Byte (declares bytExample as a Byte type variable) (declares bytExample as a Byte type variable) Data Types (Contd.)

► Byte Usage: Same as Integer Type except the range is positive and much smaller positive and much smaller Data Types (Contd.)

► Boolean Storage: 2 bytes Range: TRUE(1) or FALSE(0) Declaration: Dim blnState As Boolean (declares a Boolean type variable blnState) Data Types (Contd.)

► Boolean Usage:Example Dim blnExample As Boolean Let blnExample= FALSE Picture1.Print blnExample Let blnExample= 1 Picture1.Print blnExample Let blnExample= 6 Picture1.Print blnExample Let blnExample= -8*7+5.2 Let blnExample= -8*7+5.2 Picture1.Print blnExample Data Types (Contd.)

► Boolean Usage:Example FALSE FALSE TRUE TRUE =>Values other than 0 are TRUE =>Values other than 0 are TRUE Data Types (Contd.)

► Single (Precision Floating-Point) Storage: 4 bytes Range: -3.4…E38 to -1.4…E-45 (negative) 1.4…E-45 to 3.4…E38 (positive) Declaration: Dim sngAverage As Single (declares a Single type variable sngAverage) Data Types (Contd.)

► Double (Precision Floating-Point) Storage: 8 bytes Range: -1.7…E308 to -4.9…E-324 (negative) 4.9…E-324 to 1.7…E308 (positive) 4.9…E-324 to 1.7…E308 (positive)Declaration: Dim dblAverage As Double (declares a Double type variable dblAverage) Data Types (Contd.)

► Double Usage:Example Dim sngValue As Single, dblValue As Double Let sngValue= 1/3 Picture1.Print sngValue Let dblValue= 1/3 Picture1.Print dblValue Data Types (Contd.)

► Double Usage:Result (Single precision) (Double precision) => Value of 1/3 represented more accurately by double than by single by double than by single Data Types (Contd.)

► Double Usage:Example Dim sngValue As Single, dblValue As Double Let sngValue= 1/3 Let sngValue= sngValue * Picture1.Print sngValue Let dblValue= 1/3 Let dblValue= dblValue * Picture1.Print dblValue Data Types (Contd.)

► Double Usage:Result (Single precision; rounding error) (Double precision) => - The decimal point is floating; - Eventually both will be subjected to rounding errors value increases to large values value increases to large values - Still Double will remain more precise than Single Data Types (Contd.)

► Currency Storage: 8 bytes Range: -922,337,203,685, to 922,337,203,685, ,337,203,685, Declaration: Dim curRevenue As Currency (declares a Currency type variable curRevenue) Data Types (Contd.)

► Currency Usage:Example Dim curValue As Currency Let curValue= 1/3 Picture1.Print curValue Let curValue= 100*1/3 Picture1.Print curValue Data Types (Contd.)

► Currency Usage:Result =>- The decimal point is NOT floating; - Could be used for currency and scientific research - No rounding problems for high values Data Types (Contd.)

► Even more data types  Date Variable: for date and time  Object Variable  Variant Variable  ………. Read the book for more info. Data Types (Contd.)

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 ► Take one or more input values ► Return an output value ► Already seen:  Val(..), Str$(..), Asc(..), Chr(..)

Built-in Numeric Functions ► Sqr: calculates square-root and returns the value as double data type e.g: e.g:  Sqr(9) is 3  Sqr(0) is 0  Sqr(2) is  Sqr(30*2+4) is 8

Built-in Numeric Functions ► Int: Greatest integer less than and equal to a number e.g: e.g:  Int(2.7) is 2  Int(3) is 3  Int(-2.7) is -3

Built-in String Functions ► Left$, Mid$, Right$, UCase$, Trim$ Left$(“fanatic”, 3) is “fan”Right$(“fanatic”, 3) is “tic” Left$(“12/15/93”,2) is “12”Right$(“12/15/93”, 2) is “93” Mid$(“fanatic”, 5,1) is “t”Mid$(“12/15/93”,4, 2) is “15” Ucase(“Disk”) is “DISK”Ucase(“12two”) is “12TWO” Trim$(“ 1 2 ”) is “1 2”Trim$(“-12 ”) is “-12”

Built-in Functions (Contd.) ► Len Len(“Shenandoah”) is ? Len(“Just a moment”) is ? Len(“m”) is ?

Built-in Functions (Contd.) ► Len Len(“Shenandoah”) is 10 Len(“Just a moment”) is 13 Len(“m”) is 1

Built-in Functions (Contd.) ► Instr Instr(“Shenandoah”, “nand”) is ? Instr(“Just a moment”, “ ”) is ? Instr(“Croissant”, “ist”) is ?

Built-in Functions (Contd.) ► Instr Instr(“Shenandoah”, “nand”) is 4 Instr(“Just a moment”, “ ”) is 5 Instr(“Croissant”, “ist”) is 0

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,

Built-in Functions (Contd.) Abs(10) is 10 Abs(-10) is 10 Sgn(10) is 1 Sgn(0) is 0 Sgn(-10) is -1

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.)

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.)

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

Please read Chapter 4 completely