Download presentation
Presentation is loading. Please wait.
1
CST 1101 Problem Solving Using Computers
2
Flowgarithm Variables
CST 1101 Flowgarithm Variables
3
Variables -Edsger Dijkstra
“Once a programmer has understood the use of variables, he has understood the essence of programming” -Edsger Dijkstra
4
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.
5
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
6
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.
7
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.
8
Flowgarithm 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
9
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.
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. 33 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. 66 How many vales was stored in X during the program execution? Let us reorder the value assignments. What happens? Introduce another variable
11
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.
12
Execute to completion Execute to step-by-step Flowgarithm allows you to check the current value of the variables used in the flowchart
13
What can be done with variables of type Sting
String : concatenation “John” & “Smith” = “JohnSmith” “John” & “ Smith” = “John Smith” “John” & “ “ & “Smith” = “John Smith”
14
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
15
String Variable How many variables are used in this flowchart?
What values are assigned to the variable?
16
Exercise Create flowchart that prompts the user to enter her name and her age and then output this information.
17
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
18
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.
19
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.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.