Lecture 3: Topics If-then-else Operator precedence While loops Static methods Recursion.

Slides:



Advertisements
Similar presentations
Classes & Objects INTRODUCTION : This chapter introduces classes ; explains data hiding, abstraction & encapsulation and shows how a class implements these.
Advertisements

CPSC 388 – Compiler Design and Construction
Programming Languages and Paradigms
Interfaces A Java interface is a collection
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
Java Basics M Taimoor Khan
Lecture 2 Basics of C#. Members of a Class A field is a variable of any type that is declared directly in a class. Fields are members of their containing.
Gary MarsdenSlide 1University of Cape Town Statements & Expressions Gary Marsden Semester 2 – 2000.
Chapter 5 Functions.
Overloading methods review When is the return statement required? What do the following method headers tell us? public static int max (int a, int b)
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
COMP 14 Introduction to Programming Miguel A. Otaduy May 25, 2004.
Lesson 6 Functions Also called Methods CS 1 Lesson 6 -- John Cole1.
Parameters, Arguments, Local Variables, and Scope CSC 1401: Introduction to Programming with Java Week 8 – Lecture 1 Wanda M. Kunkle.
CSC321: Programming Languages1 Programming Languages Tucker and Noonan Chapter 9: Functions 9.1 Basic Terminology 9.2 Function Call and Return 9.3 Parameters.
Lecture From Chapter 6 & /8/10 1 Method of Classes.
Day 4 Objectives Constructors Wrapper Classes Operators Java Control Statements Practice the language.
Methods Chapter 6. 2 Program Modules in Java What we call "functions" in C++ are called "methods" in Java Purpose Reuse code Modularize the program This.
METHODS Introduction to Systems Programming - COMP 1005, 1405 Instructor : Behnam Hajian
Expressions creating information. topics  operators: precedence, associativity, parentheses, overloading  operands: side-effects, coercion  arithmetic,
General Features of Java Programming Language Variables and Data Types Operators Expressions Control Flow Statements.
Week 6 - Wednesday.  What did we talk about last time?  Exam 1 post-mortem  Recursive running time.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 6 Functions.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
CPS120: Introduction to Computer Science Decision Making in Programs.
CPS120: Introduction to Computer Science Functions.
ECE122 Feb. 22, Any question on Vehicle sample code?
CPS120: Introduction to Computer Science Lecture 14 Functions.
CSE 425: Control Flow I Categories of Control Flow Constructs Sequencing –order of expressions and statements Selection –if, else, switch Iteration –loops.
Loops (cont.). Loop Statements  while statement  do statement  for statement while ( condition ) statement; do { statement list; } while ( condition.
Basic Java Syntax COMP 401, Spring 2014 Lecture 2 1/14/2014.
“Education is a Treasure that follows you everywhere.” – Chines Proverb Methods and Functions.
1 Brief Version of Starting Out with C++, 4th Brief Edition Chapter 6 Functions.
Lecture 71 CS110 Lecture 8 February 19, 2004 Announcements –hw3 due tonight –hw4 available, due Thursday February 26 –exam Tuesday March 2 Agenda –questions.
1 Chapter 6 Methods. 2 Motivation Find the sum of integers from 1 to 10, from 20 to 30, and from 35 to 45, respectively.
Java Nuts and Bolts Variables and Data Types Operators Expressions Control Flow Statements Arrays and Strings.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Brief Edition Chapter 6 Functions.
Building java programs, chapter 3 Parameters, Methods and Objects.
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
Lecture 2 Functions. Functions in C++ long factorial(int n) The return type is long. That means the function will return a long integer to the calling.
Overloading a constructor If a constructor is overloaded, then an if statement in the client is needed when creating the object. We have always declared.
Chapter 5 Methods 1. Motivations Method : groups statements that perform a function.  Level of abstraction (black box)  Code Reuse – no need to reinvent.
Methods main concepts – method call – object 's methods – actual parameters – method declaration – formal parameters – return value other concepts – method.
COP 2220 Computer Science I Topics –Breaking Problems Down –Functions –User-defined Functions –Calling Functions –Variable Scope Lecture 4.
Inside Class Methods Chapter 4. 4 What are variables? Variables store values within methods and may change value as the method processes data.
ECE122 Feb 10, Unary Operator An operator that takes only a single operand Plus: + Minus: – Cast: (type). E.g. (double)
CS314 – Section 5 Recitation 9
Information and Computer Sciences University of Hawaii, Manoa
Introduction to Programming
Methods Chapter 6.
Chapter 6: Methods CS1: Java Programming Colorado State University
Chapter 5 Functions DDC 2133 Programming II.
C-language Lecture By B.S.S.Tejesh, S.Neeraja Asst.Prof.
Chapter 6 Methods 1.
Lecture 4 D&D Chapter 5 Methods including scope and overloading Date.
Agenda Warmup Lesson 2.5 (Ascii, Method Overloading)
if-else-if Statements
Conditional Statements
Variables and Their scope
Group Status Project Status.
Languages and Compilers (SProg og Oversættere)
Method of Classes Chapter 7, page 155 Lecture /4/6.
Review of Previous Lesson
Standard Version of Starting Out with C++, 4th Edition
Corresponds with Chapter 5
Methods and Data Passing
Scope Rules.
Chapter 6: Methods CS1: Java Programming Colorado State University
Chapter 4 Test Review First day
Presentation transcript:

Lecture 3: Topics If-then-else Operator precedence While loops Static methods Recursion

Boolean expressions continued If I can get an outdoor court tomorrow and it’s not raining or I can get an indoor court, we can play tennis English can be ambiguous, but in programming, AND always has higher precedence than OR

If-then-else if (i > j) if (a > b) ____; else // which if does it match? ____; The “else” clause matches the most recent unmatched “if” in the same {…} block

What’s the difference? boolean x; if (x == true) ______ if (x) ______ if (x = true) ______

What’s the difference? Between x++ and ++x They are both short for x = x+1, but they have different meanings in y = x++ and y = ++x The first means y = x; x++; while the second means x++ y = x Potentially confusing, better to use the longer versions in my opinion

Mixing assignments in one line a = b += c = 5 assignment operators are evaluated right to left, but other binary operators with same precedence are evaluated left to right, such as: a – b + c Very confusing! Better to avoid mixing assignment statements in one line in my opinion OK to use a+=5 instead of a = a + 5, etc, but this is not necessary

Operator precedence postfix ++, - - unary +, -, prefix ++, - - (avoid) casting ! *,/,% binary +,-, >= ==, != & (avoid) ^ (avoid) | (avoid) && || =, +=, -=, *=, /=, %= (I avoid except =) Use (,) when needed AND ALSO FOR CLARITY!

While loop demo Extracting the letters from a string

Static methods In older programming languages these were called “procedures” (when they did not return a value) and “functions” (when they returned a value) Modifiers: static, public, private Return value: void, or a type The formal parameters (or parameters) are the ones listed in the method header The actual parameters (or arguments) are the ones provided when the method is called Together, the name of the method and its list of parameter types constitute the method signature

Why do we need methods? To organize programs into manageable chunks of code Abstraction, encapsulation Always think of how to simplify code by using methods appropriately

Local variables and their scope A variable declared inside a method is local to the method and cannot be accessed outside it A variable declared in a loop or a block {…} cannot be accessed outside it Example for (int i=0; i<10; i++){ … } System.out.println(i); // error, i is not accessible

Method overloading When two methods have the same name but different signatures Many examples in the Java libraries

The call stack “Stack” of info about the method call history, including space for local variables

Recursion (chapter 19!) When a method calls itself classic example: factorial but factorial can just as well be programmed with a loop recursion really useful when the method makes two recursive calls bad example: Fibonacci (easy to write recursive code but it is very inefficient)