0% found this document useful (0 votes)
7 views3 pages

WEEK 2 Variables - Prompts

Uploaded by

Jc
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views3 pages

WEEK 2 Variables - Prompts

Uploaded by

Jc
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Displaying Info, Text, Value of a variable

- Use Print, write and display

- To display value of variable (ex: If your grosspay is 200 — put (Display grosspay)
when you put that in it will display 200)

- If there is “String grosspay= “200” (you still put “Display grosspay”)


- To display a value put it without “”, if you want to add words put it in “”

Using Prompts
- Use a prompt which asks user to enter specific value, use uppercase letters:
- First DECLARE your variable(age), DISPLAY prompt, INPUT
1. Declare Integer age
2. Display “What is your age?”
3. Input age

Variable Initialization
- When declaring a variable, you can optionally assign a value to it in the declaration
statement
Ex: Declare Real price = 49.95 (this assigns the value 49.95 to the variable “price”)

Uninitialized Variables

- A variable that has been declared, but not assigned any value
- Can cause errors and could assign a default value of 0

Spot the Error:

- test3 doesn’t have a set value (the score is unknown), so you won’t be able to add
all the test scores to get the average

- Use word “set” to give value to the variable (ex: Set test1 = 88.0)
- The word “real” means value has decimals
- The word “integer” means the value is a whole number without decimals
Assignments and Calculations
- An assignment statement gives a variable a value (Ex: Set price = 20)

- Modulus gives the remainder (10/3 = 3 it doesn’t show 3.3, it gives whole number,
10%3 = 1)
- Exponent (2^3)
- Division gives the quotient (10/3 = 3 it doesn’t show 3.3, it gives whole number)

PROBLEM 1

USE TASK LIST:


1. Declare variables
2. Get the original price of the item
3. Calculate 20 percent of the original price. This is the amount of the discount
4. Subtract the discount from the original price. This is the sale price
5. Display the sale price
ALGORITHM
1. Declare Real originalPrice, discount, salePrice
2. Display “Input the original sales price”
3. Input originalPrice
4. Set discount = originalPrice * 0.2
5. Set salePrice = originalPrice - discount
6. Display “Discounted Price = $”, salePrice
7. Set salePrice = originalPrice - (originalPrice * 0.2) (you no longer need
variable discount and step 4)

You can set a whole number to a “real amount” and “integer” but cannot set a decimal
number into integer, only into a “real amount” (If you have a whole number such as 3 and
put “Set Real amount” it will display 3.0, if you put “Set Integer amount” it will display 3)
(If you have a decimal such as 3.7 and try to set it as an integer amount, it will cause an
error. It has to be set as a real amount)
Constants
- A value that cannot be changed
- Constant is written in all uppercase letters (ex: Constant Real INTEREST_RATE =
0.069) (You use Constant instead of Declare)
- If you notice a variable is not being affected by other variables, declare the variable
as a constant
- Constant variables must have a value, cannot be left blank

You might also like