Controlling Program Flow. Data Types and Variable Declarations Controlling Program Flow.

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

5/17/ Programming Constructs... There are several types of programming constructs in JAVA. - If-else construct or ternary operator - while - do-while.
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
Intermediate Java Sakir YUCEL MISM/MSIT Carnegie Mellon University Program Control Slides adapted from Steven Roehrig.
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
Introduction to Computers and Programming Lecture 5 Boolean type; if statement Professor: Evan Korth New York University.
CS 117 Spring 2002 Review for Exam 2 March 6, 2002 open book, 1 page of notes.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
1 MATERI PENDUKUNG OPERATOR Matakuliah: M0074/PROGRAMMING II Tahun: 2005 Versi: 1/0.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
The switch Statement, DecimalFormat, and Introduction to Looping
UNIT II Decision Making And Branching Decision Making And Looping
Day 4 Objectives Constructors Wrapper Classes Operators Java Control Statements Practice the language.
 Value, Variable and Data Type  Type Conversion  Arithmetic Expression Evaluation  Scope of variable.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
Exercises A-Declare a variable of double type with initial value=0.0; B- Declare a constant of String type with initial value=“Good” C- Declare a variable.
Operators Using Java operators An operator takes one or more arguments and produces a new value. All operators produce a value from their.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
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.
S ystem P rogrammers' A ssociation for R esearching C omputer S ystems 3. Operators SPARCS JAVA Study.
Flow of Control Part 1: Selection
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
Algorithm Programming Bar-Ilan University תשס"ח by Moshe Fresko.
Mathematical Calculations in Java Mrs. G. Chapman.
Session Three Review & Conditional Control Flow. Java File Hierarchy Projects Packages Classes Methods.
CompSci 100E 2.1 Java Basics - Expressions  Literals  A literal is a constant value also called a self-defining term  Possibilities: o Object: null,
Controlling Execution Dong Shao, Nanjing Unviersity.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
Introduction to Java Java Translation Program Structure
VARIABLES, CONSTANTS, OPERATORS ANS EXPRESSION
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
3: Controlling Program Flow Using Java operators Mathematical operators Relational operators Logical operators –Primitive type: ALL (the same with C) –String:
Lesson 6 Selection Structures. Example program //This will convert a numeric grade into a letter grade import TerminalIO.KeyboardReader; public class.
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
Operators in JAVA. Operator An operator is a symbol that operates on one or more arguments to produce a result. Java provides a rich set of operators.
1.2 Built-in Types of Data Introduction to Programming in Java: An Interdisciplinary Approach · Robert Sedgewick and Kevin Wayne · Copyright © 2008 · December.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 8 Java Fundamentals Control Structures Fri.
2: Basics Basics Programming C# © 2003 DevelopMentor, Inc. 12/1/2003.
Chapter 1 Java Programming Review. Introduction Java is platform-independent, meaning that you can write a program once and run it anywhere. Java programs.
Programming Principles Operators and Expressions.
Dr. Sajib Datta Jan 21,  Declare a variable ◦ int height; [note that no value is still assigned]  Assign a variable a value ◦ height =
 Array ◦ Single & Multi-dimensional  Java Operators ◦ Assignment ◦ Arithmetic ◦ Relational ◦ Logical ◦ Bitwise & other.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
Chapter 7 Control Structures. Java has very flexible three looping mechanisms. You can use one of the following three loops:  while Loop  do...while.
Object-Oriented Programming and Problem Solving Dr. Ramzi Saifan Introduction and basics of Java Slides adapted from Steven Roehrig.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
Java Programming Fifth Edition
Java Language Basics.
CIS3931 – Intro to JAVA Lecture Note Set 2 17-May-05.
Java Primer 1: Types, Classes and Operators
2.5 Another Java Application: Adding Integers
Object Oriented Programming
Multiple variables can be created in one declaration
Expressions and Control Flow in JavaScript
JavaScript: Control Statements.
Operators and Expressions
Starting JavaProgramming
An Introduction to Java – Part I, language basics
Java Tokens & Data types
PHP.
Expressions and Assignment
Chapter 2 Programming Basics.
Fundamental OOP Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
In this class, we will cover:
Names of variables, functions, classes
Decision making and control functions
Problem 1 Given n, calculate 2n
Presentation transcript:

Controlling Program Flow

Data Types and Variable Declarations Controlling Program Flow

Everything is an Object You manipulate objects with references : television\remote control Reference can stand on its own : String s A safer practice, then, is always to initialize a reference when you create it: String s = "asdf";

Data Types Object type ready-made types your own types : class

Special case: primitive types For these types Java falls back on the approach taken by C and C++. That is, instead of creating the variable using new, an “ automatic ” variable is created that is not a reference. The variable holds the value, and it ’ s placed on the stack so it ’ s much more efficient.

More All numeric types are signed, so don ’ t go looking for unsigned types. The size of the boolean type is not explicitly defined; it is only specified to be able to take the literal values true or false.

“ wrapper ” classes if you want to make a nonprimitive object on the heap to represent that primitive type, you use the associated wrapper : char c = ‘ x ’ ; ( primitive ) Character C = new Character('x'); ( nonprimiteve )

Using Java operators

Precedence Operator precedence defines how an expression evaluates when several operators are present : The easiest one to remember is that multiplication and division happen before addition and subtraction.

Assignment “ = ” Assignment of primitives is quite straightforward. when you assign primitives you copy the contents from one place to another. A=B

Assignment “ = ” assign objects When you assign “ from one object to another ” you ’ re actually copying a reference from one place to another. Assignment.java

This phenomenon is often called aliasing. If you don ’ t want aliasing to occur in this case, You could forgot the assignment and say: n1.i = n2.i; It goes against good object-oriented design principles.

Aliasing during method calls Aliasing will also occur when you pass an object into a method: PassObject.java

In many programming languages, the method f( ) would appear to be making a copy of its argument Letter y inside the scope of the method. But once again a reference is being passed so the line y.c = 'z'; is actually changing the object outside of f( ).

Mathematical operators The basic mathematical operators are the same as the ones available in most programming languages: addition (+), subtraction (-), division (/), multiplication (*) and modulus (%, which produces the remainder from integer division). Integer division truncates, rather than rounds, the result.

Relational operators 、 = 、 == 、 != Equivalence (==) and nonequivalence works with all built-in data types, but the other comparisons won ’ t work with type boolean.

Testing object equivalence The relational operators == and != also work with all objects, but their meaning often confuses the first-time Java programmer : public class Equivalence { public static void main(String[] args) { Integer n1 = new Integer(47); Integer n2 = new Integer(47); System.out.println(n1 == n2); System.out.println(n1 != n2); }}

the output is actually false and then true. Naturally, this surprises people at first. What if you want to compare the actual contents of an object for equivalence? You must use the special method equals( ) that exists for all objects (not primitives, which work fine with == and !=. More

public class EqualsMethod { public static void main(String[] args) { Integer n1 = new Integer(47); Integer n2 = new Integer(47); System.out.println(n1.equals(n2)); } The result will be true, as you would expect.

But it ’ s not as simple as that. If you create your own class, like this: class Value { int i;} public class EqualsMethod2 { public static void main(String[] args) { Value v1 = new Value(); Value v2 = new Value(); v1.i = v2.i = 100; System.out.println(v1.equals(v2)); }}

the result is false ! This is because the default behavior of equals( ) is to compare references. So unless you override equals( ) in your new class you won ’ t get the desired behavior.

Logical operators Bitwise operators Shift operators Ternary if-else operator The comma operator

String operator + The + operator can be used to concatenate strings If an expression begins with a String, then all operands that follow must be Strings : int x = 0, y = 1, z = 2; String sString = "x, y, z "; System.out.println(sString + x + y + z);

Java uses all of C ’ s execution control statements. In Java, the keywords include if-else, while, do-while, for, and a selection statement called switch.

if-else The if-else statement is probably the most basic way to control program flow. The else is optional, so you can use if in two forms: if(Boolean-expression) statement or if(Boolean-expression) statement else statement

Iteration while(Boolean-expression) Statement do statement while(Boolean-expression); for(initialization; Boolean-expression; step) statement

public class WhileTest { public static void main(String[] args) { double r = 0; while(r < 0.99d) { r = Math.random(); System.out.println(r); }

This uses the static method random( ) in the Math library, which generates a double value between 0 and 1. (It includes 0, but not 1.) The conditional expression for the while says “ keep doing this loop until the number is 0.99 or greater. ” Each time you run this program you ’ ll get a different-sized list of numbers.