CompSci 100E 2.1 Java Basics - Expressions  Literals  A literal is a constant value also called a self-defining term  Possibilities: o Object: null,

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

Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Chapter 2: Using Data.
©2004 Brooks/Cole Chapter 2 Variables, Values and Operations.
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
Aalborg Media Lab 21-Jun-15 Software Design Lecture 2 “ Data and Expressions”
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Primitive Types Java supports two kinds of types of values – objects, and – values of primitive data types variables store – either references to objects.
Day 4 Objectives Constructors Wrapper Classes Operators Java Control Statements Practice the language.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
Primitive Data Types and Operations Identifiers, Variables, and Constants Primitive Data Types Byte, short, int, long, float, double, char, boolean Casting.
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
General Features of Java Programming Language Variables and Data Types Operators Expressions Control Flow Statements.
Chapter 3: Data Types and Operators JavaScript - Introductory.
Lecture 4 C Program Control Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
CSCI 1100/1202 January 28, The switch Statement The switch statement provides another means to decide which statement to execute next The switch.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Flow Control (Switch, do-while, break) Outline 4.7The.
Sahar Mosleh California State University San MarcosPage 1 A for loop can contain multiple initialization actions separated with commas Caution must be.
CIS 260: App Dev I. 2 Programs and Programming n Program  A sequence of steps designed to accomplish a task n Program design  A detailed _____ for implementing.
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.
BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA TYPES AND OPERATIONS.
Additional Control Structures. Chapter 9 Topics Switch Statement for Multi-way Branching Do-While Statement for Looping For Statement for Looping Using.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Chapter 7 Additional Control Structures. Chapter 7 Topics l Switch Statement for Multi-Way Branching l Do-While Statement for Looping l For Statement.
Chapter 05 (Part III) Control Statements: Part II.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 5: Introduction to C: More Control Flow.
Control Statements in C 1.Decision making statements 2.Looping statements 3.Branching statements
Chapter 2 Variables.
Flow of Control Chapter 3. Outline Branching Statements Java Loop Statements Programming with Loops The Type boolean.
Sections © Copyright by Pearson Education, Inc. All Rights Reserved.
Control Flow Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall 2014.
Java Programming, Second Edition Chapter Two Using Data Within a Program.
CompSci Primitive Types  Primitive Types (base types)  Built-in data types; native to most hardware  Note: not objects (will use mostly first.
CSM-Java Programming-I Spring,2005 Fundamental Data Types Lesson - 2.
1 Programming in C++ Dale/Weems/Headington Chapter 9 Additional Control Structures (Switch, Do..While, For statements)
Iteration & Loop Statements 1 Iteration or Loop Statements Dept. of Computer Engineering Faculty of Engineering, Kasetsart University Bangkok, Thailand.
2: Basics Basics Programming C# © 2003 DevelopMentor, Inc. 12/1/2003.
COMP Loop Statements Yi Hong May 21, 2015.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
CompSci 100E JB1.1 Java Basics (ala Goodrich & Tamassia)  Everything is in a class  A minimal program: public class Hello { public static void main(String[]
Information and Computer Sciences University of Hawaii, Manoa
Chapter 2 Variables.
Programming in Java Sachin Malhotra, Chairperson, PGDM-IT, IMS Ghaziabad Saurabh Chaudhary, Dean, Academics, IMS Ghaziabad.
Decisions Chapter 4.
2.5 Another Java Application: Adding Integers
Control Statements: Part 2
Multiple variables can be created in one declaration
Expressions and Control Flow in JavaScript
Java Programming: From Problem Analysis to Program Design, 4e
OPERATORS (1) CSC 111.
MSIS 655 Advanced Business Applications Programming
OPERATORS (2) CSC 111.
Chapter 8 JavaScript: Control Statements, Part 2
Chapter 2: Basic Elements of Java
Chapter 7 Additional Control Structures
Chapter 2 Variables.
CSC215 Lecture Control Flow.
Structured Program Development in C
Recap Week 2 and 3.
Expressions and Assignment
elementary programming
Chapter 2 Programming Basics.
Fundamental OOP Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
In this class, we will cover:
Chapter 2 Variables.
Chap 7. Advanced Control Statements in Java
ENERGY 211 / CME 211 Lecture 5 October 1, 2008.
CSC215 Lecture Control Flow.
Problem 1 Given n, calculate 2n
Presentation transcript:

CompSci 100E 2.1 Java Basics - Expressions  Literals  A literal is a constant value also called a self-defining term  Possibilities: o Object: null, the only object literal available o Boolean: true or false o Integer: e.g., 127, -13, 42, or 0 create 32-bit integers  For 64-bit long append L or l, e.g., 17L o Floating Point: or 0.0 or 2.1E16 for 64-bit doubles  For 32-bit float append F or f, e.g., 2.56F or 0.5e-12f o Character: e.g., ’A’, ’Z’, ’w’, ’$’, ’%’ for 16 bit Unicode  control: ’\n’, ’\b’, ’\f’, ’\t’, ’\r’  escape : ’\’’, ’\\’, ’\”’ o Strings: e.g., ”How are things?” or ”” (null string) or null  Use mostly same control and escape characters as char

CompSci 100E 2.2 Java Basics - Expressions  Operators  Arithmetic o +, -, *, /, % ( remainder or mod )  Increment/Decrement o e.g., k++, k--, ++k, --k  Logical (results in boolean value) o =, > o Used only for numbers except == and != o For boolean only: !, &&, ||  String Concatenation o ”I’m ” ” years old and live in ” + city  Assignment o variable = expression o variable op= expression o ( shorthand for: variable = variable op expression )

CompSci 100E 2.3 Java Basics - Expressions  Operator Precedence  Determines order of operation  See table in text  For arithmetic, matches grammar school learning o multiplication and division before addition and subtraction o what is the value of / 9.0 * 27.0 ? o (what is the value for the integer version?)  Parentheses override precedence rules (and don’t do harm when not needed)  For equal precedence (e.g., * and / ) work strictly left to right except for assignment and prefix operations which work right to left  Precedence rules same as for C and C++

CompSci 100E 2.4 Java Basics - Expressions  Casting  Allows us to change the type of the value of an expression  (Type change must be reasonable and supported.)  Simple example: double x = 5.5, y = ; int k = (int) x; int m = (int) y; double z = (double) k; // what is in x, y, z, k, m ?  Implicit Casting  When an int expression is assigned to a double, casting is automatic (no information is lost). o (double cast at end of previous example not needed)  When double is on one side of an operator and int at other, int is automatically cast to a double before op is used. 5 / 9 * (68 – 32) vs. 5.0 / 9 * (68 – 32)

CompSci 100E 2.5 Java Basics - Expressions  Autoboxing/Unboxing  Since Java 5.0, there is automatic casting between primitive types and their related Object types (also called wrapper classes ).  Simple examples: Double d = 2.9; used to require: Double d = new Double(2.9); and double x = d; used to require double x = d.doubleValue();

CompSci 100E 2.6 Java Basics – Control of Flow  If Statement  if (boolean_exp) { what_to_do_if_true }  if (boolean_exp) { what_to_do_if_true } else { what_to_do_if_false }  if (1 st _boolean_exp) { what_to_do_if_1 st _true } else if (2 nd _boolean_exp){ what_to_do_if_2nd_true } else { what_to_do_if_all_false }

CompSci 100E 2.7 Java Basics – Control Flow  Switch Statement  switch (int_type_exp) { case CONST1: action_for_CONST1; break; case CONST1: action_for_CONST1; break; case CONST2: action_for_CONST2; break; case CONST3: action_for_CONST3; break;... default: action_for_no_match; break; }

CompSci 100E 2.8 Java Basics – Control Flow  Switch Statement Example  switch (stars) { case 4: message = ”truly exceptional”; break; case 3: message = ”quite good”; break; case 2: message = ”fair”; break; case 1: case 0: message = ”forget it”; break; default: message = ”no info found”; break; }

CompSci 100E 2.9 Java Basics – Loops  While Loops  Syntax initialize while (boolean_exp) { work_to_be_done update }  Example int counter = 10; while (counter > 0) { System.out.println(counter); counter--; } System.out.println(”Blast Off!”);  What is the output?  What if we exchange order of two statements in loop?

CompSci 100E 2.10 Java Basics – Loops  For Loops  Syntax for (intialization; boolean_exp; update) { work_to_be_done }  Example for (int counter = 10; counter > 0; counter--) { System.out.println(counter); } System.out.println(”Blast Off!”);  What is the output?  When is update performed?  What is value of counter after loop?

CompSci 100E 2.11 Java Basics – Loops  Do-While Loops  Syntax initialize do { work_to_be_done update } while (boolean_exp); o NOTE REQUIRED SEMICOLON!!!  Example int counter = 10; do { System.out.println(counter); counter-- ; } while (counter > 0); System.out.println(”Blast Off!”);

CompSci 100E 2.12 Java Basics – Loops  Which Kind of Loop Do I Use?  While Loop o Don’t know how often it’s going be o Update can be anywhere in the loop body  For Loop o Know how often in advance o All information controlling loop together, in front  Do-While Loop o Least popular o Often used with data input  What is the minimum number of times each of these loop? o while? o for? o do-while?

CompSci 100E 2.13 Java Basics – Control Flow  Returning from a Method  Executing a return statements means you exit from the the method. Subsequent statements are ignored!  void Methods o Implicit return at end of body  Can make it explicit o Can have other return statements as logic dictates  Functions (non-void Methods) o Require return as last statement (with argument of correct type) o Can have other return statements as logic dictates

CompSci 100E 2.14 Java Basics – Control Flow  Break Statement  Use to exit from loop or switch o One level only! o With nested loops, only leave loop immediately surrounding break  Continue Statement  Use to go to the end of a loop, ignoring remaining statements o Loop continues with next iteration (if needed) o One level only! o With nested loops, only got to end of loop immediately surrounding continue