Download presentation
Presentation is loading. Please wait.
1
Introduction to Computing Dr. Nadeem A Khan
2
Lecture 4
3
On the web site http://suraj.lums.edu.pk/~cs101a04/ Assignment 1 has been posted.
4
Expressions
5
Numeric Expressions ► Result? 2+10/5 5+2* 2- 1/10 4*6-3^2+5/10 (3^2)*5
6
String Expressions ► Result? “Hello” + “World” “Hello “+ “ World” “Hello” & “CS101 Section 6”
7
String Expressions (Contd.) ► Strings can be concatenated by using + or &
8
What will be printed? ► Picture1.Print 2*3+2 ► Picture1.Print “hello”+ “world” ► Picture1.Print “2*3+2”
9
Variables ► What will this code do? Let num1 = 6 Picture1.Print num1 Picture1.Print num1 Let num2 = 6+2 Picture1.Print num2 Picture1.Print num2
10
Variables (Contd.) Conclusions: ► A variable holds a value ► The value can be assigned and then used
11
Variables Types ► E.g: ► String variables ► Integer variables ► Single variables
12
Declaring Variable Types ► Dim var As String ► Dim var As Integer ► Dim var As Single
13
String Variables (Contd.) Sub Command1_Click () Dim message_part1 As String, message_part2 As String, Dim message As String Picture1.Cls Let message_part1 = “hello” Let message_part2 = “students of CS101” Let message= message_part1+message_part2 Picture1.Print message Picture1.Print message + “ on Sept. 19,2002” Let message= message_part1 & “ world” Picture1.Print message End Sub
14
String Variables (Contd.) ► Displayed Result hellostudents of CS101 hellostudents of CS101 on Sept. 19,2002 hello world
15
Numeric Variable ► Example program: Sub Command1_Click () Dim interestRate As Single Dim principal As Single Dim phrase as String Picture1.Cls Let InterestRate=.0655 Let principal =100 Let phrase =“The balance after a year is” Picture1.Print phrase;(1+interestRate)*principal End Sub
16
► Displayed Result: The balance after a year is 106.55 Numeric Variable (Contd.)
17
Input/Output Using Text Box ► Contents of a text box is always a string ► Output the string by Let Text1.Text= strvar ► Input the string by Let strVar =Text1.Text
18
Input/Output Using Text Box (Contd.) ► Numbers in a textbox are also stored as strings ► Input a number by Let numVar = Val(Text1.Text) ► Output a number by Let Text1.Text=Str(numVar)
19
Arithmetic Operators Addition (2+3) Subtraction (2-3) Multiplication (2*3) Division (2/3) Exponentiation (2^3)
20
► Result? 2+10/5 5+2* 2- 1/10 4*6-3^2+5/10 (3^2)*5 Arithmetic Operators (Contd.)
21
► Operator Precedence 1. ^ 2.- operator (indicating a negative value) 3. * and / operator 4. + and - operator Arithmetic Operators (Contd.)
22
► Use parenthesis ( ) to keep intentions clear Arithmetic Operators (Contd.)
23
Completely read Chapter 2 and Chapter 3
24
Chapter 3
25
Program Planning 1. Analyze 2. Design 3. Choose the Interface 4. Code 5. Test and Debug 6. Complete the Documentation
26
The Problem-Solving Process Input Processing Output
27
Program Design Tools ► Pseudo-code ► Flow Charts ► Hierarchy Charts
28
Postage stamp problem: How many stamps to put on a envelop given one stamp is needed for every five sheets of paper or a fraction thereof How many stamps to put on a envelop given one stamp is needed for every five sheets of paper or a fraction thereof
29
Pseudocode ► Abbreviated version of actual computer code in English-like statements
30
Pseudo code: Postage Stamp Problem ► Program – Determine the number of stamps for a letter Read Sheets Set the number of stamps to sheets / 5 Round the number of stamps Display the number of stamps
31
Flowcharts ► Special geometric symbols connected by arrows
32
Postage stamp problem Start Read sheets Set stamps = sheets/ 5 Display stamps Round stamps up to next whole number End input processing output processing
33
Elements of Flowcharts Symbol Name Flowline Terminal Input/Output Processing Decision
34
Continued… Symbol Name Connector Off page Connector Predefined process Annotation
35
Hierarchy chart ► Shows overall program structure
36
Hierarchy Charts: Postage Stamp Problem Postage Stamp Problem Read Sheets Calculate stamps Display stamps Set stamps = sheets/5 Round stamps to next whole number
37
Another problem: Given a street number of one-way street in New York, decide the direction of the street, either eastbound or westbound Given a street number of one-way street in New York, decide the direction of the street, either eastbound or westboundNote: Even numbered street: Eastbound Odd numbered street: Westbound
38
Decisions ► Sequence Structure – a sequence followed without skipping any line. ► Decision Structure – a structure which requires a decision for any lines of code to be executed.
39
Decision Structure: Pseudocode IF condition is TRUE THEN Process step(s) 1 ELSE Process step(s) 2 Process step(s) 2 END IF
40
Decision Structure: Flowchart Process Step (s) 2 Is Condition True Process Step (s) 1
41
Decision Structure: Pseudocode What in case of multiple conditions?
42
Decision Structure: Pseudocode IF condition 1 is TRUE THEN Process step(s) 1 ELSE IF condition 2 is TRUE THEN Process step(s) 2 Process step(s) 2ELSE Process step(s) 3 END IF
43
Pseudo-Code: Street Direction Problem Get Street IF Street is EVEN Display Eastbound ELSE Display Westbound END IF
44
Flow Chart: Street Direction Problem End Start Get Street Is street even? Display Westbound Display Eastbound
45
Hierarchy Chart: Street Direction Problem Street Direction Program Get Street Number Decide whether street number is odd or even Display direction
46
Still Another Problem ► Calculate and report the grade-point average of a class.
47
The Loop Structure ► A programming structure that executes instructions many times.
48
Flow chart: loop structure No Process Step(s) Is condition true ? Yes
49
Pseudo code: loop structure ► DO WHILE condition is TRUE Process Step(s) LOOP
50
Draw the Flowchart for the class average problem
51
Pseudo code: Class Average Problem INITIALIZE Counter and Sum to 0 DO WHILE there are more data Get the next Grade Add the Grade to the Sum Increment the Counter LOOP Compute Average = Sum/Counter Display Average
52
Hierarchy Chart: Class Average Problem Class average program Get grade Compute sum Display average Calculate average
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.