Syntax of Variable Declarations. Variables Usages as Expressions. Self-assignment Statements. Assignment. Scoping and Naming Rules

Similar documents
CIS 110: Introduction to Computer Programming

CS111: PROGRAMMING LANGUAGE II

Programming with Java

Data and Variables. Data Types Expressions. String Concatenation Variables Declaration Assignment Shorthand operators. Operators Precedence

Building Java Programs. Chapter 2: Primitive Data and Definite Loops

Binghamton University. CS-211 Fall Syntax. What the Compiler needs to understand your program

CIS 110: Introduction to Computer Programming

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

Programming with Java

CIS 110 Introduction To Computer Programming. October 5th, 2011 Exam 1. Review problems

CIS 110: Introduction to Computer Programming

CS 106 Introduction to Computer Science I

Outline. CIS 110: Introduction to Computer Programming. What is Computer Science? What is computer programming? What is computer science?

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

CIS 110 Introduction To Computer Programming. February 29, 2012 Midterm

CS 335 Lecture 02 Java Programming

Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade;

1.00 Lecture 4. Promotion

CIS 110 Introduction To Computer Programming. October 5th, 2011 Exam 1. Answer key for review problems

ECE 122. Engineering Problem Solving with Java

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

CIS 110: Introduction to Computer Programming. Lecture 2 Decomposition and Static Methods ( 1.4)

COMP 110/L Lecture 10. Kyle Dewey

Arrays. Comp Sci 1570 Introduction to C++ Array basics. arrays. Arrays as parameters to functions. Sorting arrays. Random stuff

CIS 110: Introduction to Computer Programming

Outline. CIS 110: Introduction to Computer Programming. Any questions? My life story. A horrible incident. The awful truth

CIS 110: Introduction to Computer Programming

Outline. CIS 110: Introduction to Computer Programming. Announcements. For Loops. Redundancy in Patterns. A Solution with Our Current Tools

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. Java Basics

1.00 Lecture 6. Java Methods

Activity 6: Loops. Content Learning Objectives. Process Skill Goals

Lecture 3: C Programm

Building Java Programs

Building Java Programs Chapter 2

Midterms Save the Dates!

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

1 Epic Test Review 2 Epic Test Review 3 Epic Test Review 4. Epic Test Review 5 Epic Test Review 6 Epic Test Review 7 Epic Test Review 8

Control Structures in Java if-else and switch

CSCI 161 Introduction to Computer Science

Visual Programming. Lecture 3: Loops, Arrays. Mahmoud El-Gayyar

COMP 202 Java in one week

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

INITIALISING POINTER VARIABLES; DYNAMIC VARIABLES; OPERATIONS ON POINTERS

Controls Structure for Repetition

Building Java Programs Chapter 2. bug. Primitive Data and Definite Loops. Copyright (c) Pearson All rights reserved. Software Flaw.

Building Java Programs Chapter 2

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

Associate Professor Dr. Raed Ibraheem Hamed

CIS 110 Introduction to Computer Programming. February 29, 2012 Midterm

CS 106 Introduction to Computer Science I

Simple Java Programming Constructs 4

1007 Imperative Programming Part II

Array Basics: Outline. Creating and Accessing Arrays. Creating and Accessing Arrays. Arrays (Savitch, Chapter 7)

CS 106 Introduction to Computer Science I

CS 112 Introduction to Programming

Admin. CS 112 Introduction to Programming. Recap: Java Static Methods. Recap: Decomposition Example. Recap: Static Method Example

TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA

Java Programming Basics. COMP 401, Fall 2017 Lecture 2

Scientific Computing

Technical Questions. Q 1) What are the key features in C programming language?

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

Introduction to Programming Using Java (98-388)

C++ Support Classes (Data and Variables)

9 Working with the Java Class Library

COMP-202: Foundations of Programming. Lecture 2: Java basics and our first Java program! Jackie Cheung, Winter 2016

What the CPU Sees Basic Flow Control Conditional Flow Control Structured Flow Control Functions and Scope. C Flow Control.

COE318 Lecture Notes Week 4 (Sept 26, 2011)

Program Fundamentals

Outline. Object Oriented Programming. Course goals. Staff. Course resources. Assignments. Course organization Introduction Java overview Autumn 2003

CSE 142, Summer 2014

( &% class MyClass { }

CS1150 Principles of Computer Science Methods

Unit 5: More on Classes/Objects Notes

Use of scanf. scanf("%d", &number);

Syntax Errors; Static Semantics

Constants. Why Use Constants? main Method Arguments. CS256 Computer Science I Kevin Sahr, PhD. Lecture 25: Miscellaneous

CS313D: ADVANCED PROGRAMMING LANGUAGE

Repe$$on CSC 121 Fall 2015 Howard Rosenthal

Join the course group CS230-S18! Post questions, answer them if you know the answer!

1 Getting started with Processing

CA31-1K DIS. Pointers. TA: You Lu

CSI33 Data Structures

LAMBDA EXPRESSIONS. Summer 2018

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

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

Primitive data, expressions, and variables

CAAM 420 FALL 2012 Lecture 9. Mishael Owoyemi

AP Computer Science Unit 1. Writing Programs Using BlueJ

Odds and Ends. Think about doing research Programming grading. Assignment 3 due Tuesday

Notes from the Boards Set BN19 Page

COMP 250: Java Programming I. Carlos G. Oliver, Jérôme Waldispühl January 17-18, 2018 Slides adapted from M. Blanchette

Control Structures in Java if-else and switch

Building Java Programs

Le L c e t c ur u e e 2 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Variables Operators

A Fast Review of C Essentials Part II

Java Bytecode (binary file)

AP Computer Science Unit 1. Writing Programs Using BlueJ

Variables and literals

The for Loop. Lesson 11

School of Computer Science CPS109 Course Notes 5 Alexander Ferworn Updated Fall 15

Transcription:

Outline CIS 110: Introduction to Computer Programming 1. Variables 2. Our Mental Model of Computation Lecture 4 Variables and Our Mental Model of Computation ( 2.2) 9/19/2011 CIS 110 (11fa) - University of Pennsylvania 1 9/19/2011 CIS 110 (11fa) - University of Pennsylvania 2 String Concatenation The concatenation operation (+) glues two Strings together. hi + bye evaluates to hibye Java kindly allows us to concatenate a String and a non-string. val: + (40/3) evaluates to val: 13. Order of operations is very important! Example: val: + 20 3. Equivalent to ( val: + 20) 3. ( val: + 20) reduces to val: 20. val: 20 3 is not a valid operation! Variables (i.e., Storage Lockers) 9/19/2011 CIS 110 (11fa) - University of Pennsylvania 3 9/19/2011 CIS 110 (11fa) - University of Pennsylvania 4 Variables A variable is a named, typed location in memory that stores a value. Variable declarations are statements that create and initialize variables. public static void printvar() { int x = 22; System.out.println( The value of x: + x); We use variables by name as expressions. Variables as Storage Lockers 1. Declared a variable called x and stored 22 at that location. public static void printvar() { int x = 22; System.out.println( The value of x: + x); x 22 2. Retrieved the value 22 from the location named x 9/19/2011 CIS 110 (11fa) - University of Pennsylvania 5 9/19/2011 CIS 110 (11fa) - University of Pennsylvania 6 CIS 110 (11fa) - University of Pennsylvania 1

Syntax of Variable Declarations <type> <name> = <expression>; Declares a variable called <name> of type <type> and gives it the initial value <expression>, e.g., int age = 27; age 27 double height = 5.666667; char firstinitial = P ; height 5.666667 firstinitial P Alternatively: <type> <name>; Declares a variable called <name> of type <type> with no initial value. When possible, avoid this form because the value of your variable is initially unknown! Variables Usages as Expressions Variables can be used any where that an expression can be used, e.g., int x = 42; int y = x + 13 % 5; x 42 y 45 9/19/2011 CIS 110 (11fa) - University of Pennsylvania 7 9/19/2011 CIS 110 (11fa) - University of Pennsylvania Assignment We can reassign the value of variables <name> = <expression>; int x = 12; x 12 x = 39 / 2 + 1; x 20 x = x * 2; x 40 Self-assignment Statements Self-assignment is so common that we have short-hand to do it! x *= 2; is equivalent to x = x * 2; +=, -=, *=, /=, and %= are all available. Incrementing and decrementing by one is even more common! x++; is equivalent to x += 1; x-- is also available. ++x and --x also work! For our purposes, either is fine. See the book for the differences between ++x (pre-increment) and x++ (postincrement). 9/19/2011 CIS 110 (11fa) - University of Pennsylvania 9 9/19/2011 CIS 110 (11fa) - University of Pennsylvania 10 The Scope of a Variable The scope of a variable is the location in which it exists (and thus is valid to reference) A variable is in scope from its declaration to the curly braces ( { and ) that contain it. Scope of x public static void dostuff() { int ; System.out.println( in dostuff with x = + x); double d = 20.0; System.out.println( d before: + d); Scope of d d *= 2 + x; System.out.println( d after: + d); 9/19/2011 CIS 110 (11fa) - University of Pennsylvania 11 Scoping and Naming Rules You cannot declare a variable in a scope that has already been declared. Two variables with the same name in different scopes refer to different memory locations. public static void dostuff1() { int ; // different from x in dostuff2! public static void dostuff2() { int ; // different from x in dostuff1! 9/19/2011 CIS 110 (11fa) - University of Pennsylvania 12 CIS 110 (11fa) - University of Pennsylvania 2

Naming in Programming Analogy Peter-Michael Tacoma, WA Our Mental Model of Computation Peter Michael Alameda County, CA WE ARE NOT THE SAME PERSON (Sir) Peter Michael Knights Valley, CA 9/19/2011 CIS 110 (11fa) - University of Pennsylvania 13 9/19/2011 CIS 110 (11fa) - University of Pennsylvania 14 The Big Picture Learning about algorithmic thinking via computer programming! 1. Precision Mental Model of Computation 2. Decomposition Static Methods 3. Abstraction Mental Model of Computation Thinking like a computer Given a piece of code simulate in your head stepby-step how it executes. Example: evaluate expressions step-by-step 1. hi + 3 * 5 + bye + 12 / (double) 13 2. hi + 3 * 5 + bye + 12 / 13.0 3. hi + 15 + bye + 12 / 13.0 4. hi + 15 + bye + 0.9230769 5. hi15 + bye + 0.923769 6. hi15bye + 0.923769 7. hi15bye0.923769 9/19/2011 CIS 110 (11fa) - University of Pennsylvania 15 9/19/2011 CIS 110 (11fa) - University of Pennsylvania 16 Example: Executing Statements (1) Example: Executing Statements (2) main (line 10) 9/19/2011 CIS 110 (11fa) - University of Pennsylvania 17 9/19/2011 CIS 110 (11fa) - University of Pennsylvania 1 CIS 110 (11fa) - University of Pennsylvania 3

Example: Executing Statements (3) Example: Executing Statements (4) main (line 11) 9/19/2011 CIS 110 (11fa) - University of Pennsylvania 19 9/19/2011 CIS 110 (11fa) - University of Pennsylvania 20 Example: Executing Statements (5) Example: Executing Statements (6) foo (line 3) foo (line 4) x = 5 9/19/2011 CIS 110 (11fa) - University of Pennsylvania 21 9/19/2011 CIS 110 (11fa) - University of Pennsylvania 22 Example: Executing Statements (7) Example: Executing Statements () foo (line 5) x = 5 foo (line 6) x = 5 10 9/19/2011 CIS 110 (11fa) - University of Pennsylvania 23 9/19/2011 CIS 110 (11fa) - University of Pennsylvania 24 CIS 110 (11fa) - University of Pennsylvania 4

Example: Executing Statements (9) Example: Executing Statements (10) 5 int x = 10; foo (line 7) x = 5 10 9/19/2011 CIS 110 (11fa) - University of Pennsylvania 25 9/19/2011 CIS 110 (11fa) - University of Pennsylvania 26 Example: Executing Statements (11) Example: Executing Statements (11) main (line 13) main (line 14) 9/19/2011 CIS 110 (11fa) - University of Pennsylvania 27 9/19/2011 CIS 110 (11fa) - University of Pennsylvania 2 Develop that Mental Model! Our mental model must keep track of The call stack. The local variables in scope and their values. This is a skill that you should actively practice. Don t blindly throw code at the compiler! Take a step back, look at your code, and test your mental model out. Don t be afraid to write stuff down if you lose track of the details. 9/19/2011 CIS 110 (11fa) - University of Pennsylvania 29 CIS 110 (11fa) - University of Pennsylvania 5