SlideShare a Scribd company logo
Object Oriented
Programming Using Java
Dr. Akmal
1
Chapter 2
Introduction to
Java Applications;
Input/Output and Operators
Java™ How to Program, 10/e
© Copyright 1992-2015 by Pearson
Education, Inc. All Rights Reserved.
2
© Copyright 1992-2015 by Pearson Education, Inc. All Rights
Reserved.
3
2.3 Modifying Your First Java Program
• Class Welcome2, shown in Fig. 2.3, uses two statements to produce
the same output as that shown in Fig. 2.1.
• New and key features in each code listing are highlighted.
• System.out’s method print displays a string.
• Unlike println, print does not position the output cursor at the
beginning of the next line in the command window.
 The next character the program displays will appear immediately after the last
character that print displays.
© Copyright 1992-2015 by Pearson Education, Inc. All Rights
Reserved.
4
© Copyright 1992-2015 by Pearson Education, Inc. All Rights
Reserved.
5
2.3 Modifying Your First Java Program (Cont.)
• Newline characters indicate to System.out’s print and println
methods when to position the output cursor at the beginning of the next line in
the command window.
• Newline characters are whitespace characters.
• The backslash () is called an escape character.
 Indicates a “special character”
• Backslash is combined with the next character to form an escape sequence—
n represents the newline character.
• Complete list of escape sequences
http://docs.oracle.com/javase/specs/jls/se7/html/
jls-3.html#jls-3.10.6.
© Copyright 1992-2015 by Pearson Education, Inc. All Rights
Reserved.
6
© Copyright 1992-2015 by Pearson Education, Inc. All Rights
Reserved.
7
© Copyright 1992-2015 by Pearson Education, Inc. All Rights
Reserved.
8
2.4 Displaying Text with printf
• System.out.printf method
 f means “formatted”
 displays formatted data
• Multiple method arguments are placed in a comma-separated list.
• Calling a method is also referred to as invoking a method.
• Java allows large statements to be split over many lines.
 Cannot split a statement in the middle of an identifier or string.
• Method printf’s first argument is a format string
 May consist of fixed text and format specifiers.
 Fixed text is output as it would be by print or println.
 Each format specifier is a placeholder for a value and specifies the type of
data to output.
• Format specifiers begin with a percent sign (%) and are followed by a
character that represents the data type.
• Format specifier %s is a placeholder for a string.
© Copyright 1992-2015 by Pearson Education, Inc. All Rights
Reserved.
9
© Copyright 1992-2015 by Pearson Education, Inc. All Rights
Reserved.
10
2.5 Another Application: Adding Integers
• Integers
 Whole numbers, like –22, 7, 0 and 1024)
• Programs remember numbers and other data in the computer’s
memory and access that data through program elements called
variables.
• The program of Fig. 2.7 demonstrates these concepts.
© Copyright 1992-2015 by Pearson Education, Inc. All Rights
Reserved.
11
© Copyright 1992-2015 by Pearson Education, Inc. All Rights
Reserved.
12
© Copyright 1992-2015 by Pearson Education, Inc. All Rights
Reserved.
13
2.5.1 import Declarations
• Helps the compiler locate a class that is used in this program.
• Rich set of predefined classes that you can reuse rather than
“reinventing the wheel.”
• Classes are grouped into packages—named groups of related
classes—and are collectively referred to as the Java class library, or
the Java Application Programming Interface (Java API).
• You use import declarations to identify the predefined classes used
in a Java program.
© Copyright 1992-2015 by Pearson Education, Inc. All Rights
Reserved.
14
2.5.3 Declaring and Creating a Scanner to
Obtain User Input from the Keyboard
• Variable declaration statement
Scanner input = new Scanner( System.in );
 Specifies the name (input) and type (Scanner) of a variable that is used in this
program.
• Variable
 A location in the computer’s memory where a value can be stored for use later in a
program.
 Must be declared with a name and a type before they can be used.
 A variable’s name enables the program to access the value of the variable in memory.
 The name can be any valid identifier.
 A variable’s type specifies what kind of information is stored at that location in memory.
© Copyright 1992-2015 by Pearson Education, Inc. All Rights
Reserved.
15
2.5 Another Application: Adding Integers (Cont.)
• Scanner
 Enables a program to read data for use in a program.
 Data can come from many sources, such as the user at the keyboard or a file on disk.
 Before using a Scanner, you must create it and specify the source of the data.
• The equals sign (=) in a declaration indicates that the variable should be initialized
(i.e., prepared for use in the program) with the result of the expression to the right of
the equals sign.
• The new keyword creates an object.
• Standard input object, System.in, enables applications to read bytes of data typed
by the user.
• Scanner object translates these bytes into types that can be used in a program.
© Copyright 1992-2015 by Pearson Education, Inc. All Rights
Reserved.
16
2.5.4 Declaring Variables to Store Integers
• Variable declaration statements
int number1; // first number to add
int number2; // second number to add
int sum; // sum of number1 and number2
declare that variables number1, number2 and sum hold data of type int
 They can hold integer.
 Range of values for an int is –2,147,483,648 to +2,147,483,647.
 The int values you use in a program may not contain commas.
• Several variables of the same type may be declared in one declaration with the
variable names separated by commas.
© Copyright 1992-2015 by Pearson Education, Inc. All Rights
Reserved.
17
2.5.5 Prompting the User for Input
• Prompt
 Output statement that directs the user to take a specific action.
• Class System
 Part of package java.lang.
 Class System is not imported with an import declaration at the beginning
of the program.
© Copyright 1992-2015 by Pearson Education, Inc. All Rights
Reserved.
18
2.5.6 Obtaining an int as Input from the User
• Scanner method nextInt
number1 = input.nextInt(); // read first number from
user
 Obtains an integer from the user at the keyboard.
 Program waits for the user to type the number and press the
Enter key to submit the number to the program.
• The result of the call to method nextInt is placed in
variable number1 by using the assignment operator,
=.
 “number1 gets the value of input.nextInt().”
 Operator = is called a binary operator—it has two operands.
 Everything to the right of the assignment operator, =, is
always evaluated before the assignment is performed.
© Copyright 1992-2015 by Pearson Education, Inc. All Rights
Reserved.
19
2.5 Another Application: Adding Integers (Cont.)
• Arithmetic
sum = number1 + number2; // add numbers then store total
in sum
 Assignment statement that calculates the sum of the variables
number1 and number2 then assigns the result to variable
sum by using the assignment operator, =.
 “sum gets the value of number1 + number2.”
 Portions of statements that contain calculations are called
expressions.
 An expression is any portion of a statement that has a value
associated with it.
© Copyright 1992-2015 by Pearson Education, Inc. All Rights
Reserved.
20
2.5.9 Displaying the Result of the Calculation
• Integer formatted output
System.out.printf( "Sum is %d%n", sum );
 Format specifier %d is a placeholder for an int value
 The letter d stands for “decimal integer.”
© Copyright 1992-2015 by Pearson Education, Inc. All Rights
Reserved.
21
2.7 Arithmetic
• Arithmetic operators are summarized in Fig. 2.11.
• The asterisk (*) indicates multiplication
• The percent sign (%) is the remainder operator
• The arithmetic operators are binary operators because they each
operate on two operands.
• Integer division yields an integer quotient.
 Any fractional part in integer division is simply truncated (i.e., discarded)—no
rounding occurs.
• The remainder operator, %, yields the remainder after division.
© Copyright 1992-2015 by Pearson Education, Inc. All Rights
Reserved.
22
© Copyright 1992-2015 by Pearson Education, Inc. All Rights
Reserved.
23
2.7 Arithmetic (Cont.)
• Arithmetic expressions in Java must be written in straight-line form to
facilitate entering programs into the computer.
• Expressions such as “a divided by b” must be written as a / b, so that
all constants, variables and operators appear in a straight line.
• Parentheses are used to group terms in expressions in the same manner
as in algebraic expressions.
• If an expression contains nested parentheses, the expression in the
innermost set of parentheses is evaluated first.
© Copyright 1992-2015 by Pearson Education, Inc. All Rights
Reserved.
24
2.7 Arithmetic (Cont.)
• Rules of operator precedence
 Multiplication, division and remainder operations are applied first.
 If an expression contains several such operations, they are applied from left to right.
 Multiplication, division and remainder operators have the same level of precedence.
 Addition and subtraction operations are applied next.
 If an expression contains several such operations, the operators are applied from left to right.
 Addition and subtraction operators have the same level of precedence.
• When we say that operators are applied from left to right, we are referring to their
associativity.
• Some operators associate from right to left.
• Complete precedence chart is included in Appendix A.
© Copyright 1992-2015 by Pearson Education, Inc. All Rights
Reserved.
25
© Copyright 1992-2015 by Pearson Education, Inc. All Rights
Reserved.
26
© Copyright 1992-2015 by Pearson Education, Inc. All Rights
Reserved.
27
2.7 Arithmetic (Cont.)
• As in algebra, it’s acceptable to place redundant parentheses
(unnecessary parentheses) in an ex-pression to make the expression
clearer.
© Copyright 1992-2015 by Pearson Education, Inc. All Rights
Reserved.
28
2.8 Decision Making: Equality and Relational
Operators
• Condition
 An expression that can be true or false.
• if selection statement
 Allows a program to make a decision based on a condition’s value.
• Equality operators (== and !=)
• Relational operators (>, <, >= and <=)
• Both equality operators have the same level of precedence, which is lower than
that of the relational operators.
• The equality operators associate from left to right.
• The relational operators all have the same level of precedence and also
associate from left to right.
© Copyright 1992-2015 by Pearson Education, Inc. All Rights
Reserved.
29
© Copyright 1992-2015 by Pearson Education, Inc. All Rights
Reserved.
30
© Copyright 1992-2015 by Pearson Education, Inc. All Rights
Reserved.
31
© Copyright 1992-2015 by Pearson Education, Inc. All Rights
Reserved.
32
© Copyright 1992-2015 by Pearson Education, Inc. All Rights
Reserved.
33
2.8 Decision Making: Equality and Relational
Operators (Cont.)
• An if statement always begins with keyword if, followed by a
condition in parentheses.
 Expects one statement in its body, but may contain multiple statements if they
are enclosed in a set of braces ({}).
 The indentation of the body statement is not required, but it improves the
program’s readability by emphasizing that statements are part of the body.
• Note that there is no semicolon (;) at the end of the first line of each
if statement.
 Such a semicolon would result in a logic error at execution time.
 Treated as the empty statement—semicolon by itself.
© Copyright 1992-2015 by Pearson Education, Inc. All Rights
Reserved.
34
© Copyright 1992-2015 by Pearson Education, Inc. All Rights
Reserved.
35

More Related Content

Similar to OOP Using Java Ch2 all about oop .pptx (20)

PPT
CSL101_Ch1.ppt
kavitamittal18
 
PPT
CSL101_Ch1.ppt
DrPriyaChittibabu
 
PPTX
CSL101_Ch1.pptx
shivanka2
 
PPTX
CSL101_Ch1.pptx
Ashwani Kumar
 
PDF
Week02
hccit
 
PDF
Lec-2- Ehsjdjkck. Jdkdbd djskrogramming.pdf
RahulKumar342376
 
PPTX
JAVA CLASS PPT FOR ENGINEERING STUDENTS BBBBBBBBBBBBBBBBBBB
NagarathnaRajur2
 
PPT
presentation of java fundamental
Ganesh Chittalwar
 
PPT
a basic java programming and data type.ppt
GevitaChinnaiah
 
PPT
Ch02 primitive-data-definite-loops
James Brotsos
 
PDF
02장 Introduction to Java Applications
유석 남
 
PPTX
Core java complete ppt(note)
arvind pandey
 
PPT
savitchAbsJavaPPT Java Programming Part 1
Ghazanfar Latif (Gabe)
 
PPT
jhtp10_ch02_Intro to java Applications: Input/Output and Operators
WaqasUlHassan10
 
PPT
Comp102 lec 4
Fraz Bakhsh
 
PPT
Presentation on programming language on java.ppt
HimambashaShaik
 
PPTX
Java fundamentals
HCMUTE
 
PDF
ch02-Java Fundamentals-Java Fundamentals.pdf
ayeshazaveri4
 
PPTX
chapter1 part1 (1).pptxmmmklowqzxfvvvvvvvv
mohammadalali41
 
CSL101_Ch1.ppt
kavitamittal18
 
CSL101_Ch1.ppt
DrPriyaChittibabu
 
CSL101_Ch1.pptx
shivanka2
 
CSL101_Ch1.pptx
Ashwani Kumar
 
Week02
hccit
 
Lec-2- Ehsjdjkck. Jdkdbd djskrogramming.pdf
RahulKumar342376
 
JAVA CLASS PPT FOR ENGINEERING STUDENTS BBBBBBBBBBBBBBBBBBB
NagarathnaRajur2
 
presentation of java fundamental
Ganesh Chittalwar
 
a basic java programming and data type.ppt
GevitaChinnaiah
 
Ch02 primitive-data-definite-loops
James Brotsos
 
02장 Introduction to Java Applications
유석 남
 
Core java complete ppt(note)
arvind pandey
 
savitchAbsJavaPPT Java Programming Part 1
Ghazanfar Latif (Gabe)
 
jhtp10_ch02_Intro to java Applications: Input/Output and Operators
WaqasUlHassan10
 
Comp102 lec 4
Fraz Bakhsh
 
Presentation on programming language on java.ppt
HimambashaShaik
 
Java fundamentals
HCMUTE
 
ch02-Java Fundamentals-Java Fundamentals.pdf
ayeshazaveri4
 
chapter1 part1 (1).pptxmmmklowqzxfvvvvvvvv
mohammadalali41
 

Recently uploaded (20)

PPTX
Kel.3_A_Review_on_Internet_of_Things_for_Defense_v3.pptx
Endang Saefullah
 
PPTX
CST413 KTU S7 CSE Machine Learning Neural Networks and Support Vector Machine...
resming1
 
PDF
Generative AI & Scientific Research : Catalyst for Innovation, Ethics & Impact
AlqualsaDIResearchGr
 
PDF
輪読会資料_Miipher and Miipher2 .
NABLAS株式会社
 
PDF
PRIZ Academy - Process functional modelling
PRIZ Guru
 
PDF
Rapid Prototyping for XR: Lecture 6 - AI for Prototyping and Research Directi...
Mark Billinghurst
 
PPTX
Comparison of Flexible and Rigid Pavements in Bangladesh
Arifur Rahman
 
PDF
Validating a Citizen Observatories enabling Platform by completing a Citizen ...
Diego López-de-Ipiña González-de-Artaza
 
PPTX
LECTURE 7 COMPUTATIONS OF LEVELING DATA APRIL 2025.pptx
rr22001247
 
PPTX
Functions in Python Programming Language
BeulahS2
 
PDF
Rapid Prototyping for XR: Lecture 4 - High Level Prototyping.
Mark Billinghurst
 
PPTX
CST413 KTU S7 CSE Machine Learning Introduction Parameter Estimation MLE MAP ...
resming1
 
PDF
Rapid Prototyping for XR: Lecture 2 - Low Fidelity Prototyping.
Mark Billinghurst
 
PDF
Rapid Prototyping for XR: Lecture 3 - Video and Paper Prototyping
Mark Billinghurst
 
PDF
May 2025: Top 10 Read Articles in Data Mining & Knowledge Management Process
IJDKP
 
PDF
Decision support system in machine learning models for a face recognition-bas...
TELKOMNIKA JOURNAL
 
PDF
CLIP_Internals_and_Architecture.pdf sdvsdv sdv
JoseLuisCahuanaRamos3
 
PPTX
Work at Height training for workers .pptx
cecos12
 
PPTX
Computer network Computer network Computer network Computer network
Shrikant317689
 
PPTX
WHO And BIS std- for water quality .pptx
dhanashree78
 
Kel.3_A_Review_on_Internet_of_Things_for_Defense_v3.pptx
Endang Saefullah
 
CST413 KTU S7 CSE Machine Learning Neural Networks and Support Vector Machine...
resming1
 
Generative AI & Scientific Research : Catalyst for Innovation, Ethics & Impact
AlqualsaDIResearchGr
 
輪読会資料_Miipher and Miipher2 .
NABLAS株式会社
 
PRIZ Academy - Process functional modelling
PRIZ Guru
 
Rapid Prototyping for XR: Lecture 6 - AI for Prototyping and Research Directi...
Mark Billinghurst
 
Comparison of Flexible and Rigid Pavements in Bangladesh
Arifur Rahman
 
Validating a Citizen Observatories enabling Platform by completing a Citizen ...
Diego López-de-Ipiña González-de-Artaza
 
LECTURE 7 COMPUTATIONS OF LEVELING DATA APRIL 2025.pptx
rr22001247
 
Functions in Python Programming Language
BeulahS2
 
Rapid Prototyping for XR: Lecture 4 - High Level Prototyping.
Mark Billinghurst
 
CST413 KTU S7 CSE Machine Learning Introduction Parameter Estimation MLE MAP ...
resming1
 
Rapid Prototyping for XR: Lecture 2 - Low Fidelity Prototyping.
Mark Billinghurst
 
Rapid Prototyping for XR: Lecture 3 - Video and Paper Prototyping
Mark Billinghurst
 
May 2025: Top 10 Read Articles in Data Mining & Knowledge Management Process
IJDKP
 
Decision support system in machine learning models for a face recognition-bas...
TELKOMNIKA JOURNAL
 
CLIP_Internals_and_Architecture.pdf sdvsdv sdv
JoseLuisCahuanaRamos3
 
Work at Height training for workers .pptx
cecos12
 
Computer network Computer network Computer network Computer network
Shrikant317689
 
WHO And BIS std- for water quality .pptx
dhanashree78
 
Ad

OOP Using Java Ch2 all about oop .pptx

  • 2. Chapter 2 Introduction to Java Applications; Input/Output and Operators Java™ How to Program, 10/e © Copyright 1992-2015 by Pearson Education, Inc. All Rights Reserved. 2
  • 3. © Copyright 1992-2015 by Pearson Education, Inc. All Rights Reserved. 3
  • 4. 2.3 Modifying Your First Java Program • Class Welcome2, shown in Fig. 2.3, uses two statements to produce the same output as that shown in Fig. 2.1. • New and key features in each code listing are highlighted. • System.out’s method print displays a string. • Unlike println, print does not position the output cursor at the beginning of the next line in the command window.  The next character the program displays will appear immediately after the last character that print displays. © Copyright 1992-2015 by Pearson Education, Inc. All Rights Reserved. 4
  • 5. © Copyright 1992-2015 by Pearson Education, Inc. All Rights Reserved. 5
  • 6. 2.3 Modifying Your First Java Program (Cont.) • Newline characters indicate to System.out’s print and println methods when to position the output cursor at the beginning of the next line in the command window. • Newline characters are whitespace characters. • The backslash () is called an escape character.  Indicates a “special character” • Backslash is combined with the next character to form an escape sequence— n represents the newline character. • Complete list of escape sequences http://docs.oracle.com/javase/specs/jls/se7/html/ jls-3.html#jls-3.10.6. © Copyright 1992-2015 by Pearson Education, Inc. All Rights Reserved. 6
  • 7. © Copyright 1992-2015 by Pearson Education, Inc. All Rights Reserved. 7
  • 8. © Copyright 1992-2015 by Pearson Education, Inc. All Rights Reserved. 8
  • 9. 2.4 Displaying Text with printf • System.out.printf method  f means “formatted”  displays formatted data • Multiple method arguments are placed in a comma-separated list. • Calling a method is also referred to as invoking a method. • Java allows large statements to be split over many lines.  Cannot split a statement in the middle of an identifier or string. • Method printf’s first argument is a format string  May consist of fixed text and format specifiers.  Fixed text is output as it would be by print or println.  Each format specifier is a placeholder for a value and specifies the type of data to output. • Format specifiers begin with a percent sign (%) and are followed by a character that represents the data type. • Format specifier %s is a placeholder for a string. © Copyright 1992-2015 by Pearson Education, Inc. All Rights Reserved. 9
  • 10. © Copyright 1992-2015 by Pearson Education, Inc. All Rights Reserved. 10
  • 11. 2.5 Another Application: Adding Integers • Integers  Whole numbers, like –22, 7, 0 and 1024) • Programs remember numbers and other data in the computer’s memory and access that data through program elements called variables. • The program of Fig. 2.7 demonstrates these concepts. © Copyright 1992-2015 by Pearson Education, Inc. All Rights Reserved. 11
  • 12. © Copyright 1992-2015 by Pearson Education, Inc. All Rights Reserved. 12
  • 13. © Copyright 1992-2015 by Pearson Education, Inc. All Rights Reserved. 13
  • 14. 2.5.1 import Declarations • Helps the compiler locate a class that is used in this program. • Rich set of predefined classes that you can reuse rather than “reinventing the wheel.” • Classes are grouped into packages—named groups of related classes—and are collectively referred to as the Java class library, or the Java Application Programming Interface (Java API). • You use import declarations to identify the predefined classes used in a Java program. © Copyright 1992-2015 by Pearson Education, Inc. All Rights Reserved. 14
  • 15. 2.5.3 Declaring and Creating a Scanner to Obtain User Input from the Keyboard • Variable declaration statement Scanner input = new Scanner( System.in );  Specifies the name (input) and type (Scanner) of a variable that is used in this program. • Variable  A location in the computer’s memory where a value can be stored for use later in a program.  Must be declared with a name and a type before they can be used.  A variable’s name enables the program to access the value of the variable in memory.  The name can be any valid identifier.  A variable’s type specifies what kind of information is stored at that location in memory. © Copyright 1992-2015 by Pearson Education, Inc. All Rights Reserved. 15
  • 16. 2.5 Another Application: Adding Integers (Cont.) • Scanner  Enables a program to read data for use in a program.  Data can come from many sources, such as the user at the keyboard or a file on disk.  Before using a Scanner, you must create it and specify the source of the data. • The equals sign (=) in a declaration indicates that the variable should be initialized (i.e., prepared for use in the program) with the result of the expression to the right of the equals sign. • The new keyword creates an object. • Standard input object, System.in, enables applications to read bytes of data typed by the user. • Scanner object translates these bytes into types that can be used in a program. © Copyright 1992-2015 by Pearson Education, Inc. All Rights Reserved. 16
  • 17. 2.5.4 Declaring Variables to Store Integers • Variable declaration statements int number1; // first number to add int number2; // second number to add int sum; // sum of number1 and number2 declare that variables number1, number2 and sum hold data of type int  They can hold integer.  Range of values for an int is –2,147,483,648 to +2,147,483,647.  The int values you use in a program may not contain commas. • Several variables of the same type may be declared in one declaration with the variable names separated by commas. © Copyright 1992-2015 by Pearson Education, Inc. All Rights Reserved. 17
  • 18. 2.5.5 Prompting the User for Input • Prompt  Output statement that directs the user to take a specific action. • Class System  Part of package java.lang.  Class System is not imported with an import declaration at the beginning of the program. © Copyright 1992-2015 by Pearson Education, Inc. All Rights Reserved. 18
  • 19. 2.5.6 Obtaining an int as Input from the User • Scanner method nextInt number1 = input.nextInt(); // read first number from user  Obtains an integer from the user at the keyboard.  Program waits for the user to type the number and press the Enter key to submit the number to the program. • The result of the call to method nextInt is placed in variable number1 by using the assignment operator, =.  “number1 gets the value of input.nextInt().”  Operator = is called a binary operator—it has two operands.  Everything to the right of the assignment operator, =, is always evaluated before the assignment is performed. © Copyright 1992-2015 by Pearson Education, Inc. All Rights Reserved. 19
  • 20. 2.5 Another Application: Adding Integers (Cont.) • Arithmetic sum = number1 + number2; // add numbers then store total in sum  Assignment statement that calculates the sum of the variables number1 and number2 then assigns the result to variable sum by using the assignment operator, =.  “sum gets the value of number1 + number2.”  Portions of statements that contain calculations are called expressions.  An expression is any portion of a statement that has a value associated with it. © Copyright 1992-2015 by Pearson Education, Inc. All Rights Reserved. 20
  • 21. 2.5.9 Displaying the Result of the Calculation • Integer formatted output System.out.printf( "Sum is %d%n", sum );  Format specifier %d is a placeholder for an int value  The letter d stands for “decimal integer.” © Copyright 1992-2015 by Pearson Education, Inc. All Rights Reserved. 21
  • 22. 2.7 Arithmetic • Arithmetic operators are summarized in Fig. 2.11. • The asterisk (*) indicates multiplication • The percent sign (%) is the remainder operator • The arithmetic operators are binary operators because they each operate on two operands. • Integer division yields an integer quotient.  Any fractional part in integer division is simply truncated (i.e., discarded)—no rounding occurs. • The remainder operator, %, yields the remainder after division. © Copyright 1992-2015 by Pearson Education, Inc. All Rights Reserved. 22
  • 23. © Copyright 1992-2015 by Pearson Education, Inc. All Rights Reserved. 23
  • 24. 2.7 Arithmetic (Cont.) • Arithmetic expressions in Java must be written in straight-line form to facilitate entering programs into the computer. • Expressions such as “a divided by b” must be written as a / b, so that all constants, variables and operators appear in a straight line. • Parentheses are used to group terms in expressions in the same manner as in algebraic expressions. • If an expression contains nested parentheses, the expression in the innermost set of parentheses is evaluated first. © Copyright 1992-2015 by Pearson Education, Inc. All Rights Reserved. 24
  • 25. 2.7 Arithmetic (Cont.) • Rules of operator precedence  Multiplication, division and remainder operations are applied first.  If an expression contains several such operations, they are applied from left to right.  Multiplication, division and remainder operators have the same level of precedence.  Addition and subtraction operations are applied next.  If an expression contains several such operations, the operators are applied from left to right.  Addition and subtraction operators have the same level of precedence. • When we say that operators are applied from left to right, we are referring to their associativity. • Some operators associate from right to left. • Complete precedence chart is included in Appendix A. © Copyright 1992-2015 by Pearson Education, Inc. All Rights Reserved. 25
  • 26. © Copyright 1992-2015 by Pearson Education, Inc. All Rights Reserved. 26
  • 27. © Copyright 1992-2015 by Pearson Education, Inc. All Rights Reserved. 27
  • 28. 2.7 Arithmetic (Cont.) • As in algebra, it’s acceptable to place redundant parentheses (unnecessary parentheses) in an ex-pression to make the expression clearer. © Copyright 1992-2015 by Pearson Education, Inc. All Rights Reserved. 28
  • 29. 2.8 Decision Making: Equality and Relational Operators • Condition  An expression that can be true or false. • if selection statement  Allows a program to make a decision based on a condition’s value. • Equality operators (== and !=) • Relational operators (>, <, >= and <=) • Both equality operators have the same level of precedence, which is lower than that of the relational operators. • The equality operators associate from left to right. • The relational operators all have the same level of precedence and also associate from left to right. © Copyright 1992-2015 by Pearson Education, Inc. All Rights Reserved. 29
  • 30. © Copyright 1992-2015 by Pearson Education, Inc. All Rights Reserved. 30
  • 31. © Copyright 1992-2015 by Pearson Education, Inc. All Rights Reserved. 31
  • 32. © Copyright 1992-2015 by Pearson Education, Inc. All Rights Reserved. 32
  • 33. © Copyright 1992-2015 by Pearson Education, Inc. All Rights Reserved. 33
  • 34. 2.8 Decision Making: Equality and Relational Operators (Cont.) • An if statement always begins with keyword if, followed by a condition in parentheses.  Expects one statement in its body, but may contain multiple statements if they are enclosed in a set of braces ({}).  The indentation of the body statement is not required, but it improves the program’s readability by emphasizing that statements are part of the body. • Note that there is no semicolon (;) at the end of the first line of each if statement.  Such a semicolon would result in a logic error at execution time.  Treated as the empty statement—semicolon by itself. © Copyright 1992-2015 by Pearson Education, Inc. All Rights Reserved. 34
  • 35. © Copyright 1992-2015 by Pearson Education, Inc. All Rights Reserved. 35