0% found this document useful (0 votes)
5 views

Conditional statements (if statement)

Uploaded by

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

Conditional statements (if statement)

Uploaded by

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

CONDITIONAL STATEMENT

(IF STATEMENTS)
The boolean Type and Operators

Often in a program you need to compare two values,


such as whether i is greater than j. Java provides six
comparison operators (also known as relational
operators) that can be used to compare two values.
The result of the comparison is a Boolean value: true
or false.

boolean b = (1 > 2);


2
Relational Operators
3

 Condition: represented by a logical (Boolean) expression


that evaluates to a logical value of true or false.
 Relational operators:
 Allow comparisons.
 Require two operands (binary).
 Evaluate to true or false.

Ex: String txt ;


int x = 5;
Simple If statement
if(x < 15) //evaluates to true
txt=“X is less than 15”;
Relational Operators (cont.)
4

 There are six comparison (or relational) operators:

Operator Meaning Example


== Equal to x == y
!= Not equal to x != y
> Greater than x>y
>= Greater than or equal to x >= y
< Less than x<y
<= Less than or equal to x <= y
Relational Operators (Cont.)
5

 Relational operators can be used with all three


simple data types:
1. Numbers:
 8 < 15 evaluates to true.
 6.5 != 6.5 evaluates to false.
2. Character:
 Result depends on machine’s collating
sequence (ASCII character set).
 ‘A’ < ‘B’ and ‘C’ < ‘c’ evaluate to True.
If statement :Flow Charts
6

Simple if statement

if-else statement

nested-if statement

Com
plica
te d
if and if...else
7

 if and if...else statements can be used to


create:
1. One-way selection
2. Two-way selection
3. Multiple selections
1. One-Way Selection
8

 One-way selection syntax:

 Statement is executed if the value of the expression


is true.
 Statement is bypassed if the value is false;
program goes to the next statement.
 Expression is called a decision maker.
Example
9
Note

if i > 0 { if (i > 0) {
System.out.println("i is positive"); System.out.println("i is positive");
} }
(a) Wrong (b) Correct

if (i > 0) { if (i > 0)
System.out.println("i is positive"); Equivalent System.out.println("i is positive");
}

(a) (b)

10
Two-Way Selection
11

 Two-way selection syntax:

 If expression is true, statement1 is executed;


otherwise, statement2 is executed.
 statement1 and statement2 can be any Java
statements.
Example
12

int age = 20;

if (age > 18)


{
System.out.println("Eligible to vote.“);
System.out.println("No longer a minor.");
}
else
{
System.out.println("Not eligible to vote.“);
System.out.println("Still a minor);
}
Example
13

 Write a Java Program to Check Whether an


entered Number is Even or Odd.
Multiple Selections: Nested if
14

 Nesting: one control statement is located within another.


 An else is associated with the most recent if that has
not been paired with an else.
Example
15

Write a java program to enter an integer,


then check whether the entered integer is
positive, negative or zero.
Logical Operators
16

 Each comparison operation involves two


operands, e.g., x <= 100.
 It is invalid to write 1 < x < 100 in programming.
 Instead, you need to break out the two
comparison operations x > 1, x < 100, and join with
a logical AND operator.
 Ex: (x > 1) && (x < 100), where && denotes AND
operator.
Logical Operators and Logical Expressions
17

 Logical (Boolean) operators: enable you to combine


logical expressions.
&& (AND) Operator
18

AND (&&)

true true true

true false false

false True false

false false false


|| (OR) Operator
19

OR(||)

true true true

true false true

false True true

false false false


! (Not) Operator
20

When you use the ! operator, !true is false and !


false is true. Putting ! in front of a logical
expression reverses the value of that logical
expression.

You might also like