Understanding Variables in Programming and Memory Concepts

Slide Note
Embed
Share

Variables are essential in programming as they allow temporary storage of data within a program. This content delves into the concept of variables, their importance, and how they function in memory. It explains the relationship between variable names and memory locations, discusses data types, and highlights the necessity of declaring variables before use. Understanding variables is fundamental to grasping the core of programming and efficient problem-solving with computers.


Uploaded on Sep 26, 2024 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. Download presentation by click this link. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

E N D

Presentation Transcript


  1. CST 1101 CST 1101 Problem Solving Using Computers Problem Solving Using Computers 1

  2. CST 1101 Flowgarithm Variables 2

  3. Variables Variables Once a programmer has understood the use of variables, he has understood the essence of programming -Edsger Dijkstra 3

  4. Variables Variables Variable: a small piece or chunk of data. Variables enable one to temporarily store data within a program, and are therefore very useful. Note: variables are not persistent. When you exit your program, the data is deleted. To create persistent data, you must store it to a file system. 4

  5. Bucket Analogy Bucket Analogy It is useful to think of a variable as a bucket of data. The bucket has a unique name, and can only hold certain kinds of data. balance is a variable containing the value 200, and can contain only integers. 200 balance 5

  6. Memory Concepts Memory Concepts Variable names correspond to locations in the computer s primary memory. Every variable has a name, a type and a value. However, the value of a variable can vary (change) as a program executes. That's why we call them "variables"! As an example, study the following table that traces the value of a variable called X. 6

  7. Data Types Data Types Every variable must have two things: a data type and a name. Data Type: defines the kind of data the variable can hold. For example, can this variable hold numbers? Can it hold text? Flowgarithm, Python, Java support several different data types. 7

  8. Flowgarithm Flowgarithm Data Types Data Types In Flowgarithm, variables, before they can be used, should be declared. You need to specify: Name Type Integer Real String Boolean Data type cannot be changed during the execution 8

  9. Changing the Variable Value Changing the Variable Value A variable can have its value set (or changed) in one of three ways: By the value entered from an INPUT statement. By the value entered or calculated from an equation in an ASSIGN statement. By a return value from a procedure call (more on this later). It is variables, and their changing data values, that enable a program to act differently every time it is executed. 9

  10. Description Value of X Program When the program begins, no variables exist. In Flowgarithm, variables need to be declared. Variable declaration is: variable name and variable type Undefined The first assignment statement, X 32, assigns the data value 32 to the variable X. 32 The next assignment statement, X X+1, retrieves the current value of X, 32, adds 1 to it, and puts the result, 33, in the variable X. The next assignment statement, X X*2, retrieves the current value of X, 33, multiplies it by 2, and puts the result, 66, in the variable X. How many vales was stored in X during the program execution? Let us reorder the value assignments. What happens? Introduce another variable 33 66 10

  11. Tracking Variables Values Tool Tracking Variables Values Tool * Integer can store a positive or negative whole number, but can't store fractional values. If a number with a fractional value is stored into a integer, the fractional value will be discarded. The Variable Watch Window displays integers in blue. * Real data type can store any number - both whole numbers and ones with fractional values. The Variable Watch Window displays reals in purple. * String data type is used to store any textual data. This includes words, letters, or anything else you would send in a text message. The Variable Watch Window displays strings in red. * Boolean can store either "true" or "false". These are the basis of decision making in a computer program. The Variable Watch Window displays Booleans in teal. http://www.flowgorithm.org/documentation/types.htm 11

  12. Execute to completion Execute to step-by-step Flowgarithm allows you to check the current value of the variables used in the flowchart 12

  13. What can be done with variables of type Sting What can be done with variables of type Sting String : concatenation John & Smith = JohnSmith John & Smith = John Smith John & & Smith = John Smith 13

  14. How to Input / Output Values? How to Input / Output Values? Input statement / symbol: An input statement/symbol allows the user of a program to enter a variable value during program execution. It is important that a user know exactly what type of value is expected for input. Output statement / symbol: Flowgarithm Console captures the value of the output statement: This value can be a name of a variable (then the variable value is submitted for output) Or it is a string that combines strings and variable names into one string using the & symbol an then this string is submitted for output 14

  15. String Variable String Variable 1. How many variables are used in this flowchart? 2. What values are assigned to the variable? 15

  16. Exercise Exercise Create flowchart that prompts the user to enter her name and her age and then output this information. 16

  17. Exercise Exercise Create a flowchart that prompts the user to enter two numbers and then displays the sum, product, and average of these numbers. Here is a sample output 17

  18. Exercise Exercise Create a flowchart that calculates a user s weekly paycheck. The program should prompt the user for his/her name, rate of pay, and hours worked. Then, the program should summarize the input. 18

  19. Variable Names (i.e., Identifiers) Variable Names (i.e., Identifiers) In Flowgorithm, identifiers must adhere to the following rules: They must start with a letter. After the first letter, the identifier can contain addition letters or numbers. Spaces are not allowed. They cannot be reserved words or words already defined by Flowgorithm (please see below) Also note: Languages such as Visual Basic and C also allow the underscore character "_". Flowgorithm, however, does not allow it. Identifiers are not case-sensitive. The following are some simple example identifiers. http://www.flowgorithm.org/documentation/keywords.htm 19

More Related Content