midterm 0 review sessions3.amazonaws.com/.../midterm-0-review-session.pdfmidterm 0 review session...

42
Midterm 0 Review Session COMP110

Upload: trinhtuong

Post on 22-Apr-2018

216 views

Category:

Documents


2 download

TRANSCRIPT

Midterm 0 Review SessionCOMP110

Outline

• Concept Review

• Q&A

Variables

• Variables are “containers” that hold information

• We can give variables a name to help contextualize the data we are trying to manipulate

• In TS, they type of information dictates the type of variable you should use

Declaration

• “Speaking” a variable into existence

• let <name>: <type>;

• let x: number;

• let isWednesday: boolean;

• let firstName: string;

Initialization

• Giving a variable an “initial” value

• <name> = <value>;

• x = 7;

• isWednesday= true;

• firstName = “Jeffrey”;

Declaration and Initialization

• Sometimes we want to declare and initialize a variable at the same time!

• let <name>: <type> = <value>;

• let x: number = 7;

• let isWednesday: boolean = true;

• let name: string = “Jeffrey”

Everything in Typescript has a type!

Type Values

number Any number (including fractional) (1, 2, 3, 3.14,…)

boolean true and false

string Any text (“hello”, “goodbye”, etc.)

*Note that TS is case sensitive!!!

More on Strings

• In TS, strings are enclosed in double quotes – known as string literals

• Notice that even numbers can be strings: “123”

Strings

• In TS, strings are enclosed in double quotes – known as string literals

• We can “add” two strings together using string concatenation

• Ex.

• let currentDay: string = “Wednesday”;

• print(“Today is: ” + currentDay);

Scope

• Every variable in TS has a scope

• A scope is the context in which the variable is accessible

• The scope of a variable in TS is limited to the curly braces that enclose it

function perimeter(length: number): void {let result: number = length * 4;print(“The perimeter is “ + result);

}print(result); //BAD

Scope

• Two questions you should ask yourself about any variable you see in TS:• What is the type of the variable?

• What is the scope of the variable?

Functions

• By default, code executes line by line (sequentially)

• We can use functions for more complex control flow

Functions

1. Computer encounters a function call and marks a placeholder at the current line

2. Some code in the function runs

3. The computer returns to the placeholder and execution continues sequentially

Definition

function <name>(<parameters>): <return type> {

// something cool here

}

function perimeter(length: number): number {

let result: number = length * 4;return result;

}

Calling

<name>(<arguments>);

perimeter(4);

Parameters

• Our generic input when defining the function

• Specified as a comma separated list

Arguments• Our specific input when calling the function

• Specified as a comma separated list

<name1>, <name2>, <name3>, . . .

<name1>: <type1>, <name2>: <type2>, <name3>: <type3>, . . .

Parameters vs Arguments

• Parameters are the generic placeholders for arguments

• When a function is called, the arguments get copied into the parameters

• There must be a 1-to-1 correspondence between number of arguments passed and number of parameters!!

• Types must match as well!

function doSomething(num1: number, word1: string): void {//code elided

}

//within our code somewhere

doSomething(5, “hello”)

void functions

• Some functions don’t return a value, they just perform some computation and then end

• Even these functions “technically”: have a return type – void

• No return statement, execution ends when we hit the closing bracket

let energy: number = 0;

function drinkCoffee(): void {

energy = energy + 1;

}

return Statements

• Syntax: return <expression>;

• The type of the expression must match the return type of the function

• As soon as a return is encountered, execution of the function ends

function officeHoursBuilding(): string {return "sitterson";

}

print() vs return

function perimeter(length: number): number {

let result: number = length * 4;return result;

}

perimeter(4);

print(“Function has returned”);

print() vs return

function perimeter(length: number): number {

let result: number = length * 4;return result;

}

print(perimeter(4));

print(“Function has returned”);

Important note on return

• There can be multiple return statements per function

• However, only one return will be executed per invocation of the function

function dayOfWeek(day: string): string {

if (day === “Monday”) {

return “It is Monday :(“;

} else {

return “At least it isn’t Monday”;

}

}

Relational Operators

• We use relational operators as the basis for building conditional statements

• Evaluate to a boolean

• Examples:

let test1: boolean = 7 > 5;

let test2: boolean = 6 !== 2;

• What is the value of test1

and test2?

Logical Operators

• Logical operators used with boolean types only

• Used in creating conditional statements

• Two logical operators: && and ||

• Syntax: <boolean1> && <boolean2>

• && – Evaluates to true only if boolean1 AND boolean2 are both true

• || – Evaluates to true if boolean1 OR boolean2 are true

Booleans

condition1 && condition2

&&

Condition 1 Condition 2 Result

true true true

true false false

false true false

false false false

Booleans

condition1 || condition2

||

Condition 1 Condition 2 Result

true true true

true false true

false true true

false false false

Negation – !

• We can put ! in front of any statement to flip the meaning

!(7 > 9) -> true!(isWednesday) -> false!(5 <= 2) -> true

Ex:if (!isRaining){

print(“No need for an umbrella today!”);}

https://goo.gl/vZ3oHF

If – then – else

• Only the if or the else will run• Additionally, at least one will run, the program will never

skip both

if (<conditional>){//then do this

}else {

//do this instead}//rest of code

If – then – else

• IMPORTANT: Whatever you put inside the parentheses must evaluate to a boolean

if (<conditional>){//then do this

}else {

//do this instead}//rest of code

Assignment vs. Equality

• = is different from ===

• = is known as the assignment operator

let a: number = 6;

• === is the equality operator

7 === (2 + 5); -> true

Nested if

• We can nest multiple if – then – else statements in our code for more complex logic

if (<conditional>){

if (<conditional>){

//then do this

}

else {

//do this instead

}

}

else {

//do this instead

}

//rest of code

else-if

if (condition) {

//do this

}

else if (<condition>) {

//do this instead

}

else if (<condition>) {

//do this instead

}

else {

catch all case

}

while Loop

• while some condition is true, do something

• Syntax:

while (<condition>) {

//do something

}

//rest of code

• Whatever is inside the parentheses must evaluate to a boolean!!!!

Infinite Loops

while (<condition>){

//do something

}

• If <condition> never evaluates to false then we will loop forever

• Common causes: not incrementing your counter variable! Ex:

let i: number = 0;

while (i < 10){

print(i);

}

Infinite Loops

while (<condition>){

//do something

}

• If <condition> never evaluates to false then we will loop forever

• Common causes: incrementing counter variable outside the loop! Ex:

let i: number = 0;

while (i < 10){

print(i);

}

i = i + 1;

Arrays

• An array is a list

• Each element in the list is the same type

• Elements can be addressed by an index

• Indexing starts at 0!!!!!

Element 7 12 15 2 6 8 4 8 3 22

Index 0 1 2 3 4 5 6 7 8 9

Arrays

• We can access each element in an array <name>[<index>];

• Ex: myArray[5];

• Each array also has a length property

• Ex: myArray.length

• Indexes can be an expression: myArray[myArray.length – 1]

Element 7 12 15 2 6 8 4 8 3 22

Index 0 1 2 3 4 5 6 7 8 9

myArray

Array Syntax

What is a class?

• Blueprint for an object, it defines a type

• Wrapper around a bunch of logically related variables (properties)

class <Name> {

// variable (property) declarations here<name>: <type>;<name>: <type> = <default value>;

}

How to create objects

• Cannot initialize like simple types, must be constructed

let <name>: <ClassName> = new <ClassName>();

let <name>: <ClassName>;

<name> = new <ClassName>();

let myCar: Vehicle = new Vehicle();

let myCar: Vehicle;

myCar = new Vehicle();

Declaring and initializing object in one line

Declaring and initializing object in one line

Declaration

Declaration

Initialization

Initialization

Constructing Objects

// let <name>: <type> = new <type>();

let jeffrey: Person = new Person();

// access properties: <name>.<prop>

print(jeffrey.age); // 0

jeffrey.age = 22;

jeffrey.name =“Jeffrey”;

jeffrey.height = 69;

class Person {

name: string;

age: number = 0;

height: number = 0;

}