1 / 12

IF Statement & Logical Operators

IF Statement & Logical Operators. Skill Area 306.2. Materials Prepared by Dhimas Ruswanto , BMm. Lecture Overview. Expressions Control Structures If Statement If..else statement Advanced If statement Logical Operators. Expressions.

evan
Download Presentation

IF Statement & Logical Operators

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. IF Statement & Logical Operators Skill Area 306.2 Materials Prepared by DhimasRuswanto, BMm

  2. Lecture Overview • Expressions • Control Structures • If Statement • If..else statement • Advanced If statement • Logical Operators

  3. Expressions • Anything that evaluates to a value is an expression An expression is said to return a value. • Thus, 3+2; returns the value 5 and so is an expression. • All expressions are statements. x = a + b; • Not only adds a and b and assigns the result to x, but returns the value of that assignment (the value of x) as well. • Thus, this statement is also an expression. • Because it is an expression, it can be on the right side of an assignment operator:

  4. Expressions x = a + b • This line is evaluated in the following order: • Add a to b. • Assign the result of the expression a + b to x. • If a, b, and x are all integers, and if a has the value 2 and b has the value 5, x will be assigned the value 7.

  5. Control Structures • A program is usually not limited to a linear sequence of instructions. During its process it may sequence, repeat code or take decisions. For that purpose, VB provides control structures that serve to specify what has to be done by our program, when and under which circumstances. • If and Else • Case Statements • Do While loop • Repeat Until loop • For loop

  6. IF statement • Normally, your program flows along line by line in the order in which it appears in your source code. • The if statement enables you to test for a condition (such as whether two variables are equal) and branch to different parts of your code, depending on the result. If(expression) statement • The expression in the parentheses can be any expression at all, but it usually contains one of the relational expressions. • If the expressionhasthe value 0, it is considered false, and the statement is skipped. If it has any nonzero value, it is considered true, and the statement is executed

  7. IF statement DimfirstNumber, secondNumberAsInteger firstNumber = 10 secondNumber = 7 If (firstNumber > secondNumber) Then MsgBox("First is greater than Second") EndIf

  8. IF…then…else statement • Often your program will want to take one branch if your condition is true, another if it is false. • The keyword else can make for far more readable code: If(expression) statement Else statement

  9. IF…then…else statement DimfirstNumber, secondNumberAsInteger firstNumber = 10 secondNumber = 7 If (firstNumber > secondNumber) Then MsgBox("First is greater than Second") Else MsgBox("Second is greater than First") EndIf

  10. Advanced IF statement  If(expression) Then If(expression) Then statement Else statement EndIf Else If(expression) Then statement Else statement EndIf EndIf

  11. Logical Operators •  To ask more than one relational question at a time • Logical Operators: • = is equal to • >= is greater than or equal to • <= is less than or equal to • <> is not equal to • Logical Operations: • AND, OR, NOT, NAND, NOR, XOR

  12. --- continue to next slide ---

More Related