1 / 25

Variables, Arithmetical & Relational Operators

Variables, Arithmetical & Relational Operators. Skill Area 306.1. Materials Prepared by Dhimas Ruswanto , BMm. Lecture Overview. Data Types Variables Constant Strings Arithmetical Operators Relational Operators Comments. VARIABLE. Data Types.

holden
Download Presentation

Variables, Arithmetical & Relational 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. Variables, Arithmetical & Relational Operators Skill Area 306.1 Materials Prepared by DhimasRuswanto, BMm

  2. Lecture Overview • Data Types • Variables • Constant • Strings • Arithmetical Operators • Relational Operators • Comments

  3. VARIABLE

  4. Data Types • Integer - This data type is used to represent integer. • Float - This data type is used to represent floating point number. • Double - This data type is used to represent double precision floating point number. • Char - This data type is used to represent a single character. • Boolean - This data type is used to represent boolean value. It can take one of two values: True or False. • String  is not a basic data type but may be implemented as an array of characters.

  5. Variables • A variable is the content of a memory location that stores a certain value. • A variable is identified or denoted by a variable name. • The syntax for declaring variable name: • Dimvariable_nameAsdata_type • DimfirstNumberAs Integer identifier type Dimension

  6. Rules for defining Variable Name • must begin with an alphabetic character or underscore • E.g.: _num • may contain only letters, digits and underscore (no special characters) • E.g.: myNum1 • case sensitive • E.g.: NUM1 differs from num1 • can not use keywords as identifiers • E.g.: Dim, If, While, • use meaningful variable names

  7. Declaring Variable Dim a As Integer • This declares a variable name a of type integer. • If there exists more than one variable of the same type, such variables can be represented by separating variable names using comma. • For instance: • Dim x, y, z As Integer • This declares 3 variables x, y and z all of data type integer.

  8. Example: • IF – THEN – ELSE • DO – WHILE - LOOP  Keyword • A symbol in programming language that has a special meaning for the compiler or interpreter. Example: • Dim, “”, := Key Terms Reserved Words • Words that has specific roles in the context in which it occurs, which cannot be used for other purposes.

  9. CONSTANTS

  10. Constants • Like variables, constants are data storage locations. • Unlike variables, and as the name implies, constants don't change. • You must initialize a constant when you create it, and you cannot assign a new value later. • Two types of constants: literal and symbolic.

  11. Literal Constants • A literal constant is a value typed directly into your program wherever it is needed. • For example • Dim myAgeAs Integer = 39 • myAge is a variable of type integer  • 39 is a literal constant. • You can't assign a value to 39, and its value can't be changed.

  12. Literal Constants • If your program has one integer variable named students and another named classes, you could compute how many students you have, given a known number of classes, if you knew there were 15 students per class: • students = classes * 15; • 15 is a literal constant

  13. Symbolic Constants • A symbolic constant is a constant that is represented by a name, just as a variable is represented. • Your code would be easier to read, and easier to maintain, if you substituted a symbolic constant for this value: students = classes * studentsPerClass • If you later decided to change the number of students in each class, you could do so where you define the constant studentsPerClass without having to make a change every place you used that value.

  14. STRINGS

  15. String • A sequence of characters is often referred to as a character “string”. • Example: 'Declaring variables DimfirstName, lastName, fullNameAsString 'Assign the value for each variables firstName = "James" lastName = "Bond" 'combine firstName with lastName fullName = firstName & lastName 'display the output MsgBox(fullName)

  16. String • A string value must inside the double-quote (“ “). DimfirstNameAs String = “James” • To combine two Strings, we have to use ampersand symbol (&). fullName = firstName & lastName

  17. Arithmetical and relational operators

  18. Mathematical Operators • There are five mathematical operators: • addition (+) • subtraction (-) • multiplication (*) • division (/) • modulus (%) • integer division (\)

  19. Relational Operators • The relational operators are used to determine whether two numbers are equal, or if one is greater or less than the other. Every relational statement evaluates to either 1 (TRUE) or 0 (FALSE).

  20. COMMENTS

  21. Comments • When you are writing a program: • always clear • self-evident what you are trying to do. • Comments are simply text that is ignored by the compiler, but that may inform the reader of what you are doing at any particular point in your program. • VB comments come in: • the single-quote(‘) comment, which tells the compiler to ignore everything that follows this comment, until the end of the line.

  22. Comments (cont.) • As a general rule, the overall program should have comments at the beginning, telling you what the program does. • Each function should also have comments explaining what the function does and what values it returns. • Finally, any statement in your program that is obscure or less than obvious should be commented as well.

  23. Comments (cont.) 'Declaring variables DimfirstNumber, secondNumber, total AsInteger 'Assign the value for each variables firstNumber = 10 secondNumber = 13 'calculate the total total = firstNumber * secondNumber 'display the output MsgBox(total)

  24. Comments (cont.) • DO add comments to your code.  • DO keep comments up-to-date.  • DO use comments to tell what a section of code does. • DON'T use comments for self-explanatory code.

  25. --- continue to next slide ---

More Related