You are on page 1of 35

Chapter Four

OOP using Java Prepared by Mulugeta M. 1


Chapter Four

Expressions and Flow Control

OOP using Java Prepared by Mulugeta M. 2


Outline
 Distinguish between instance, static and local variables

 Describe how to initialize instance variables (using constructor)

 Recognize, describe, and use Java software operators

 Distinguish between legal and illegal assignments of primitive Types

 Identify Boolean expressions and their requirements in control

constructs
 Recognize assignment compatibility and required casts in fundamental

types
 Use if, switch, for, while, and do constructions and the labeled forms of

break and continue as flow control structures in a program

OOP using Java Prepared by Mulugeta M. 3


Types of variables
 Local variable

 Created inside a method or block of statement


 E.g.
public int getSum(int a, int b)
{ This method has
int sum=0;
local variables
such as a, b, sum.
sum=a+b;
Their scope is
return sum; limited to the
} method.

OOP using Java Prepared by Mulugeta M. 4


Types of variables
 Instance variable
 Created inside a class but outside any method. Store different data
for different objects.
 E.g.
public class Sample1 {
int year;
This method has
int month; two instance
public void setYear(int y) { variables year and
year=y; month.
}

}

OOP using Java Prepared by Mulugeta M. 5


Types of variables
 Static variable
 Created inside a class but outside any method. Store one data for all
objects.
 E.g.
public class Sample {
static int year; This method has
int month;
one static
public static void setYear(int y) {
variable: year
year=y;
}

}

OOP using Java Prepared by Mulugeta M. 6


Instance vs. Static variables
Shared by
all objects
O1 O4

O2
Static O5
variable

O3 O stands for O6
Object
OOP using Java Prepared by Mulugeta M. 7
Instance vs. Static variables

O1 Instance O4
Instance variable
variable
Each object
has its own
data for the
instance
variable Instance
variable
O2 Instance
variable O5

Instance
Instance variable
variable

O3 O stands for O6
Object
OOP using Java Prepared by Mulugeta M. 8
Java Operators

 Refer to

Core Java Volume I-


Fundamentals, 8th Editions
Around Page
43

OOP using Java Prepared by Mulugeta M. 9


Java Operators
 Arithmetic operators
 + (addition)
 – (subtraction)
 * (multiplication)
 / (division)
 Integer division by zero is not allowed where as floating-
point division by zero yields an infinite.

OOP using Java Prepared by Mulugeta M. 10


Java Operators
 Increment and Decrement operators
 ++ adds 1 to the current value of the variable .

 -- subtracts 1 from the variable.

 Variants: postfix and prefix

 E.g. ++m and n++

OOP using Java Prepared by Mulugeta M. 11


Java Operators
 Assignment operator
 Used to assign value to a variable.

E.g.
int x=34;

OOP using Java Prepared by Mulugeta M. 12


Java Operators

 Logical operators

 && for the logical “and”


operator and
 || for the logical “or” operator.

OOP using Java Prepared by Mulugeta M. 13


Java Operators

Relational operators
== (equal),

!= (not equal)

< (less than),

> (greater than),

<= (less than or equal), and

>= (greater than or equal) operators.

OOP using Java Prepared by Mulugeta M. 14


Java Operators
Bitwise Operators
When working with any of the integer types, you have operators

that can work directly with the bits that make up the integers.
The bitwise operators are

& (“and”)

| (“or”)

^ (“xor”)

~ (“not”) e.g. used to get one’s complement of a number. (Read

more on page 1522, Java how to program 7th Ed.)

OOP using Java Prepared by Mulugeta M. 15


Casting
Conversions between Numeric Types conversions
without
information loss

conversions
that may lose
precision

Legal conversions between numeric types

OOP using Java Prepared by Mulugeta M. 16


Casting…
Conversions in which loss of information is

possible are done by means of casts.


The syntax for casting is to give the target type in

parentheses, followed by the variable name. E.g.

double x = 9.997; double x = 9.997;

int nx = (int) x; int nx = (int) Math.round(x);

nx is now 10
nx is now 9

OOP using Java Prepared by Mulugeta M. 17


Casting…

OOP using Java Prepared by Mulugeta M. 18


Conditional Statements

if Conditions
 if condition

 If…else condition

 if …else if…else condition

OOP using Java Prepared by Mulugeta M. 19


Conditional Statements

 if condition --- Syntax


if (condition)
{ For example:

statement1 if (yourSales >= target)

statement2 {

... performance = "Satisfactory";


bonus = 100;
}
}

OOP using Java Prepared by Mulugeta M. 20


Conditional Statements
 if … else condition --- Syntax
if (condition)
{
statement1
statement2
For example:
if (yourSales >= target)
...
{
} performance = "Satisfactory";
else bonus = 100 + 0.01 * (yourSales - target);
{ }
statementx else
statementy {
... performance = "Unsatisfactory";
bonus = 0;
}
}

OOP using Java Prepared by Mulugeta M. 21


Conditional Statements
 if … else if … else condition --- Syntax
if (condition)
{
statement1
...
}
else if
{
statementx
...
}

else
{
statement…
}

OOP using Java Prepared by Mulugeta M. 22


Conditional Statements
Multiple Selections—The
switch Statement
The if/else construct can be

cumbersome when you have


• byte,
to deal with multiple • short,
selections with many • int
• char
alternatives.
E.g.
To set up menuing system

OOP using Java Prepared by Mulugeta M. 23


Repetition Statement

 for loop

 while loop

 do…while loop

OOP using Java Prepared by Mulugeta M. 24


Repetition Statement…for
 Syntax
for ( initialization; loopContinuationCondition; increment )
{
Statement (s)
}

Example

OOP using Java Prepared by Mulugeta M. 25


Repetition Statement…while
 Syntax

initialization;
while ( loopContinuationCondition )
{
statement
int x=0;
while(x<4)
increment; {
} System.out.println(“x=“+x);
x++;
}

OOP using Java Prepared by Mulugeta M. 26


Repetition Statement…do while
 Syntax
int x=0;
do
do
{ {
statement System.out.println(“x=“+x);
x++;
}
}
while ( condition ); while(x<4);

OOP using Java Prepared by Mulugeta M. 27


Repetition Statement
 break and continue Statements

 break in
 switch statements
 repetition statements.

 The break statement, when executed in a while, for,

do…while or switch, causes immediate exit from


that statement.

OOP using Java Prepared by Mulugeta M. 28


Repetition Statement

OOP using Java Prepared by Mulugeta M. 29


Repetition Statement

continue Statement
The continue statement, when executed in

a while, for or do…while, skips the


remaining statements in the loop body and
proceeds with the next iteration of the loop.

OOP using Java Prepared by Mulugeta M. 30


Repetition Statement

OOP using Java Prepared by Mulugeta M. 31


Outline
 Distinguish between instance, static and local variables

 Describe how to initialize instance variables (using constructor)

 Recognize, describe, and use Java software operators

 Distinguish between legal and illegal assignments of primitive Types

 Identify Boolean expressions and their requirements in control

constructs
 Recognize assignment compatibility and required casts in fundamental

types
 Use if, switch, for, while, and do constructions and the labeled forms of

break and continue as flow control structures in a program

OOP using Java Prepared by Mulugeta M. 32


Summery
 Variables

 Operators

 Casts

 Conditions

 Loops

 Break and Continue

OOP using Java Prepared by Mulugeta M. 33


Exercises
1. Write an application that finds the smallest of
several integers. Assume that the first value
read specifies the number of values to input
from the user.

2. Write an application that calculates the


product of the odd integers from 1 to 15.

OOP using Java Prepared by Mulugeta M. 34


Exercises
3. What does the following program segment do?
for (int i = 1; i <= 5; i++ )
{
for (int j = 1; j <= 3; j++ )
{
for (int k = 1; k <= 4; k++ )
System.out.print( '*' );
System.out.println();
} // end inner for
System.out.println();
} // end outer for

OOP using Java Prepared by Mulugeta M. 35

You might also like