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

Slides:



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

1 PHP Statement Constructs Server Scripting. 5-2 Basic Statement All Statements end in a semicolon. Statements are delimited from the HTML code by enclosing.
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 10.
Introduction to Computing Dr. Nadeem A Khan. Lecture 23.
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 27.
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 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.
Chapter 5 - VB.Net by Schneider
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 11.
Introduction to Computing Dr. Nadeem A Khan. Lecture 6.
Introduction to Computing Dr. Nadeem A Khan. Lecture 28.
Visual Basic Statements
Chapter 4 - VB.Net by Schneider1 Chapter 4 General Procedures 4.1 Sub Procedures, Part I 4.2 Sub Procedures, Part II 4.3 Function Procedures 4.4 Modular.
Selection Structures Relational and Logical Operators; If Statements.
Chapter 5 - Visual Basic Schneider1 Chapter 5 Decisions.
Introduction to Computing Dr. Nadeem A Khan. Lecture 18.
Chapter 4 - Visual Basic Schneider1 Chapter 4 General Procedures.
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.
CS0004: Introduction to Programming Relational Operators, Logical Operators, and If Statements.
Review for Exam 2 School of Business Eastern Illinois University © Abdou Illia, Spring 2003 (Week 10, Friday 3/21/2003) - IF Blocks - Do Loops - Select.
Language Elements 1. Data Types 2 Floating Point (real) Single Precision Double Precision Decimal Fixed Point (integer) Byte Short Integer Long Numerical.
Chapter 5 – Decisions 5.1 Relational and Logical Operators 5.2 If Blocks 5.3 Select Case Blocks.
Chapter 4 - VB 2008 by Schneider1 Chapter 4 – Decisions 4.1 Relational and Logical Operators 4.2 If Blocks 4.3 Select Case Blocks.
Chapter 5 Decisions. Outline and Objectives Relational and Logical Operators If Blocks Select Case Blocks.
Chapter 5 - VB 2005 by Schneider1 Chapter 5 – Decisions 5.1 Relational and Logical Operators 5.2 If Blocks.
CSC115 Introduction to Computer Programming Zhen Jiang Dept. of Computer Science West Chester University West Chester, PA 19383
Chapter 51 Decisions Relational and Logical Operators If Blocks Select Case Blocks.
Review for Final (Part 2) School of Business Eastern Illinois University © Abdou Illia, Spring 2003 (Week 15, Friday 5/2/2003)
CST336, Dr. Krzysztof Pietroszek Week 2: PHP. 1.Introduction to PHP 2.Embed PHP code into an HTML web page 3.Generate (output HTML) web page using PHP.
Introduction to Computing Dr. Nadeem A Khan. Lecture 7.
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.
Loop and repetition. Today Passing values to and back from Sub procedures Option Buttons Do While Loops For Loops Population Growth.
Chapter 4 - Visual Basic Schneider1 Chapter 4 General Procedures.
Fourth Quarter.  Involves loops or cycles ◦ Loops: means that a process may be repeated as long as certain condition remains true or remains false. ◦
Department of Electronic & Electrical Engineering Lecture 3 IO reading and writing variables scanf printf format strings "%d %c %f" Expressions operators.
1 VB-04-Control Structures 16 March 2016 Visual Basic Control Structures - Selection.
Control Structure  What is control Structure?  Types of Controls  Use the control structure in VBScript.  Example Summery.
Chapter 4 - VB 2008 by Schneider1 Chapter 4 – Decisions 4.1 Relational and Logical Operators 4.2 If Blocks 4.3 Select Case Blocks.
Week 4 Relational and Logical Operators Dr. Jennifer Cunningham.
Introduction to Computing Dr. Nadeem A Khan. Lecture 24.
Introduction to Computing Dr. Nadeem A Khan. Lecture 21.
VBA - Excel VBA is Visual Basic for Applications
COMPUTATIONAL CONSTRUCTS
The Selection Structure
CSC115 Introduction to Computer Programming
Chapter 4 - Visual Basic Schneider
VB Decisions, Conditions & If
Chapter 5 - Visual Basic Schneider
VB Decisions & Conditions
Conditional Logic Presentation Name Course Name
Language Constructs Construct means to build or put together. Language constructs refers to those parts which make up a high level programming language.
Chapter 5 Decisions.
Presentation transcript:

Introduction to Computing Dr. Nadeem A Khan

Lecture 19

Lecture 20 in Lab !

Functions

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:

► 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 of these sections ► Attempt questions of Exercises

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: =; <>; ; = => 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” ► 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

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

Note: ► Read: Schneider (Section 5.1, 5.2) ► Read comments and do practice problems of these sections of these sections ► Attempt questions of Exercises