0% found this document useful (0 votes)
14 views32 pages

Lecture 4 Ed

The if-else statement allows the program to execute one set of statements if a condition is true, and another set of statements if the condition is false.

Uploaded by

WAFA
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views32 pages

Lecture 4 Ed

The if-else statement allows the program to execute one set of statements if a condition is true, and another set of statements if the condition is false.

Uploaded by

WAFA
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 32

String Concatenation

➢ The plus operator (+) is also used for arithmetic


addition
➢ The function that the + operator performs depends
on the type of the information on which it operates
➢ If both operands are strings, or if one is a string and
one is a number, it performs string concatenation
➢ If both operands are numeric, it adds them
➢ The + operator is evaluated left to right
➢ Parentheses can be used to force the operation order

1
What is the output of the following statements?

public static void main(String[] args) {


int numberOne=5,numberTwo=10;
System.out.println("Belaynesh+Chekol");
System.out.println("Belaynesh"+"Chekol");
System.out.println("numberOne+numberTwo");
System.out.println(numberOne+numberTwo);
System.out.println("numberOne"+numberTwo);

}
2
Escape Sequences
➢ What if we wanted to print a double quote character?
➢ The following line would confuse the compiler
because it would interpret the second quote as the
end of the string
System.out.println ("I said "Hello" to you.");

➢ An escape sequence is a series of characters that


represents a special character
➢ An escape sequence begins with a backslash
character (\), which indicates that the character(s)
that follow should be treated in a special way

System.out.println ("I said \"Hello\" to you.");


3
Escape Sequences
➢ Some Java escape sequences:

Escape Sequence Meaning

\b backspace
\t tab
\n newline
\r carriage return
\" double quote
\' single quote
\\ backslash

4
Write a program to display the following.

**
****
******
******
****
**
Write a program to display the following.

public static void main(String[] args) {


System.out.println(" ** ");
System.out.println(" **** ");
System.out.println("******");
System.out.println("******");
System.out.println(" **** ");
System.out.println(" ** ");
}
Write a program to display the following.
public static void main (String args[])
{
System.out.println(" /\\ ");
System.out.println(" /\"\"\\ ");
System.out.println("/\"\"\"\"\\");
System.out.println("\\\"\"\"\"/");
System.out.println(" \\\"\"/ ");
System.out.println(" \\/ ");
}
Arithmetic Expressions
➢ An expression is a combination of one or more
operands and their operators
➢ Arithmetic expressions compute numeric results and
make use of the arithmetic operators:

Addition +
Subtraction -
Multiplication *
Division /
Remainder %

➢ If either or both operands associated with an


arithmetic operator are floating point, the result is a
floating point
9
Division and Remainder
➢ If both operands to the division operator (/) are
integers, the result is an integer (the fractional part is
discarded)
14 / 3 equals? 4

8 / 12 equals? 0

➢ The remainder operator (%) returns the remainder


after dividing the second operand into the first

14 % 3 equals? 2

8 % 12 equals? 8

10
Operator Precedence
➢ Operators can be combined into complex
expressions
result = total + count / max - offset;

➢ Operators have a well-defined precedence which


determines the order in which they are evaluated

➢ Multiplication, division, and remainder are evaluated


prior to addition, subtraction, and string
concatenation

➢ Arithmetic operators with the same precedence are


evaluated from left to right

➢ Parentheses can be used to force the evaluation 11

order
Operator Precedence
➢ What is the order of evaluation in the following
expressions?

a + b + c + d + e a + b * c - d / e
1 2 3 4 3 1 4 2

a / (b + c) - d % e
2 1 4 3

a / (b * (c + (d - e)))
4 3 2 1

12
Assignment Revisited
➢ The assignment operator has a lower precedence
than the arithmetic operators

First the expression on the right hand


side of the = operator is evaluated

answer = sum / 4 + MAX * lowest;


4 1 3 2

Then the result is stored in the


variable on the left hand side

13
Assignment Revisited
➢ The right and left hand sides of an assignment
statement can contain the same variable

First, one is added to the


original value of count

count = count + 1;

Then the result is stored back into count


(overwriting the original value)

14
Augmented Assignment Operators

➢ The operators +, -, *, /, and % can be combined with the


assignment operator to form augmented operators. Very
often the current value of a variable is used, modified, and
then reassigned back to the same variable.
For example, the following statement increases the variable
count by 1:
count = count + 1;
➢ Java allows you to combine assignment and addition
operators using an augmented (or compound) assignment
operator.
For example, the preceding statement can be written as
count += 1;
Augmented Assignment Operators

➢ The += is called the addition assignment operator.

The augmented assignment operator is performed last after


all the other operators in the expression are evaluated.
Augmented Assignment Operators

➢ Example
double a = 6.5;
a += a + 1;
//a=a+(a+1)
Increment and Decrement Operators

➢ The increment operator (++) and decrement operator


(--) are for incrementing and decrementing a variable
by 1.
For example, the following code increments i by 1 and
decrements j by 1.
int i = 3, j = 3;
i++; // i becomes 4
J--; // j becomes 2
➢ These operators are known as postfix increment (or
postincrement) and postfix decrement (or
postdecrement), because the operators ++ and -- are
placed after the variable.
➢ These operators can also be placed before the variable.
int i = 3, j = 3;
++i; // i becomes 4
--j; // j becomes 2
➢ ++i increments i by 1 and --j decrements j by 1. These
operators are known as prefix increment (or preincrement)
and prefix decrement (or predecrement).
➢ The effect of i++ and ++i or i--and --i are the same in the
preceding examples.
➢ However, their effects are different when they are used in
statements that do more than just increment and
decrement.
int i = 10;
int newNum = 10 * i++;
System.out.print("i is " + i + ", newNum is " + newNum);
newNum = 10 * (++i);
System.out.print("i is " + i + ", newNum is " + newNum);

What will be the output of the above code fragment?


➢ What values result from the following expressions?
•9 / 5
• 695 % 20
•7 + 6 * 5
•7 * 6 + 5
• 248 % 100 / 5
•6 * 3 - 9 / 4
• (5 - 7) * 4
• 6 + (18 % (17 - 12))
Data Conversions
➢ Sometimes it is convenient to convert data from one
type to another
➢ For example, we may want to treat an integer as a
floating point value during a computation
➢ Conversions must be handled carefully to avoid
losing information
➢ Widening conversions are safest because they tend
to go from a small data type to a larger one (such as
a short to an int)
➢ Narrowing conversions can lose information because
they tend to go from a large data type to a smaller
one (such as an int to a short)
23
Data Conversions
➢ In Java, data conversions can occur in three ways:
• assignment conversion
• arithmetic promotion
• casting

➢ Assignment conversion occurs when a value of one


type is assigned to a variable of another
• Only widening conversions can happen via assignment

➢ Arithmetic promotion happens automatically when


operators in expressions convert their operands

24
Data Conversions
➢ Casting is the most powerful, and dangerous,
technique for conversion
• Both widening and narrowing conversions can be
accomplished by explicitly casting a value
• To cast, the type is put in parentheses in front of the value
being converted

➢ For example, if total and count are integers, but we


want a floating point result when dividing them, we
can cast total:
result = (float) total / count;

25
Selection statements
➢ The program can decide which statements to execute
based on a condition.

➢ For example, if a user enters a 0 or negative number


to a height or width of a square and compute area

Statement one: compute and display the result


Statement two: send an error message to the user
Code snippet:
If (height<=0|| width<=0)
{
S.o.p(“Enter a valid height or weight”);
26
}
Selection statements
else
{
Double area=width*height;
S.o.p(area);
}

➢ Selection statements use conditions that are Boolean


expressions.
➢ A Boolean expression is an expression that evaluates
to a Boolean value: true or false.

27
Selection statements
➢ Java provides six relational operators (also known as
comparison operators).

28
if Statements
➢ An if statement is a construct that enables a program
to specify alternative paths of execution.
➢ Java has several types of selection statements: one-
way if statements, two-way if-else statements, nested
if statements, multi-way if-else statements, switch
statements, and conditional expressions.
➢ A one-way if statement executes an action if and only
if the condition is true.
➢ The syntax for a one-way if statement is:
if (boolean-expression)
{
statement(s);
} 29
Two-Way if-else Statements
➢ An if-else statement decides the execution path
based on whether the condition is true or false.
➢ A one-way if statement performs an action if the
specified condition is true. If the condition is false,
nothing is done.
➢ But what if you want to take alternative actions when
the condition is false?
➢ You can use a two-way if-else statement. The actions
that a two-way if-else statement specifies differ
based on whether the condition is true or false.

30
Two-Way if-else Statements
➢ syntax for a two-way if-else statement:
if (boolean-expression)
{
statement(s)-for-the-true-case;
}
else
{
statement(s)-for-the-false-case;
}

31
Two-Way if-else Statements
➢ Write a program that checks if a number is odd or
even.

32

You might also like