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

## Presentation on theme: "Introduction to Computing Dr. Nadeem A Khan. Lecture 4."— Presentation transcript:

Introduction to Computing Dr. Nadeem A Khan

Lecture 4

On the web site http://suraj.lums.edu.pk/~cs101a04/ Assignment 1 has been posted.

Expressions

Numeric Expressions ► Result?  2+10/5  5+2* 2- 1/10  4*6-3^2+5/10  (3^2)*5

String Expressions ► Result?  “Hello” + “World”  “Hello “+ “ World”  “Hello” & “CS101 Section 6”

String Expressions (Contd.) ► Strings can be concatenated by using + or &

What will be printed? ► Picture1.Print 2*3+2 ► Picture1.Print “hello”+ “world” ► Picture1.Print “2*3+2”

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

Variables (Contd.) Conclusions: ► A variable holds a value ► The value can be assigned and then used

Variables Types ► E.g: ► String variables ► Integer variables ► Single variables

Declaring Variable Types ► Dim var As String ► Dim var As Integer ► Dim var As Single

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

String Variables (Contd.) ► Displayed Result hellostudents of CS101 hellostudents of CS101 on Sept. 19,2002 hello world

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

► Displayed Result: The balance after a year is 106.55 Numeric Variable (Contd.)

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

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)

Arithmetic Operators  Addition (2+3)  Subtraction (2-3)  Multiplication (2*3)  Division (2/3)  Exponentiation (2^3)

► Result?  2+10/5  5+2* 2- 1/10  4*6-3^2+5/10  (3^2)*5 Arithmetic Operators (Contd.)

► Operator Precedence 1. ^ 2.- operator (indicating a negative value) 3. * and / operator 4. + and - operator Arithmetic Operators (Contd.)

► Use parenthesis ( ) to keep intentions clear Arithmetic Operators (Contd.)

Completely read Chapter 2 and Chapter 3

Chapter 3

Program Planning 1. Analyze 2. Design 3. Choose the Interface 4. Code 5. Test and Debug 6. Complete the Documentation

The Problem-Solving Process Input Processing Output

Program Design Tools ► Pseudo-code ► Flow Charts ► Hierarchy Charts

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

Pseudocode ► Abbreviated version of actual computer code in English-like statements

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

Flowcharts ► Special geometric symbols connected by arrows

Postage stamp problem Start Read sheets Set stamps = sheets/ 5 Display stamps Round stamps up to next whole number End input processing output processing

Elements of Flowcharts Symbol Name Flowline Terminal Input/Output Processing Decision

Continued… Symbol Name Connector Off page Connector Predefined process Annotation

Hierarchy chart ► Shows overall program structure

Hierarchy Charts: Postage Stamp Problem Postage Stamp Problem Read Sheets Calculate stamps Display stamps Set stamps = sheets/5 Round stamps to next whole number

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

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.

Decision Structure: Pseudocode IF condition is TRUE THEN Process step(s) 1 ELSE Process step(s) 2 Process step(s) 2 END IF

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

Decision Structure: Pseudocode What in case of multiple conditions?

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

Pseudo-Code: Street Direction Problem Get Street IF Street is EVEN Display Eastbound ELSE Display Westbound END IF

Flow Chart: Street Direction Problem End Start Get Street Is street even? Display Westbound Display Eastbound

Hierarchy Chart: Street Direction Problem Street Direction Program Get Street Number Decide whether street number is odd or even Display direction

Still Another Problem ► Calculate and report the grade-point average of a class.

The Loop Structure ► A programming structure that executes instructions many times.

Flow chart: loop structure No Process Step(s) Is condition true ? Yes

Pseudo code: loop structure ► DO WHILE condition is TRUE Process Step(s) LOOP

Draw the Flowchart for the class average problem

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

Hierarchy Chart: Class Average Problem Class average program Get grade Compute sum Display average Calculate average