Introduction to Java Programming and Data Structures - Chapter 3 Selections
Selections in Java Programming
The boolean
Type and Relational Operators
Purpose: Often, programs require comparing two values (e.g., checking if integer
i
is greater than integerj
).Relational Operators: Java provides six comparison operators, also known as
relational operators
, used to compare two values.Result: The outcome of a comparison operation is a
boolean
value, which can be eithertrue
orfalse
.Example:
boolean b = (1 > 2);
would result inb
beingfalse
.
List of Relational Operators:
<
(less than)Mathematics Symbol:
<
Example (radius is 5):
radius < 0
Result:
false
<=
(less than or equal to)Mathematics Symbol:
\le
Example (radius is 5):
radius <= 0
Result:
false
>
(greater than)Mathematics Symbol:
>
Example (radius is 5):
radius > 0
Result:
true
>=
(greater than or equal to)Mathematics Symbol:
\ge
Example (radius is 5):
radius >= 0
Result:
true
==
(equal to)Mathematics Symbol:
=
Example (radius is 5):
radius == 0
Result:
false
!=
(not equal to)Mathematics Symbol:
\ne
Example (radius is 5):
radius != 0
Result:
true
One-Way if
Statements
Syntax:
java if (boolean-expression) { statement(s); }
Execution Flow: If the
boolean-expression
evaluates totrue
, thestatement(s)
inside theif
block are executed. If it evaluates tofalse
, thestatement(s)
are skipped.Flowchart: A diamond shape represents the
boolean-expression
. Iftrue
, flow goes to statements; iffalse
, it bypasses them.Example:
java if (radius >= 0) { area = radius * radius * PI; // PI represents a constant like 3.14159 System.out.println("The area for the circle of radius " + radius + " is " + area); }
Here, if
radius
is greater than or equal to0
, the area is calculated and printed.
Notes on Syntax: While braces
{}
are generally recommended for readability and to avoid potential errors, for a single statement, they are optional.Correct with braces:
if (i > 0) { System.out.println("i is positive"); }
Equivalent without braces for a single statement:
if (i > 0) System.out.println("i is positive");
Incorrect (semicolon after if):
if (i > 0); { System.out.println("i is positive"); }
(This creates an empty statement for theif
, and the print statement is always executed, which is a logic error).
Demo Example (
SimpleIfDemo
): A program that prompts the user for an integer and prints
Selections in Java Programming
The boolean
Type and Relational Operators
Purpose: Compare two values.
Relational Operators: Six operators used for comparison.
Result: A
boolean
value (true
orfalse
).Example:
boolean b = (1 > 2);
results inb
beingfalse
.
List of Relational Operators:
<
(less than) - Math:<
<=
(less than or equal to) - Math:\le
>
(greater than) - Math:>
>=
(greater than or equal to) - Math:\ge
==
(equal to) - Math:=
!=
(not equal to) - Math:\ne
One-Way if
Statements
Syntax:
java if (boolean-expression) { statement(s); }
Execution Flow:
If
boolean-expression
istrue
,statement(s)
execute.If
boolean-expression
isfalse
,statement(s)
are skipped.
Key Points on Syntax:
Braces
{}
are recommended for readability, required for multiple statements.Optional for a single statement:
if (i > 0) System.out.println("i is positive");
Common Error: Do not place a semicolon after the
if
condition, e.g.,if (i > 0);
will lead to logic errors.