Presentation is loading. Please wait.

Presentation is loading. Please wait.

Basic Elements of Programming A VB program is built from statements, statements from expressions, expressions from operators and operands, and operands.

Similar presentations


Presentation on theme: "Basic Elements of Programming A VB program is built from statements, statements from expressions, expressions from operators and operands, and operands."— Presentation transcript:

1 Basic Elements of Programming A VB program is built from statements, statements from expressions, expressions from operators and operands, and operands from variables/constants and the properties of objects Operand OperatorExpressionStatement

2 Programming Constructs VB code is generally comprised of combinations of the following program statements Sequence: Consisting of a number of instructions which are processed in sequence Selection: Consisting of branches in the VB program, containing different instructions which are processed depending on the results of certain tested conditions Iteration: Consisting of groups of statements which are repeatedly executed until a certain tested condition is satisfied

3 Selection (Branching Constructs in VB) Branching constructs are used to control program flow A Condition/Expression is evaluated, and the result determines which statements the program executes There are 2 main types of Branching Construct in VB: IF statements SELECT CASE statements

4 Program Statements Evaluate an Expression Outcome AOutcome B

5 If Statements Projects can take one action or another, based on a condition Make a decision and take alternate courses of action based on the outcome If..Then statement syntax: If [Condition/Expression] Then Action/Statements Else Action/Statements End If

6 The word Then must appear on the same line as If Else and End If must appear on separate lines The statements underneath the Then and Else clause are indented for ‘readability’ and ‘clarity’ (always indent code, especially with If statements, the indentation helps to visualise the intended logic and saves on project debugging time) If..Then statements are generally used in conjunction with the Relational (Comparison) Operators These Relational (Comparison) Operators are used to compare expressions, and return a result of either True or False (Boolean Data Type)

7 The test of an If statement is based on a condition To form conditions, Relational(Comparison) Operators are used, resulting in an outcome being either ‘True’ or ‘False’ (Boolean Data Type) There are 6 Relational(Comparison) Operators in VB: Order of Precedence of Relational Operators > greater than not equal to >= greater than or equal to <= less than or equal to

8 Conditions can be formed with - numeric variables and constants - string variables and constants - object properties, and - arithmetic expressions However, comparisons have to be made on like data types strings compared to strings numeric values compared with numeric values whether a variable, constant, property of an object, or arithmetic expression

9 Dim Num1 as Integer If Num1 > 10 Then Print “Num1 is greater than 10” End If If Num1 > 10 Then Print “Num1 is greater than 10” Else Print “Num1 is less than 10” End If Val(Text1.Text)

10 Multiple Branching The logic of a program may require that there be more than one branch in the program In this case the ElseIf keyword(clause) is added to the If..Then statement to increase flexibility An infinite amount of ElseIf statements can be included into the If..Then statement

11 If..Then..Else statement syntax: If [Condition/Expression] Then Action/Statements ElseIf [Condition/Expression] Then Action/Statements Else Action/Statements End If

12 Dim Temperature As Single If Temperature = 30 Then Print “Hot” Else Print “Moderate” End If

13 If..Then..Else statements have a definite hierarchy The order of an If..Then..Else statement is important, due to the fact that if the first line of the If..Then..Else statement is ‘True’, then none of the other ElseIf statements will be processed/executed If..Then statements can be given greater flexibility in 2 main ways: [1] Using Logical Operators [2] Nesting If..Then statements

14 Logical Operators Logical Operators should be used when a limited number of conditions are to be tested The 3 most commonly used Logical Operators in VB are in ‘Order of Precedence’: AND OR NOT Compound conditions/expressions are created using Logical Operators Use compound conditions/expressions to test more than one condition

15 AND Both conditions must be true for the entire condition to be true OR If one condition or both conditions are true, the entire condition is true NOT Reverses the condition, so that a true condition will evaluate false and vice versa The use of parentheses can change the ‘Order of Precedence’ of these Logical Operators Always plan the use of Logical Operators, as their use can often involve confusing logic


Download ppt "Basic Elements of Programming A VB program is built from statements, statements from expressions, expressions from operators and operands, and operands."

Similar presentations


Ads by Google