0% found this document useful (0 votes)
21 views17 pages

Data Type, Variable(2)(2)

Uploaded by

pranav.garg1006
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)
21 views17 pages

Data Type, Variable(2)(2)

Uploaded by

pranav.garg1006
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/ 17

Basic Program Structure

Data Type, Variable

Introduction to Computational Thinking 2


Lesson Objectives

At the end of this lesson, you should be able to:


• Explain the following concepts:
- Variables
- Assignment operator
- Arithmetic operators
- Basic numeric data types

• Use variables, assignment operator, arithmetic operators,


and basic numeric data types in coding

Introduction to Computational Thinking 3


Topic Outline

Variables

Assignment Operator

Arithmetic Operators

Basic Numeric Data Types

Introduction to Computational Thinking 4


Scenario 3: Find the Distance Traveled

Introduction to Computational Thinking 5


Scenario 3: Find the Distance Traveled

+ 7
4

Introduction to Computational Thinking 6


Variables

In most computer programs, we need data storage to represent and store data temporarily.

A variable is like a labeled box that contains a value inside it.

Why abstraction (give names to values of expression)?

• Reuse names instead of values


- Helpful in keeping track of useful information without
needing to remember a bunch of numbers

moneyInWallet moneyInWallet priceOfChickenRice priceOfCoffee

• Easier to change the code later

Introduction to Computational Thinking 7


Variable Names and Values

Each variable has its:

• Name: e.g., priceOfChickenRice

• Value: e.g., 2.8

Name Value
priceOfChickenRice = 2.8
priceOfChickenRice 2.8

Introduction to Computational Thinking 8


Expressions

Expression: anything that produces/ returns a value

• combination of values (e.g., literal, variables, etc.) and operations (e.g., operators, functions, etc.)

moneyInWallet priceOfChickenRice priceOfCoffee

Examples
• 3.14
• 100*15
• Result *100

Introduction to Computational Thinking 9


Assignment Operator

Assignment Operator: binds variables and values

The ‘=‘ sign is the assignment operator, not the equality in mathematics.

When we see: We mean:

x = 7.1 We have a variable called x and a value of 7.1 assigned to it.

We evaluate the value of the expression (7*2 -5), that is 9 and


x=7*2–5
assign a value of 9 to x.
x=x+7 We recall the value of x, add 7 to it, and assign the expression result to x.

Introduction to Computational Thinking 10


Assignment Operator

Basic Syntax

Left Hand Side (LHS) = Right Hand Side (RHS)

a variable an expression

Assignment means:

1. Evaluate the expression on RHS

2. Take the resulting value and assign it to the name (variable) on the LHS.

Introduction to Computational Thinking 11


Basic Numeric Data Types: Integers and Floats

Integers • are like whole numbers, including the negative numbers

• can be negative {–1, –2, –3, – 4, – 5, …}, positive {1, 2, 3, 4, 5, …}, or zero {0}

Integers: {…, –5, –4, –3, –2, –1, 0, 1, 2, 3, 4, 5, …}

Integers

–5 –4 –3 –2 –1 0 1 2 3 4 5

Whole Numbers

Floats Represent real numbers; allow fractions (e.g., 2.8, 7.1, 9.0001)

Introduction to Computational Thinking 12


Arithmetic Operators

• Used in common arithmetic

• Each arithmetic operator is a mathematical function that takes one/ two operand(s) and
performs a calculation on them

• Most computer languages contain a set of such operators that can be used within
equations to perform a number of types of sequential calculation

Different programming languages support


arithmetic operators in different ways.

Introduction to Computational Thinking 13


Scenario 3: Find the Distance Traveled - Flowchart

Flowchart

start

Read horizonDist

Read vertDist
3
dist = horizonDist + vertDist

+ 7 Display distance travelled


4 between these two points

end

Introduction to Computational Thinking 14


Question to Ponder Before Coding Scenario 3

For the next lesson…

• How many variables should you define?

• What is the data type of each variable?

• Do you need assignment operator in your program?

• Do you need arithmetic operators in your program?

Introduction to Computational Thinking 15


Summary

Binding variables and values


Name
• equal sign is an assignment of a
• descriptive value to a variable name
• meaningful • value is stored in computer memory
Variable: • helps you re-read code • an assignment binds a name to a
Abstracting has • cannot be keywords value
expressions • retrieve value associated with name
or variable by invoking the name

Value Basic Data Types Arithmetic


Operators:
• information stored • integers
• can be updated • floats + – × ÷
More on this later..

Introduction to Computational Thinking 16


References for Images

No. Slide No. Image Reference


Money in the wallet [Online Image]. Retrieved April 18, 2018 from
1 7, 9
https://torange.biz/money-wallet-1376/.

By No machine-readable author provided. Terence assumed (based on copyright


claims). - No machine-readable source provided. Own work assumed (based on
2 7, 9
copyright claims)., CC BY 2.5,retrieved April 18, 2018 from
https://commons.wikimedia.org/w/index.php?curid=696774.

Nilsson S. (2014). Cup of coffee [Online Image]. Retrieved April 17, 2018 from
3 7, 9
https://www.flickr.com/photos/infomastern/14956851150.

By User:Bobarino - Made by following Information.png, CC BY-SA 3.0, retrieved


4 13
April 18, 2014 from https://en.wikipedia.org/w/index.php?curid=9180601.

Question problem [Online Image]. Retrieved April 18, 2018 from


5 15
https://pixabay.com/en/question-problem-think-thinking-622164/.

Introduction to Computational Thinking 17

You might also like