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

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

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

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

Java Bytecode (binary file)

COMPUTER PROGRAMMING LOOPS

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

Introduction to Object-Oriented Programming

Pace University. Fundamental Concepts of CS121 1

8. Control statements

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

CS 106 Introduction to Computer Science I

Key Differences Between Python and Java

CS111: PROGRAMMING LANGUAGE II

Quiz 1: Functions and Procedures

APCS Semester #1 Final Exam Practice Problems

4. Structure of a C++ program

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

YOLOP Language Reference Manual

CS 106 Introduction to Computer Science I

(Not Quite) Minijava

CS125 : Introduction to Computer Science. Lecture Notes #6 Compound Statements, Scope, and Advanced Conditionals

Introduction. C provides two styles of flow control:

Programming Lecture 4

Chapter 8. Statement-Level Control Structures

Java Programming: Guided Learning with Early Objects Chapter 5 Control Structures II: Repetition

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

Working with JavaScript

Introduction to Programming Using Java (98-388)

In Java, data type boolean is used to represent Boolean data. Each boolean constant or variable can contain one of two values: true or false.

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

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

Flow Control. CSC215 Lecture

Objectives. Chapter 4: Control Structures I (Selection) Objectives (cont d.) Control Structures. Control Structures (cont d.) Relational Operators

Java Programming: Guided Learning with Early Objects Chapter 5 Control Structures II: Repetition

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

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

Chapter 4: Control Structures I (Selection) Objectives. Objectives (cont d.) Control Structures. Control Structures (cont d.

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

5. Control Statements

CGS 3066: Spring 2015 JavaScript Reference

CS302: Self Check Quiz 2

Java Loop Control. Programming languages provide various control structures that allow for more complicated execution paths.

Java Programming Basics. COMP 401, Spring 2017 Lecture 2

Chapter 8 Statement-Level Control Structures

Methods. Contents Anatomy of a Method How to design a Method Static methods Additional Reading. Anatomy of a Method

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

Chapter 2 Author Notes

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

LAMBDA EXPRESSIONS. Summer 2018

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

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

Object-oriented programming in...

STUDENT LESSON A12 Iterations

Basic Java Syntax. COMP 401, Fall 2015 Lecture 2 1/13/2014

UNIT I Programming Language Syntax and semantics. Kainjan Sanghavi

EECS168 Exam 3 Review

Programming Lecture 4

Basic Java Syntax, cont d. COMP 401, Fall 2015 Lecture 3 1/15/2015

NAMESPACES IN C++ You can refer the Programming with ANSI C++ by Bhushan Trivedi for Understanding Namespaces Better(Chapter 14)

CS 320: Concepts of Programming Languages

CSI33 Data Structures

CS 320: Concepts of Programming Languages

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

Relational Operators and if. Class 10

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

Understanding main() function Input/Output Streams

C++ for Python Programmers

Smalltalk: developed at Xerox Palo Alto Research Center by the Learning Research Group in the 1970 s (Smalltalk-72, Smalltalk-76, Smalltalk-80)

Classes. Logical method to organise data and functions in a same structure. Also known as abstract data type (ADT).

Introduction to Bioinformatics

Object-Oriented Programming in Java

Decision Making in C

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

Chapter 8. Statement-Level Control Structures

Java Programming Basics. COMP 401, Fall 2017 Lecture 2

Overview of the Ruby Language. By Ron Haley

Chapter 4 Defining Classes I

ARG! Language Reference Manual

5. Defining Classes and Methods

CISC-124. Casting. // this would fail because we can t assign a double value to an int // variable

class objects instances Fields Constructors Methods static

Lab # 2. For today s lab:

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

Operators. Java operators are classified into three categories:

Data Structure and Programming Languages

CS 231 Data Structures and Algorithms, Fall 2016

2.8. Decision Making: Equality and Relational Operators

Final Exam. Programming Assignment 3. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu. Readings

AN OVERVIEW OF C, PART 3. CSE 130: Introduction to Programming in C Stony Brook University

Programming Lecture 4

Defining Classes and Methods. Objectives. Objectives 6/27/2014. Chapter 5

CSE 341: Programming Languages

C Syntax Out: 15 September, 1995

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

Unit 2: Java in the small

Programming in C. What is C?... What is C?

Programming in C UVic SEng 265

Full file at

Lesson 6A Loops. By John B. Owen All rights reserved 2011, revised 2014

BIT Java Programming. Sem 1 Session 2011/12. Chapter 2 JAVA. basic

Transcription:

CS 112 Introduction to Computing II Wayne Snyder Department Boston University Today: Java expressions and operators concluded Java Statements: Conditionals: if/then, if/then/else Loops: while, for Next Time: Arrays, methods, program structure, fields vs local variables, public vs private, the keyword static. Reading assignments are posted on the web site! Java Statements Recall: The core of a Java program is a main method consisting of a sequence of statements followed by semicolons; each statement is executed in sequence, and each has some effect on the state of the computer: 2 1

Flow of control: conditionals if/then, if/then/else Conditional statements alter the flow of execution by testing some boolean condition and branching one way or the other; you just have to translate what you already know from Python into Java syntax: Simple if/then statement: 3 Flow of control: conditionals if, if/then, if/then/else Conditional statements alter the flow of execution by testing some boolean condition and branching one way or the other; you just have to translate what you already know from Python into Java syntax: if/then/else statement: 4 2

Flow of control: conditionals if, if/then, if/then/else Conditional statements alter the flow of execution by testing some boolean condition and branching one way or the other; you just have to translate what you already know from Python into Java syntax: Compound if/then/else statement: 5 Flow of control: conditionals if, if/then, if/then/else Compound statements Any single statement can be replaced by a compound statement, consisting of a sequence of statements delimited by curly braces, instead of indentation, to indicate that all these statements should be executed in sequence: 6 3

Flow of control: compound statements Compound statements Any single statement can be replaced by a compound statement, consisting of a sequence of statements delimited by curly braces, instead of indentation, to indicate that all these statements should be executed in sequence: 7 Flow of control: compound statements Programming Tip: I strongly recommend that you ALWAYS use parentheses, even to enclose a single statement: Don t: Do: 8 4

Flow of control: compound statements Why? Because if you decide to add code later, you will avoid a nasty bug (especially if you are used to Python s grouping by indentation): What you want: Not what you want! Will be executed no matter what! 9 Flow of control: loops while and for while loop: for loop: 10 5

Flow of control: loops while and for The for loop syntax is very different from Initialization: executed once before loop body Condition: test at beginning of loop (exactly like while loop) Update: executed once at end of loop body Similar to: 11 Flow of control: break and continue More complex examples of for loops: Multiple declarations and multiple updates: Same as: 12 6

Flow of control: break and continue break and continue work exactly the same as in Python! break jumps out of the loop entirely; continue jumps to end of loop and continues with next iteration: 13 Flow of control: loops: break, continue Note carefully: when executing a continue in a for loop, the next statement to be executed is the update: 14 7

Local Variables and Scope The scope of a variable (= where you can refer to it) is from the point of the declaration to the next right curly brace. You may not refer to a variable outside its scope, and you may not re-declare a variable inside its scope. Not Legal! Scope of x Legal! Not Legal! 15 Local Variables and Scope The scope of a variable (= where you can refer to it) is from the point of the declaration to the next right curly brace. You may not refer to a variable outside its scope, and you may not re-declare a variable inside its scope. Scope of x 16 8

Local Variables and Scope The scope of a variable (= where you can refer to it) is from the point of the declaration to the next right curly brace. You may not refer to a variable outside its scope, and you may not re-declare a variable inside its scope. Scope of j 17 Local Variables and Scope The scope of a variable (= where you can refer to it) is from the point of the declaration to the next right curly brace. You may not refer to a variable outside its scope, and you may not re-declare a variable inside its scope. Scope of p 18 9

Local Variables and Scope The scope of a variable (= where you can refer to it) is from the point of the declaration to the next right curly brace. Note carefully how this works in a for loop the scope of the initialization variable includes the condition, the update, and the whole loop body: Scope of i 19 Flow of control: loops: break, continue This leads us to a useful idiom: when you need to refer to the initialization after the for loop, declare it before the loop: 20 10