Title: A1259771488Jqhix
1Conditional Statements
Contents
- If else statement
- Relational operators
- Examples using if else statements
- Programming style
- Nested if statements
2Conditional Statements
The if else statement
Form of the if else statement
if ( boolean condition ) //statement
sequence else //alternative statement
sequence
The else clause is optional, and may not be
needed in every situation in which a segment of
code is executed conditionally
3Conditional Statements
Relational operators
The boolean condition in an if else statement
is often expressed as a test of a relationship
between two variables.
Is x equal to y? Is x greater than or equal to
0? Is x less than size? Is x not equal to 0?
(x y)
(x gt 0)
(x lt size)
(x ! 0)
These tests are expressed in java in terms of the
relational operators
tests whether the expressions on the left and
right are equivalent lt Is expression on left
less than or equal to exp. on right? lt Is
expression on left less than expression on
right? gt Is expression on left greater than
expression on right? gt Is expression on left
greater than or equal to exp. on right? ! tests
whether the expressions on the left and right are
not equal
4Conditional Statements
Relational operators
Be sure to distinguish between the relational
operator and the assignment operator
Tests if the contents of variable x are the same
as the contents of variable y
x y
Assigns the value stored in variable y to
variable x (overwriting what is in x and leaving
y unchanged)
x y
Several of the relational operators use two
characters for their symbol. There must NOT be a
space between these two characters.
x y x ! y x lt y x gt y
5Conditional Statements
Example
import java.util.Scanner //needed for input
stream classes public class ConditionalStatementEx
ample public static void main (String
args) //convert a possibly negative integer
received from the keyboard to //its positive
(absolute) value System.out.println(Enter an
integer) Scanner console new
Scanner(System.in) BufferedReader br new
BufferedReader(isr) int theInteger
console.nextInt( ) if (theInteger lt 0)
theInteger - theInteger System.out.println(Th
e absolute value is theInteger)
If the integer is already positive, do nothing
else clause is not needed.
6Conditional Statements
Programming style
public static void main(String args)
//list of statements indented 2 to 4 spaces
int num1, num2, num3 5 num1
3 num3 7 num2 5 num3 11
//which number is larger if (num1 gt
num2) num1 num3 System.out.println(The
larger pair is num1, num3)
else num2 num3 System.out.println(The
larger pair is num2, num3)
Main block
Block of stmts. contained in if -- block
Block of stmts. contained in else -- block
7Conditional Statements
Programming style
Note that if there is only a single statement in
the if or else block, curly brackets are not
needed. If there is more than one statement in
one of these blocks, the curly brackets are
required.
if (boolean condition) statement else
statement
if (boolean condition) statement
statement else statement
statement
Curly brackets optional
Curly brackets required
8Conditional Statements
Nested if statements
Consider a function that assigns a letter grade
to a numerical test score. (The test score is
supplied as a parameter writing functions will
be explained fully later, right now we only wish
to illustrate a nested if statement.
public char gradeGiver(int testScore)
if (testScore gt 90) return A
else if (testScore gt 80)
return B else if (testScore gt
70) return C else //testScore lt
70 return F
By convention, each if and else block is
indented 2 to 4 spaces.
9Conditional Statements
Nested if statements
The preceding form is somewhat difficult to
follow, therefore the preferred way of writing
nested if statements is to use an else if
construction.
public char gradeGiver (int testScore)
if (testScore gt 90) return A else if
(testScore gt 80) return B else if
(testScore gt 70) return C
else return F
10Introduction to Java Streams
11Conditional Statements
Example
import java.io. //needed for input stream
classes public class ConditionalStatementExample
public static void main (String
args) throws java.io.IOException //convert a
possibly negative integer received from the
keyboard to //its positive (absolute)
value System.out.println(Enter an
integer) InputStreamReader isr new
InputStreamReader(System.in) BufferedReader br
new BufferedReader(isr) String inputString
br.readLine( ) int theInteger
Integer.parseInt(inputString) if (theInteger lt
0) theInteger - theInteger System.out
.println(The absolute value is
theInteger)
If the integer is already positive, do nothing
else clause is not needed.
12Conditional Statements
Example
import java.io. //needed for keyboard
input import java.util. //needed for String
Tokenizer public class ConditionalStatementExample
2 public static void main (String
args) throws java.io.IOException
//Enter two numbers and print the larger of the
two System.out.println(Enter two integer
numbers ) InputStreamReader isr new
InputStreamReader(System.in) BufferedReader br
new BufferedReader(isr) String inputString,
s1, s2 inputString br.readLine(
) StringTokenizer st new StringTokenizer(input
String) s1 st.nextToken( ) s2
st.nextToken( ) int num1, num2 num1
Integer.parseInt(s1) num2 Integer.parseInt(s2)
if (num1 gt num2) System.out.println(T
he larger number is num1) else
System.out.println(The larger number is
num2)
13(No Transcript)