Class 6
Class 6
Class 6
swapping
nested ifs vs independent ifs
dangling else
comparing Strings
switch
conditional operator
formatting output
swapping
int x = 5, y = 10;
int temp;
temp = x;
x = y;
y = temp;
Nested If Demo
if (n>=8)
System.out.println(n + " is a big number.");
else if (n>=4)
System.out.println (n + " is a medium number.");
else if (n>0)
System.out.println (n + " is a small positive number.");
else
System.out.println (n + " is not a positive number.");
n=9, output: 9 is a big number.
n=2, output: 2 is a small positive number.
Nested If Demo
if (n>=8)
System.out.println(n + " is a big number.");
if (n>=4)
System.out.println (n + " is a medium number.");
if (n>0)
System.out.println (n + " is a small positive number.");
else
System.out.println (n + " is not a positive number.");
n=9: output, 9 is a big number.
9 is a medium number.
9 is a small positive number.
n=2: output, 2 is a small positive number.
dangling else
The Java compiler always associates an else with the
immediately preceding ifunless told to do otherwise by the
placement of braces ({ and }).
Referred to as the dangling-else problem.
Copyright 1992-2012 by
Pearson Education, Inc. All Rights
Reserved.
dangling else
The following code is not what it appears:
if ( x > 5 )
if ( y > 5 )
System .out.println( "x and y are > 5" );
else
System .out.println( "x is < = 5" );
x=10 y=10
x=10 y=3
x=3 y=10
Copyright 1992-2012 by
Pearson Education, Inc. All Rights
Reserved.
dangling else
The following code is not what it appears:
if ( x > 5 )
if ( y > 5 )
System .out.println( "x and y are > 5" );
else
System .out.println( "x is < = 5" );
if ( x > 5 )
if ( y > 5 )
System .out.println( "x and y are > 5" );
else
System .out.println( "x is < = 5" );
Copyright 1992-2012 by
Pearson Education, Inc. All Rights
Reserved.
dangling else
To force the nested ifelse statement to execute as it was
originally intended, we must write it as follows:
if ( x > 5 )
{
if ( y > 5 )
System .out.println( "x and y are > 5" );
}
else
System .out.println( "x is < = 5" );
x=10 y=10
x=10 y=3
x=3 y=10
Copyright 1992-2012 by
Pearson Education, Inc. All Rights
Reserved.
comparing doubles
doubles almost never match allow
tolerance
if ( Math.abs( x- y) < tolerance)
they are essentially equal
comparing strings
if (str1.equals(str2))
they look the same
if ( str1.compareTo(str2))
neg, str1 comes first
pos, str2 comes first
0, look the same
switch statement
in place of nested ifs for some
situations
checks for equality only (implicitly)
can be used when value is int, char,
not double
switch
Example:
switch (grade) {
case 'A': System.out.println(Well done!);
break;
case 'B': System.out.println (Not bad.);
break;
default: System.out.println (A little less TV might
help.);
}
System.out.println (Your grade is + grade);
switch
switch
You can have multiple values giving the same option:
Here 1 is Sunday, and count from there.
switch (day) {
case 2:
case 4:
case 6: System.out.println (gym);
break;
case 3:
case 5: System.out.println (art);
break;
case 1:
case 7: System.out.println (sleep);
}
System.out.println (Enjoy your day!);
conditional operator
example
example:
larger = ( (x > y) ? x : y);
means
if (x > y)
larger = x;
else
larger = y;
conditional operator
example
example:
System.out.print ( "You " +
((score>=60) ? "pass" : "fail"));
Weird Decimals
doublex=2.45;
x=1.05*x;
System.out.println(x);
Weird Decimals
doublex=2.45;
x=1.05*x;
System.out.println(x);
2.5725000000000002
Formatting output
int a = 5;
double b = 1.23;
char c = 'X';
System.out.printf("Age %d. Letter %c. Price
%f\n",
a, c, b);
Formatting output
Age 5. Letter X. Price
1.230000
int a = 5;
double b = 1.23;
char c = 'X';
System.out.printf("Age %d. Letter %c. Price
%f\n",
a, c, b);
%[flag][width][.precision]conversion
conversion (required):
d decimal
f float
c char
s String
%[flag][width][.precision]conversion
width
total width of the field containing the
value
%[flag][width][.precision]conversion
precision
number of digits to the right of the
decimal point,
Rounds (doesn't truncate)
%[flag][width][.precision]conversion
flag
- left justify in the field
, comma separator
0 leading zeros
a bc
43. 63def
38gh