• Visual Basic includes a number of features that allow us to select among
alternative pathways, or to repeat the execution of a particular block of statements. • Example: we can choose to execute one of several different blocks of statements, depending on the value of an expression. This process is known as selection. Or, we can choose one of two different paths, depending on the outcome of a logical test (i.e., depending whether a logical expression is true or false). This process is known as branching. • Many programs require that a group of instructions be executed repeatedly, until some particular condition has been satisfied. This process is known as looping. • Sometimes the number of passes through the loop will be known in advance (unconditional looping). RELATIONAL OPERATORS AND LOGICAL EXPRESSIONS • In order to carry out branching operations in Visual Basic, we must be able to express conditions of equality and inequality. To do so, we make use of the following relational operators (also called comparison operators): • Equal: = • Not equal: <> • Less than: < • Less than or equal to: <= • Greater than: > • Greater than or equal to: >= • These operators are used to compare numeric quantities (i.e., constants, numeric variables or numeric expressions) or strings, thus forming logical expressions that are either true or false. The operands within a logical expression must be of the same type; i.e., both must be numeric or both must be strings. • Several logical expressions involving strings are presented below. All variables represent strings. Each logical expression will be either true or false, depending on the particular strings that are assigned to the string variables. Student = "Smith" char <> "w" Target < City • The first expression will be true if the string assigned to Student is "Smith"; otherwise, the expression will be false. Similarly, the second expression will be true if the string assigned to char is not "w", and the last expression will be true if the string assigned to Target comes earlier in the alphabet than the string assigned to City. Thus, if Target represents "Philadelphia" and City represents "Pittsburgh", the expression will be true. LOGICAL OPERATORS • In addition to the relational operators, Visual Basic contains several logical operators. They are And, Or, Xor (exclusive Or), Not, Eqv (equivalent) and Imp (implies). The first three operators (And, Or and Xor) are used to combine logical expressions, thus forming more complex logical expressions. • And will result in a condition that is true if both expressions are true. • Or will result in a condition that is true if either expression is true, or if they are both true • Xor, however, will result in a condition that is true only if one of the expressions is true and the other is false. • Not is used to reverse (negate) the value of a logical expression (e.g., from true to false, or false to true). • Eqv will result in a condition that is true if both expressions have the same logical value (either both true or both false); • Imp will always result in a true condition unless the first expression is true and the second is false. • EXAMPLE 3.3 • Shown below are several logical expressions that make use of logical operators. X = 27 And Student = "Smith" X > 0 And Student <= "Smith" C < Sqr(A + B) Or FLAG <> CUTOFF C < Sqr(A + B) Xor FLAG <> CUTOFF Not(Student = "Smith") And (Account = "CURRENT") (Student = "Smith") Eqv (Account = "CURRENT") (Student = "Smith") Imp (Account = "CURRENT") The complete hierarchy of arithmetic, relational and logical operators is as follows: Operation Operator 1. Exponentiation ^ 2. Negation (i.e., preceding a numeric − quantity with a minus sign) 3. Multiplication and division */ 4. Integer division \ 5. Integer remainder Mod 6. Addition and subtraction +− 7. Relational = <> < <= > >= 8. Logical Not Not 9. Logical And And 10. Logical Or Or 11. Logical Xor Xor 12. Logical Eqv Eqv 13. Logical Imp Imp