Sahar Mosleh California State University San MarcosPage 1 A for loop can contain multiple initialization actions separated with commas Caution must be.

Slides:



Advertisements
Similar presentations
© 2007 Lawrenceville Press Slide 1 Assignment Statement An assignment statement gives a value to a variable. Assignment can take several forms: x = 5;
Advertisements

Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
©2004 Brooks/Cole Chapter 2 Variables, Values and Operations.
CS102--Object Oriented Programming
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 3: Numeric Data *Variables *Numeric data.
Loops – While, Do, For Repetition Statements Introduction to Arrays
Slides prepared by Rose Williams, Binghamton University Chapter 1 Getting Started 1.2 Expressions and Assignment Statement.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
Loops Repetition Statements. Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional.
CSci 142 Data and Expressions. 2  Topics  Strings  Primitive data types  Using variables and constants  Expressions and operator precedence  Data.
Primitive Types, Strings, and Console I/O Chapter 2.1 Variables and Values Declaration of Variables Primitive Types Assignment Statement Arithmetic Operators.
Java Data Types. Primitive Data Types Java has 8 primitive data types: – char: used to store a single character eg. G – boolean: used to store true or.
Basic Notions Review what is a variable? value? address? memory location? what is an identifier? variable name? keyword? what is a legal identifier? what.
LESSON 6 – Arithmetic Operators
Loops and Iteration for Statements, while Statements and do-while Statements.
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.
CHAPTER 4: CONTROL STRUCTURES - SEQUENCING 10/14/2014 PROBLEM SOLVING & ALGORITHM (DCT 1123)
CPS120: Introduction to Computer Science Operations Lecture 9.
CompSci 100E 2.1 Java Basics - Expressions  Literals  A literal is a constant value also called a self-defining term  Possibilities: o Object: null,
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 2- 1 June 3, June 3, 2016June 3, 2016June 3, 2016 Azusa, CA.
November 1, 2015ICS102: Expressions & Assignment 1 Expressions and Assignment.
CHAPTER 4 GC 101 Data types. DATA TYPES  For all data, assign a name (identifier) and a data type  Data type tells compiler:  How much memory to allocate.
VARIABLES, CONSTANTS, OPERATORS ANS EXPRESSION
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
Control Structures - Selections - Repetitions/iterations (part 2) 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
Mathematical Calculations in Java Mrs. C. Furman.
COMP Primitive and Class Types Yi Hong May 14, 2015.
Programming in Java (COP 2250) Lecture 4 Chengyong Yang Fall, 2005.
Sahar Mosleh California State University San MarcosPage 1 Data Types and Operators.
Arithmetic Expressions Addition (+) Subtraction (-) Multiplication (*) Division (/) –Integer –Real Number Mod Operator (%) Same as regular Depends on the.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
CSI 3125, Preliminaries, page 1 Data Type, Variables.
Data Types, Variables, and Arithmetic Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006 by.
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
CMSC 202 Java Primer 1. July 24, 2007 Copyright © 2008 Pearson Addison-Wesley 2 A Sample Java Application.
LESSON 5 – Assignment Statements JAVA PROGRAMMING.
1 Primitive Types n Four integer types:  byte  short  int (most common)  long n Two floating-point types:  float  double (most common) n One character.
Java-02 Basic Concepts Review concepts and examine how java handles them.
CSCI 1100/1202 January 18, Arithmetic Expressions An expression is a combination of operators and operands Arithmetic expressions compute numeric.
ICS102 Lecture 1 : Expressions and Assignment King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer.
Chapter 2: Data and Expressions. Variable Declaration In Java when you declare a variable, you must also declare the type of information it will hold.
1 2. Program Construction in Java. 01 Java basics.
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.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. 4 Simple Flow of Control.
Java Programming: Guided Learning with Early Objects Chapter 1 Basic Elements of Java.
Information and Computer Sciences University of Hawaii, Manoa
Lecture 4b Repeating With Loops
Chapter 2 Variables.
Data Types and Expressions
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
Chapter 3 Loops Section 3.3 Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.
Multiple variables can be created in one declaration
Assignment and Arithmetic expressions
Type Conversion, Constants, and the String Object
Lecture 3 Expressions Richard Gesick.
Basic Notions Review what is a variable? value? address? memory location? what is an identifier? variable name? keyword? what is legal identifier? what.
Chapter 2 Edited by JJ Shepherd
Arithmetic Expressions & Data Conversions
Chapter 2 Variables.
Expressions and Assignment
Chapter 2 Programming Basics.
Primitive Types and Expressions
Chapter 2 Variables.
Chapter 3 Flow of Control Loops in Java.
Looping and Repetition
Arithmetic Expressions & Data Conversions
Presentation transcript:

Sahar Mosleh California State University San MarcosPage 1 A for loop can contain multiple initialization actions separated with commas Caution must be used when combining a declaration with multiple actions It is illegal to combine multiple type declarations with multiple actions, for example To avoid possible problems, it is best to declare all variables outside the for statement A for loop can contain multiple update actions, separated with commas, also It is even possible to eliminate the loop body in this way However, a for loop can contain only one Boolean expression to test for ending the loop The Comma in for Statements

Sahar Mosleh California State University San MarcosPage 2 Infinite Loops A while, do-while, or for loop should be designed so that the value tested in the Boolean expression is changed in a way that eventually makes it false, and terminates the loop If the Boolean expression remains true, then the loop will run forever, resulting in an infinite loop Loops that check for equality or inequality (== or !=) are especially prone to this error and should be avoided if possible

Sahar Mosleh California State University San MarcosPage 3 Loop Bugs The two most common kinds of loop errors are unintended infinite loops and off-by-one errors An off-by-one error is when a loop repeats the loop body one too many or one too few times This usually results from a carelessly designed Boolean test expression Use of == in the controlling Boolean expression can lead to an infinite loop or an off-by-one error This sort of testing works only for characters and integers, and should never be used for floating-point

Sahar Mosleh California State University San MarcosPage 4 Example: Number = 2; While (number !=12) { system.out.println(number); //Ok since printing even numbers and number riches 12 Number = number +2; } Number = 1; While (number !=12) { system.out.println(number); //Not Ok since printing odd numbers and number never riches 12 Number = number +2; } Number = 2; While (number <12) { system.out.println(number); //Ok for odd and even numbers Number = number +2; }

Sahar Mosleh California State University San MarcosPage 5 Local Variables A variable declared within a method definition is called a local variable All variables declared in the main method are local variables All method parameters are local variables If two methods each have a local variable of the same name, they are still two entirely different variables

Sahar Mosleh California State University San MarcosPage 6 Blocks A block is another name for a compound statement, that is, a set of Java statements enclosed in braces,{}. A variable declared within a block is local to that block, and cannot be used outside the block Once a variable has been declared within a block, its name cannot be used for anything else within the same method definition

Sahar Mosleh California State University San MarcosPage 7 Declaring Variables in a for Statement You can declare one or more variables within the initialization portion of a for statement A variable so declared will be local to the for loop, and cannot be used outside of the loop If you need to use such a variable outside of a loop, then declare it outside the loop

Sahar Mosleh California State University San MarcosPage 8 Assignment Compatibility In general, the value of one type cannot be stored in a variable of another type int intVariable = 2.99; //Illegal The above example results in a type mismatch because a double value cannot be stored in an int variable However, there are exceptions to this double doubleVariable = 2; For example, an int value can be stored in a double type

Sahar Mosleh California State University San MarcosPage 9 Assignment Compatibility More generally, a value of any type in the following list can be assigned to a variable of any type that appears to the right of it byte  short  int  long  float  double Char Note that as your move down the list from left to right, the range of allowed values for the types becomes larger An explicit type cast is required to assign a value of one type to a variable whose type appears to the left of it on the above list (e.g., double to int) Note that in Java an int cannot be assigned to a variable of type boolean, nor can a boolean be assigned to a variable of type int

Sahar Mosleh California State University San MarcosPage 10 Arithmetic Operators and Expressions As in most languages, expressions can be formed in Java using variables, constants, and arithmetic operators These operators are + (addition), - (subtraction), * (multiplication), / (division), and % (modulo, remainder) An expression can be used anyplace it is legal to use a value of the type produced by the expression

Sahar Mosleh California State University San MarcosPage 11 Arithmetic Operators and Expressions If an arithmetic operator is combined with int operands, then the resulting type is int If an arithmetic operator is combined with one or two double operands, then the resulting type is double If different types are combined in an expression, then the resulting type is the right-most type on the following list that is found within the expression byte  short  int  long  float  double Char

Sahar Mosleh California State University San MarcosPage 12 Integer and Floating-Point Division When one or both operands are a floating-point type, division results in a floating-point type 15.0/2 evaluates to 7.5 When both operands are integer types, division results in an integer type Any fractional part is discarded The number is not rounded 15/2 evaluates to 7 Be careful to make at least one of the operands a floating-point type if the fractional portion is needed

Sahar Mosleh California State University San MarcosPage 13 The % Operator The % operator is used with operands of type int to recover the information lost after performing integer division 15/2 evaluates to the quotient 7 15%2 evaluates to the remainder 1 The % operator can be used to count by 2's, 3's, or any other number To count by twos, perform the operation number % 2, and when the result is 0, number is even

Sahar Mosleh California State University San MarcosPage 14 Type Casting A type cast takes a value of one type and produces a value of another type with an "equivalent" value If n and m are integers to be divided, and the fractional portion of the result must be preserved, at least one of the two must be type cast to a floating- point type before the division operation is performed double ans = n / (double)m; Note that the desired type is placed inside parentheses immediately in front of the variable to be cast Note also that the type and value of the variable to be cast does not change

Sahar Mosleh California State University San MarcosPage 15 More Details About Type Casting When type casting from a floating-point to an integer type, the number is truncated, not rounded (int)2.9 evaluates to 2, not 3 When the value of an integer type is assigned to a variable of a floating-point type, Java performs an automatic type cast called a type coercion double d = 5; In contrast, it is illegal to place a double value into an int variable without an explicit type cast int i = 5.5; // Illegal int i = (int)5.5 // Correct