Introduction to Computing: Functions, Conditions, and Decision Structures
This lecture series, led by Dr. Nadeem A. Khan, focuses on essential programming concepts such as functions, conditions, and decision structures. Participants will learn how to define subroutines and functions, understand variable scopes (local, module-level, global), and apply top-down design and structured programming techniques. The course includes practical examples of converting temperatures, calculating cider production, and executing decision blocks for comparing variables. By the end, learners will grasp the foundational programming skills needed for effective problem solving.
Introduction to Computing: Functions, Conditions, and Decision Structures
E N D
Presentation Transcript
Introduction to Computing Dr. Nadeem A Khan
Functions • General Format : Subroutine Sub SubprogrammeName (list of parameters) statements End Sub • General Format : Functions Sub FunctionName (list of parameters) As datatype statements Function Name = expression End Function
Functions • Examples: Function FtoC (t As Single) As Single FtoC = (5/9)*(t-32) End Function Function FirstName$ (nom As String) Dim firstSpace As Integer Rem Extract the first name from the full name nom Let firstSpace = Instr(nom, “ ”) FirstName$ = Left$(nom, firstSpace-1) End Function
Functions • Using function in a program Sub Command1_Click Dim x As Single Picture1.Print FtoC(212) x = FtoC(32) Picture1.Print x x = FtoC(81+32) Picture1.Print x End Sub
Functions (Contd.) • Result: 100 0 45
Functions (Contd.) • Practice Problem Function Cider (g As Single, x As Single) As Single Cider=g*x End Function Sub DisplayNumOfGallons(galPerBu, apples As Single) Picture1.Cls Picture1.Print “You can make”; Cider(galPerBu, apples); Picture1.Print “gallons of cider.” End Sub Sub GetData (gallonsPerBushel As Single, apples As Single) Let gallonsPerBushel =3 Let apples =9 End Sub
Functions (Contd.) • Practice Problem Sub Command1_Click Rem How many gallons of apple cider can we make Dim gallonsPerBushel As Single, apples as Single Call GetData(gallonPerBushel, apples) Call DisplayNumOfGallons(gallonsPerBushel, apples) End Sub
Functions (Contd.) • Practice Problem: Result You can make 27 gallons of cider
Private and Public • Variables, Subroutines and Functions can be declared as Private or Public as well • Read Section 2.4 (Scott Warner) Scope • Local • Module/Form-Level • Global
Modular Design • Top-Down Design • Structured Programming • Sequences • Decisions • Loops
Note: • Read: Schneider (Section 4.2,4.3, 4.4) • Read comments and do practice problems of these sections • Attempt questions of Exercises
Conditions: Examples • Expression True/False 2 < 5 ? -5 > -2.5 ? 1 < 1 ? 1 = 1 ? 3.5 <= 3.5 ? -9 >= -35 ? -2 <> -3 ?
Conditions: Examples (Contd.) • Expression True/False 2 < 5 True -5 > -2.5 False 1 < 1 False 1 = 1 True 3.5 <= 3.5 True -9 >= -35 True -2 <> -3 True
Conditions: Examples (Contd.) • Expression True/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 Characters ANSI(ASCI) Value Digits (0-9) 48-57 Upper Case (A-Z) 65-90 Lower Case (a-z) 97-122 • Compare two strings character by character
Conditions: Examples (Contd.) • Expression True/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” • Expression True/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” • Expression True/False (a + b) < 2*a True (Len(c) - b) = (a/2) True c < “good” & d False
Conditions (Contd.) • Condition is an expression involving relational operators; it is either True or False • Relational operators: =; <>;<;>;<=;>= => See Table 5.1
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” • Expression True/False (2<n)And(n<6) ? Not(n<6) ? (answ=“Y”) Or (answ=“y”) ?
Conditions: Examples (Contd.) • Assume n= 4; answ=“Y” • Expression True/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
Decision Structure:Flowchart Is Condition True Process Step (s) 2 Process Step (s) 1
IF BLOCKS IF condition Then action1 Else 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=a Else 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=a Else 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 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
Note: • Read: Schneider (Section 5.1, 5.2) • Read comments and do practice problems of these sections • Attempt questions of Exercises