Sie sind auf Seite 1von 28

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" );

The compiler actually interprets the statement as

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!);

The Conditional Operator


The conditional operator evaluates to one of
two expressions based on a boolean condition
Its syntax is:
condition ? expression1 : expression2

If the condition is true, expression1 is


evaluated; if it is false, expression2 is
evaluated
The value of the entire conditional operator is
the value of the selected expression
Copyright 2012 Pearson
Education, Inc.

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"));

Trouble with Tabs


//FunkyTabs.java
public class FunkyTabs
{
public static void main(String[] args)
{
System.out.println("123456789\tA");
System.out.println("1234567891\tB");
System.out.println("12345678912\tC");
System.out.println("123456789123\tD");
}
}

Trouble with Tabs


//FunkyTabs.java
123456789A
public class FunkyTabs1234567891 B
{
12345678912C
public static void main(String[]
args)
123456789123
D
{
System.out.println("123456789\tA");
System.out.println("1234567891\tB");
System.out.println("12345678912\tC");
System.out.println("123456789123\tD");
}
}

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);

The order of filling values comes from


the list of variables.
Format strings should match declared
types.

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);

The order of filling values comes from


the list of variables.
Format strings should match declared
types.

%[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

int x = 38; double y=43.62981423;


System.out.printf("abc%7.2fdef%5dgh", y, x);

a bc

43. 63def

38gh

Das könnte Ihnen auch gefallen