boolean & if-then-else

Similar documents
boolean Expressions Relational and Equality Operators and if-then-else Conditional Control Statement

logical operators and else-if statements

Practice with if-then-else. COMP101 Lecture 7 Spring 2018

Classes, Types, and Objects

VSCode: Open Project -> View Terminal -> npm run pull -> npm start. while Loops. Lecture 09. Don t Stop Til You Get Enough

npm run pull npm start

FUNctions. Lecture 03 Spring 2018

Arrays and while Loops. Lecture 15 - COMP110 - Spring 2018

Data Types and the main Function

npm run pull npm start

Scopes Global and Block

npm run pull npm start

Data Classes & Objects. Lecture 19 - Spring COMP101

Introduction to Lists and Recursion

Data Classes & Objects and CSV Processing. Lecture 8 - Spring COMP110

npm run pull npm start

Recursive Data Types, null, and Linked Lists

06 - Building Lists Recursively. Spring 2018 COMP110

VSCode: Open Project -> View Terminal -> npm run pull -> npm start. Lecture 16

Mouse / Keyboard Events & Anonymous Functions. Lecture 19

Programming Language. Control Structures: Selection (switch) Eng. Anis Nazer First Semester

Functions as Parameters & Functional Interfaces. Lecture 12 COMP110 Spring 2018

Introducing: main Function, Comments, Statements

CS 106 Introduction to Computer Science I

Sorting, Searching and Array's Built-in Methods. Lecture 16 COMP110 Spring 2018

Lecture 02 Making Decisions: Conditional Execution

Selection Structures II

Programming in C++ PART 2

Function Literals and Type Inference. Lecture 14 Spring 2018 COMP110

Pull Lecture Materials and Open PollEv. Poll Everywhere: pollev.com/comp110. Lecture 12. else-if and while loops. Once in a while

AQA Decision 1 Algorithms. Section 1: Communicating an algorithm

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 8: SEP. 29TH INSTRUCTOR: JIAYIN WANG

Sorting and Searching. Lecture 14

Changing an Object s Properties

Open and respond to this poll: PollEv.com/comp110. Calling Methods Mr. Roboto

An introduction to R: Organisation and Basics of Algorithmics

Assessment - Unit 3 lessons 16-21

Week 4 EECS 183 MAXIM ALEKSA. maximal.io

Typescript on LLVM Language Reference Manual

2D Array Practice. Lecture 26

Programming Karel the Robot

Making Decisions. Agenda

Control Flow: Branching booleans and selection statements CS GMU

Computer Science 217

Administration. Exceptions. Leftovers. Agenda. When Things Go Wrong. Handling Errors. CS 99 Summer 2000 Michael Clarkson Lecture 11

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

CMSC 201 Computer Science I for Majors

CS Introduction to Programming Fall 2016

Lecture 8: Conditionals & Control Flow (Sections ) CS 1110 Introduction to Computing Using Python

BCIS 3630 Dr. GUYNES SPRING 2018 TUESDAY SECTION [JAN version] GRADER COURSE WEBSITE

CS1 Lecture 3 Jan. 18, 2019

CHRIST THE KING BOYS MATRIC HR. SEC. SCHOOL, KUMBAKONAM CHAPTER 9 C++

CMSC201 Computer Science I for Majors

Introduction to Scratch

CS1150 Principles of Computer Science Boolean, Selection Statements

Intensive Introduction to Computer Science. Course Overview Programming in Scratch

Excel Functions & Tables

COMP-202: Foundations of Programming. Lecture 8: for Loops, Nested Loops and Arrays Jackie Cheung, Winter 2016

Object-Oriented Programming. Lecture 17

Lecture 5 Tao Wang 1

AP Statistics Assignments Mr. Kearns José Martí MAST 6-12 Academy

Recap. ANSI C Reserved Words C++ Multimedia Programming Lecture 2. Erwin M. Bakker Joachim Rijsdam

Conditionals & Control Flow

Introduction to Mathematical and Scientific Programming TUTORIAL WEEK 2 (MATH 1MP3) Winter 2019

CS 106A, Lecture 7 Parameters and Return

Lecture 02. The "Moves" and Quiz 0

5.1. Examples: Going beyond Sequence

Introduction to Information Technology ITP 101x (4 Units)

CHAPTER : 9 FLOW OF CONTROL

How It All Stacks Up - or - Bar Charts with Plotly. ISC1057 Janet Peterson and John Burkardt Computational Thinking Fall Semester 2016

15-110: Principles of Computing, Spring 2018

Mobile Computing Professor Pushpendra Singh Indraprastha Institute of Information Technology Delhi Java Basics Lecture 02

Default Parameters and Shapes. Lecture 18

H212 Introduction to Software Systems Honors

Selection statements. CSE 1310 Introduction to Computers and Programming Alexandra Stefan University of Texas at Arlington

Introduction to Game Programming. 9/14/17: Conditionals, Input

Announcements. 1. Forms to return today after class:

CSE115 / CSE503 Introduction to Computer Science I. Dr. Carl Alphonce 343 Davis Hall Office hours:

Loops (while and for)

Administration. Conditional Statements. Agenda. Syntax. Flow of control. Lab 2 due now on floppy Lab 3 due tomorrow via FTP

Object-Oriented Programming

Topic 5: Enumerated Types and Switch Statements

61A Lecture 21. Friday, March 13

Michele Van Dyne Museum 204B CSCI 136: Fundamentals of Computer Science II, Spring

Flow Control. CSC215 Lecture

Computers 101. Lecture 1

Introduction to Programming for Biology Research

Week 8: The fundamentals of graph theory; Planar Graphs 25 and 27 October, 2017

YEAH 2: Simple Java! Avery Wang Jared Bitz 7/6/2018

CS-201 Introduction to Programming with Java

INTERMEDIATE LEVEL PYTHON PROGRAMMING SELECTION AND CONDITIONALS V1.0

CS1 Lecture 3 Jan. 22, 2018

CS61C Machine Structures. Lecture 3 Introduction to the C Programming Language. 1/23/2006 John Wawrzynek. www-inst.eecs.berkeley.

Lecture 3. COMP1006/1406 (the Java course) Summer M. Jason Hinek Carleton University

(4-2) Selection Structures in C H&K Chapter 4. Instructor - Andrew S. O Fallon CptS 121 (September 12, 2018) Washington State University

A453 Task 1: Analysis: Problem: Solution:

Chapter. Solving Problems that Require Decisions. Objectives: At the end of the chapter students should be able to:

Linked Lists CS 16: Solving Problems with Computers I Lecture #16

Relational & Logical Operators, if and switch Statements

Introduction to Decision Structures. Boolean & If Statements. Different Types of Decisions. Boolean Logic. Relational Operators

Transcription:

boolean & if-then-else Lecture 03 Step 1: Open VSCode and its Integrated Terminal Step 2: npm run pull Step 3: npm run start Step 4: Open another tab to pollev.com/comp110

Assignments Out Problem Set 0 Due tomorrow at 11:59pm Worksheet 0 Due Tuesday at 11:59pm Because we grade worksheets manually, we cannot accept worksheets beyond 24h after the deadline. We will drop the lowest worksheet score of the semester. Instructions for scanning and uploading are on the comp110.com website

Labor Day Vacation's Special Office Hours & Review Sessions Schedule Office Hours in Sitterson 008 open today until 8pm and tomorrow until 5pm (Labor day) Office hours *not* open Sunday nor Monday Small group review sessions on Thursdays and Fridays in Fred Brooks 008 from 2pm to 6pm Thurs, 2pm to 5pm Fri (Labor day)

Warm-up #1: Given the two separate programs A and B below, which one will print 110? pollev.com/comp110 function assign(x: number): void { let y: number; y = x; function main(): void { let y: number = 0; assign(110); print(y); main(); A let y: number = 0; function assign(x: number): void { y = x; function main(): void { assign(110); print(y); main(); B

Tracing A - Incorrect - Prints 0 function assign(x: number): void { let y: number; y = x; function main(): void { let y: number = 0; assign(110); print(y); main();

Tracing B - Correct - Prints 110 let y: number = 0; function assign(x: number): void { y = x; function main(): void { assign(110); print(y); main();

"Global" Variables Variables declared outside of functions are called global variables. Functions in the same file can read and assign values to global variables. Useful for keeping track of the state of a program... i.e. a player's score in a game i.e. a user's responses to a series of questions In our early days as a programmers, global variables are OK to use. We will learn better ways to keep track of state later in the semester, though!

Warm-up #2: What is the output of the following program? pollev.com/comp110 let x: number = 110; let y: number = 401; function main(): void { let temp: number; temp = x; x = y; y = temp; print(x + ", " + y); main();

Concatenation - string's + Operator When you "add" two Strings together it is called Concatenation let example: string = "Hello" + "World"; // example's value is now "HelloWorld" Concatenation means, simply, to "join" one string to another. When the program concatenates two strings, the result is a single String value. You can also concatenate numerical types to a String. This is very useful! let answer: number = 42; let example2: string = "The answer is " + answer; // example2's value is now "The answer is 42"

The boolean type Only two possible values: true or false A built-in, simple data type just like string and number Exist in every programming language and fundamental to computational logic.

How do we compare number and string data? with relational and equality operators! Test Math TypeScript Operator "is greater than?" > > "is at least?" >= "is less than?" < < "is at most?" <= "is equal to?" = === "is not equal to?"!==

Hands-on #1) Boolean Expressions Open Lecture 3's 00-booleans-app.ts program There are some number and string variables declared at the top of the file you will make use of in the subsequent print statements. Your goal is to get each of the print statements to print true using the prescribed operator (i.e. >, >=, ===, etc) and any two of the variables available. Check-in on PollEv.com/comp110 once you are printing all true values.

The equal to Operator is === THREE equals symbols side-by-side means "is equal to?" 1 === 1 true 1 === 2 false Important! Equality operator is very different from the assignment operator. = is read as "is assigned a value of" === is read as "is equal to?" let b: boolean = x === y; "The boolean variable b is declared and assigned an initial value of testing 'is x equal to y?'" Use only w/ simple types (string, number, boolean), not w/ Objects/Arrays.

The not equal to Operator is!== The! symbol in many programming languages means "NOT" 1!== 1 false 1!== 2 true let b: boolean = x!== y; "The boolean variable b is declared and assigned an initial value of testing 'is x not equal to y?'"

Follow-along: if-then statements Open 01-if-then-app.ts We will write our first if-then statement! if (humidity > 70) { print("it is humid.");

if-then Statements General form of an if-then statement: if (<"test" - boolean expression>) { <then block runs when test is true> if-then is a statement. It can be written anywhere you can write a statement. The test you place in the parenthesis must be a boolean expression. The statements within the curly brace "then block" will run if the test expression evaluates to true. Otherwise, the computer will jump over the then block.

if-then Statements In a flow chart ("control flow") we draw an if-then statement as a diamond. It will have two arrows coming out. We label these arrows for the two cases: true will point to code in the then block false will point to code following the then block if test then true false

print("the humidity is..."); if (humidity >= 70) false true { print("it is humid."); print("have a great day :)");

Hands-on #2: Write your own if-then statement Open 02-if-then-else-app.ts Under the TODO comment, write an if-then statement that will print "It is hot." if the value of the temperature variable is greater than 80 degrees. Try changing the value of the temperature variable to convince yourself it works. Check-in on pollev.com/comp110 once complete

How do we perform a different action when the test condition is false?

Follow-along #2: Writing an if-then-else statement 1. Still in 03-if-then-else-app.ts, we will add an else statement together if (temperature > 80) { print("it is hot."); else { print("it is nice outside.");

if-then-else Statements General form of an if-then-else statement: if (<"test" - boolean expression>) { <then block runs when test is true> else { <else block runs when test is false> Works the same as an if-then statement, however, when the test expression evaluates to false the statements within the else block will run. Once either block completes, the computer resumes at the next line following the else block.

if-then-else Statements Notice, like the if-then statement, the then block runs only when the test condition is true Unlike the if-then statement, the else block runs only when the test condition is false After either the then-block or else-block complete, they both continue to the same next step if test then else true false

Hands-on #3) Write an if-then-else statement 1. Open 03-number-game-app.ts 2. Implement the logic described in the TODO using an if-then-else statement: 3. Choose any number you'd like to be your answer for your program. IF the user guesses it correctly, call the function named correct OTHERWISE, call the function named incorrect 4. Check-in when you have completed #3: pollev.com/comp110

Nesting if-then-else statements within if-then-else statements The then and else blocks contain one or more statements but isn't if-then a statement? Yes! You can write further if-then statements inside of then or else blocks and the same rules apply.

Hands-on #4) Write an if-then-else statement 1. Still in 03-number-game-app.ts 2. Inside of the else block, rather than calling the function named incorrect, write an if-then-else statement with this logic: 3. If the user's guess was TOO HIGH, call the function named lower, otherwise, call the function named higher 4. Check-in when you have completed #3: pollev.com/comp110

if (guess === 42) { correct(); else { if (guess > 42) { lower(); else { higher();

Warm-down Question: What is the output of the following code? function a(): void { print("a"); b(); print("a"); function b(): void { print("b"); c(); print("b"); function c(): void { print("c"); a();