🖥️
Intro to Computer Science (ICS3U/C)
  • An Introduction to Computer Science
  • Videos & Slides
  • Unit 1: In the Beginning
    • The History of Computers
    • Binary & Logic
      • Bits and Bytes (Binary)
      • Transistors (Changing Bits)
      • Logic Gates
        • Poster
        • Logic.ly
    • The Parts of a Computer
  • Unit 2: Intro to Code
    • How Do We Code?
      • Coding Conventions (Rules)
      • Commenting Code
    • What is HTML?
      • Hello World! (in HTML)
      • HTML Slideshow
    • Hello World!
    • Input / Output
      • The Console
      • Prompt, Alert, Confirm
    • Variables & Data
      • Strings (Text)
      • Numbers (Values)
        • Converting & Rounding
        • The Math Object
          • Random Numbers
      • Booleans
        • Truthiness
      • Arrays
  • Unit 3: Control Flow
    • Conditionals (if this, do that)
      • If...Else
        • Logical Operators
      • Switch / Case
      • Ternary Operators
    • Loops (Repeating Code)
      • For...Loop
      • While & Do/While Loops
    • Debugging
  • Unit 4: Functions
    • Functional Programming
    • User Defined Functions
      • Hoisting and Scope
    • Calling a JS Function
  • TL;DR
    • Programming Basics
    • Slideshows & Demos
    • Javascript Syntax Poster
  • Advanced Topics
    • Recursion
    • Structures & Algorithms
    • Mmm... Pi
  • External Links
    • Typing Club!
    • repl.it
    • Khan Academy
    • Geek Reading
    • ECOO CS Contest
Powered by GitBook
On this page
  1. Unit 2: Intro to Code

Variables & Data

So, you've mastered the Hello World! program and you want to move forward? Excellent. Let's talk about storing and manipulating Data.

PreviousPrompt, Alert, ConfirmNextStrings (Text)

Last updated 6 years ago

A variable holds information, numbers, words: data. It gives the programmer the ability to store, manipulate, retrieve, and output information. Some programming languages are . They have a specific kind of variable, depending on what you want to do. This is important because different types of variables require different amounts of memory (RAM) in the computer. C++ and Java are strongly (or statically) typed languages. Here are a few variable types, the amount of memory they require, and their limitations:

DATA TYPE

CONTENTS

SIZE (IN BYTES)

RANGE

short int

Whole Numbers

2

-32,768 to 32,767

int

Whole Numbers

4

-2,147,483,648 to 2,147,483,647

unsigned int

Positive Whole Numbers

4

0 to 2,147,483,647

char

Single Character

1

-128 to 127

float

Numbers, including decimal values

4

bool (or boolean)

True or False

1

0 to 1

String

Text*

Varies

Char[] array

*Strings are not a primitive data type, they are an array of chars or a special type called an object. More on data types: C++: Java: Javascript:

When creating a variable in strongly typed languages, the programmer needs to know exactly what the variable will be holding. The micro-management of memory and data types is what makes C++ such a strong language for things like video games.

Javascript is . Neither is Python, Ruby, Perl, or Lua. They are what we call . They can transition between variable types and do not require the programmer to declare what type of variable is required. The interpreter will even convert a character to a number and back again, as needed.

Javascript uses a keyword to create (declare) a variable. There are three different keywords, each with a special meaning:

// can hold all sorts of data and is globally scoped (more on scope later)
var myVariable;

// can hold all sorts of data and is locally or block scoped
let myOtherVariable;

// can hold all sorts of data but that data cannot change (or shouldn't, at least)
const myConstant;

For this course, it would be best if we use let as often as possible.

I don't expect everyone to read it, but on variables in JS.

The "const" declaration in JS does not necessarily mean the variable cannot change. This is a strange property of Javascript and is above the scope of this course. If you'd like to learn more, you can search "javascript immutable const".

Python is a dynamically typed language. You don't even need to put a declaration at all before a variable. You can simply give a new variable name and Python will open memory for it.

a = 123 			            # integer
b = 123L			            # long integer
pi = 3.14 			            # double float
myString = "hello" 			    # string
myList = [0,1,2] 			    # list
myTuple = (0,1,2) 			    # tuple
myFile = open(‘hello.py’, ‘r’) 	# file

C++ is a very strongly or statically typed language. It even has something called pointers which point to (reference) an area in memory instead of a value. Kind of like pointing to your locker instead of seeing what is in your locker.

int myInt;
int * myPointer;   // A pointer to an int
float myBigNumber;
bool theSkyIsBlue = true;   // You can initialize variables as you declare them
char aSingleLetter;
char myWord[] = { 'H', 'e', 'l', 'l', 'o', '\0' };    // String
char myOtherWord[] = "Hello";

Java is extremely similar to C++ in terms of variable declarations and options. It is a strongly (or statically) typed language.

int numberOfDays = 7;
byte nextInStream;
short hour;
long totalNumberOfStars;
float reactionTime;
double itemPrice;
String helloWorld = "Hello World!";
String newStringOnHeap = new String("Goodbye Sleep!");

Java is a bit different than C++ in that it has a String variable (array of chars). A String is more than just a variable - it is something called an Object. You'll see in the code above, there are two ways to declare a String. That's because there is something called a Heap in Java. More on that in Grade 12!

17×10−38 to 1.7×103817\times10^{-38} \text{ to }1.7\times10^{38}17×10−38 to 1.7×1038
"Typed" languages
https://www.geeksforgeeks.org/c-data-types/
https://www.geeksforgeeks.org/data-types-in-java/
https://www.geeksforgeeks.org/variables-datatypes-javascript/
not a strongly typed language
dynamically typed
here is a great chapter