Quiz 1: Functions and Procedures

Similar documents
COMSC-051 Java Programming Part 1. Part-Time Instructor: Joenil Mistal

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

Control Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003

Control Structures. Lecture 4 COP 3014 Fall September 18, 2017

CSE 142 Su 04 Computer Programming 1 - Java. Objects

Control Structures in Java if-else and switch

Control Structures in Java if-else and switch

Flow Control. CSC215 Lecture

COMP101: Final Review. With your boy(s), Mason and San

APCS Semester #1 Final Exam Practice Problems

Chapter 2: Functions and Control Structures

8. Control statements

Second Term ( ) Department of Computer Science Foundation Year Program Umm Al Qura University, Makkah

Conditionals and Loops

While Loops CHAPTER 5: LOOP STRUCTURES. While Loops. While Loops 2/7/2013

Text Input and Conditionals

Overview: Programming Concepts. Programming Concepts. Names, Values, And Variables

Overview: Programming Concepts. Programming Concepts. Chapter 18: Get With the Program: Fundamental Concepts Expressed in JavaScript

Pace University. Fundamental Concepts of CS121 1

Decision Making in C

Introduction. C provides two styles of flow control:

CS111: PROGRAMMING LANGUAGE II

Introduction to Visual Basic and Visual C++ Arithmetic Expression. Arithmetic Expression. Using Arithmetic Expression. Lesson 4.

Visual C# Instructor s Manual Table of Contents

Control Structures. Code can be purely arithmetic assignments. At some point we will need some kind of control or decision making process to occur

Chapter 3. More Flow of Control. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal

Condition-Controlled Loop. Condition-Controlled Loop. If Statement. Various Forms. Conditional-Controlled Loop. Loop Caution.

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

DEBUGGING TIPS. 1 Introduction COMPUTER SCIENCE 61A

Introduction to C/C++ Lecture 3 - Program Flow Control

Course Text. Course Description. Course Objectives. StraighterLine Introduction to Programming in C++

Module 2: Choice and Iteration

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal

LESSON 3. In this lesson you will learn about the conditional and looping constructs that allow you to control the flow of a PHP script.

Tutorials. Inf1-OP. Learning Outcomes for this week. A Foundation for Programming. Conditionals and Loops 1

Chapter 17. Fundamental Concepts Expressed in JavaScript

COMP-202: Foundations of Programming. Lecture 6: Conditionals Jackie Cheung, Winter 2016

Working with JavaScript

Introduction to Bioinformatics

Comp 151. Control structures.

C++ Programming: From Problem Analysis to Program Design, Third Edition

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

npm run pull npm start

Scope of this lecture. Repetition For loops While loops

1007 Imperative Programming Part II

Review. Primitive Data Types & Variables. String Mathematical operators: + - * / % Comparison: < > <= >= == int, long float, double boolean char

Lesson 06 Arrays. MIT 11053, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL

3 Programming. Jalal Kawash Mandatory: Chapter 5 Section 5.5 Jalal s resources: How to movies and example programs available at:

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

C++ Data Types. 1 Simple C++ Data Types 2. 3 Numeric Types Integers (whole numbers) Decimal Numbers... 5

Module 3 SELECTION STRUCTURES 2/15/19 CSE 1321 MODULE 3 1

7/8/10 KEY CONCEPTS. Problem COMP 10 EXPLORING COMPUTER SCIENCE. Algorithm. Lecture 2 Variables, Types, and Programs. Program PROBLEM SOLVING

COMP-202: Foundations of Programming. Lecture 3: Boolean, Mathematical Expressions, and Flow Control Sandeep Manjanna, Summer 2015

Spring 2018 Discussion 7: March 21, Introduction. 2 Primitives

Introduction to Conditionals

Repetition Structures

All copyrights reserved - KV NAD, Aluva. Dinesh Kumar Ram PGT(CS) KV NAD Aluva

Java+- Language Reference Manual

L o o p s. for(initializing expression; control expression; step expression) { one or more statements }

CSE101-lec#12. Designing Structured Programs Introduction to Functions. Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU

CGS 3066: Spring 2015 JavaScript Reference

Contents. Jairo Pava COMS W4115 June 28, 2013 LEARN: Language Reference Manual

CMSC 330: Organization of Programming Languages. OCaml Imperative Programming

Conditionals, Loops, and Style

Lecture Outline. COMP-421 Compiler Design. What is Lex? Lex Specification. ! Lexical Analyzer Lex. ! Lex Examples. Presented by Dr Ioanna Dionysiou

Chapter 4: Making Decisions

Chapter 4 The If Then Statement

CS1004: Intro to CS in Java, Spring 2005

What is PHP? [1] Figure 1 [1]

CS302: Self Check Quiz 2

Reading assignment: Reviews and Inspections

Computers and FORTRAN Language Fortran 95/2003. Dr. Isaac Gang Tuesday March 1, 2011 Lecture 3 notes. Topics:

Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define

STATS 507 Data Analysis in Python. Lecture 2: Functions, Conditionals, Recursion and Iteration

Page 1. Reading assignment: Reviews and Inspections. Foundations for SE Analysis. Ideally want general models. Formal models

GridLang: Grid Based Game Development Language Language Reference Manual. Programming Language and Translators - Spring 2017 Prof.

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

Fundamentals. Fundamentals. Fundamentals. We build up instructions from three types of materials

Scala Style Guide Spring 2018

Introduction to Python

COMP 110 Introduction to Programming. What did we discuss?

Computational Expression

Definition: A data structure is a way of organizing data in a computer so that it can be used efficiently.

Chapter 4: Conditionals and Loops

Unit 2: Java in the small. Prepared by: Dr. Abdallah Mohamed, AOU-KW

Information Science 1

An Introduction to Programming with C++ Sixth Edition. Chapter 7 The Repetition Structure

Lab 5 - Repetition. September 26, 2018

INTRODUCTION TO C++ PROGRAM CONTROL. Dept. of Electronic Engineering, NCHU. Original slides are from

4. Inputting data or messages to a function is called passing data to the function.

MITOCW watch?v=0jljzrnhwoi

CONDITIONAL EXECUTION

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

IT 374 C# and Applications/ IT695 C# Data Structures

The way I feel about music is that there is no right and wrong. Only true and false. Fiona Apple. true false false

Repetition Structures II

Chapter 3 - Simple JavaScript - Programming Basics. Lesson 1 - JavaScript: What is it and what does it look like?

(Not Quite) Minijava

Decision Control Structure. Rab Nawaz Jadoon DCS. Assistant Professor. Department of Computer Science. COMSATS IIT, Abbottabad Pakistan

Transcription:

Quiz 1: Functions and Procedures

Outline Basics Control Flow While Loops Expressions and Statements Functions

Primitive Data Types 3 simple data types: number, string, boolean Numbers store numerical data Strings store textual data Booleans store true or false

Primitives Examples What are the types of these variables?

Declaring a Variable Springing a variable into existence Syntax:

Initializing a Variable After declaring a variable, we can assign it a value. Syntax:

Declaring and Initializing AT THE SAME TIME Very simple to declare and initialize in the same line, simply combine the two statements. Very common to see this when you know an initial value for a variable. Declaration Initialization

Type Inference Sometimes you explicitly declare a type, sometimes you don t Programs are able to infer the type of the assignment

Conditional Operators: Equality Equality Operators === equal to operator -- THREE equals symbols in a row.!== not equal to operator --! symbol followed by TWO equals symbols i. The! means NOT

Conditional Operators: Relational Relational Operators > Greater than >= Greater than or equal to At least < <= Less than Less than or equal to At most

Conditional Operators: Logical && Negation && AND operator double ampersand Using the AND operator with boolean expressions results in true only if both expressions are true. OR operator double vertical bar Using the OR operator with boolean expressions results in true if either expression is true or if both expressions are true.! NOT operator Flipped meaning of a boolean If something normally evaluates to true, prefacing it with the! operator will result in a false value ( and vice versa)

Control Flow Order in which statements, instructions, and function calls are executed Path of a program changes depending on the values of variables during execution Generally, the computer reads code as we read books BUT it depends on If-then-else Function calls Loops

Control Flow Variables : Program :: Characters : Book If-then-else Function calls While loops Declare variables before using them in your code

If-then and if-then-else if (<conditional statement>) { // code to execute ONLY if conditional statement is true } else { // code that executes ONLY when conditional statement is false } Use boolean expressions (evaluating to true or false) Useful when you want for the program to do different things based on a variable s value

If-then and if-then-else Say we re making someone s favorite food. The food that we make will depend on the recipient! What will this program print out? What s my favorite food? pretzels

If-then and if-then-else = Meredith ; Say we re making someone s favorite food. The food that we make will depend on the recipient! What will this program print out if person is now Meredith? What s my favorite food? cake

} How can we simplify this code? Avoid the need for a nested if statement by using an else if! nesting if (<conditional statement>) { // executes if true } else if (<conditional statement>) { // executes if true and first conditional statement is false } else { // executes if both conditional statements were false

Implementing an else if Avoid the need for a nested if statement by using an else if! 1. Remove the opening curly brace after the else, and remove the matching closing curly brace 2. Move the nested if to be right next to the else keyword, and fix the indentation and spacing More clean, concise, speedy, easy to read

Unreachable Statements WS0 Review: Is any of this code unreachable? When a statement can t possibly execute in any circumstance Common causes: Nested if statements within an else Code after a return statement (when return is not in an if block, etc.)

While Loops While (<boolean expression test >) { // repeat block - statements in braces run when the test is true } Code runs as long as the boolean expression is true After the last statement in the repeat block completes, computer jumps back to the test Repeats code while boolean expression continues to be true after executing the repeat block

While Loops: Control Flow When to use? Repeating a task a specific number times Avoiding infinite loops: Make sure that your boolean expression will evaluate to false eventually!! Increment your counter, change variable from true to false, etc.

While Loops vs. If-then-else WHILE Computer jumps back up to the test after executing the repeat block BOTH Test in parentheses must be a boolean expression If test evaluates to true: computer moves to the first line of code in the following { } If test evaluates to false, computer will jump over the following { } IF No repetition Opportunity to check for different boolean expressions with else if or execute default code with else

Check-in and Hot Date Check-in code: A4043 Find a partner, introduce yourselves to each other! Talk about why you are taking COMP110, and what you did last summer!

Expressions and statements A statement is an instruction that you are giving to the computer. Expressions are statements made up of variables, literals, operators, or functions that can be evaluated to produce a single result.

Example Expressions What type do the expressions on the right-hand side of the assignment operator evaluate to? What does the last line evaluate to?

Functions A function is a named section of a program that performs a specific procedure. You can use functions to do ~anything~

Defining Functions Parts of function definitions: Name: the name of our function Parameters: placeholder variables for expected inputs Return type: the type of value we want to return from the function Body: the statements or lines of code that make up the function This will often include return statements We define functions using the following syntax: let <name> = (<parameters>): <return type> => { //function body };

Calling Functions To run the code in our functions, we call them If the function definition has parameters, we must pass values (arguments) into the function call Arguments must match parameters in type and order To call functions we use the following syntax: <name>(<arguments>);

Calling Functions Given the function definition below, how would you call this function so that we combine apples with sugar to make our apple pie?

Arguments vs Parameters Parameters are the pieces of information that a function needs to run. Arguments are the actual values provided to a function.

Return Statements Return statements stop execution of a function, and return a single value back to the location where it was called. Execution stops even if we are within an if statement, while loop, etc. Return value must match the return type of a function.

Practice 1 Given which answer choice swaps these 2 values correctly? a) b) c) d)

Practice 2 Create function calls that can result in each letter being returned. Are all of the branches reachable?

Practice 3 If you had to rename this function, what would you call it? It s the power function!