|
|
if ( CONDITION )
ONE-statement
|
Explanation:
|
|
Here, we will first use a few simple Boolean expressions to learn about the if-statement
|
import java.util.Scanner;
public class If1
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int a;
System.out.print("Enter an integer value: ");
a = in.nextInt(); // Read in an integer from keyboard
System.out.print("Input value = ");
System.out.println(a);
if ( a < 0 ) // If-statement
a = -a; // Negate a ONLY if a < 0
System.out.print("It's absolute value = ");
System.out.println(a);
}
}
|
Example outputs:
cheung@HOME2(40)> java If1 Enter an integer value: -4 Input value = -4 It's absolute value = 4 cheung@HOME2(41)> java If1 Enter an integer value: 4 Input value = 4 It's absolute value = 4 |
How to run the program:
|
|
a = read input from keyboard;
if ( a < 0 )
a = -a;
Print a;
|
|
|
|
Explanation:
|
|
Explanation:
|
a = read input from keyboard;
if ( a < 0 )
a = -a;
Print a;
|
|
|
|
Explanation:
|
|
|
Explanation:
|
|
|
if ( CONDITION )
ONE-statement
|
Notes:
|
|
|
import java.util.Scanner; // Import Scanner class (contains methods
// for reading keyboard input)
public class Abc2
{
public static void main(String[] args)
{
double a, b, c, x1, x2; // Define 5 variable
Scanner in = new Scanner(System.in); // Construct Scanner object
a = in.nextDouble(); // Read in next number into a
b = in.nextDouble(); // Read in next number into b
c = in.nextDouble(); // Read in next number into c
x1 = ( -b - Math.sqrt( b*b - 4*a*c ) ) / (2*a);
x2 = ( -b + Math.sqrt( b*b - 4*a*c ) ) / (2*a);
System.out.print("x1 = ");
System.out.println(x1);
System.out.print("x2 = ");
System.out.println(x2);
}
}
|
Shortcoming:
|
|
Schematically:
|
import java.util.Scanner;
public class If2
{
public static void main(String[] args)
{
double a, b, c, x1, x2; // Define 5 variable
Scanner in = new Scanner(System.in); // Construct Scanner object
a = in.nextDouble(); // Read in next number into a
b = in.nextDouble(); // Read in next number into b
c = in.nextDouble(); // Read in next number into c
x1 = x2 = 0; // Default solution
/* ------------------------------------------------------
Only compute solution if determinant b*b - 4*a*c >= 0
------------------------------------------------------ */
if ( b*b - 4*a*c >= 0 )
{
x1 = ( -b - Math.sqrt( b*b - 4*a*c ) ) / (2*a);
x2 = ( -b + Math.sqrt( b*b - 4*a*c ) ) / (2*a);
}
System.out.print("x1 = ");
System.out.println(x1);
System.out.print("x2 = ");
System.out.println(x2);
}
}
|
How to run the program:
|
Example:
import java.util.Scanner;
public class If3
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int a;
System.out.print("Enter an integer value: ");
a = in.nextInt(); // Read in an integer from keyboard
System.out.print("Input value = ");
System.out.println(a);
if ( a < 0 ) ; // Bogus ;
a = -a;
System.out.print("It's absolute value = ");
System.out.println(a);
}
}
|
Structure diagram of this erroneous program:
|
Notes:
|
Example output:
Enter an integer value: 4 Input value = 4 It's absolute value = -4 (Because a = -a; is always executed) |
How to run the program:
|
Example:
import java.util.Scanner;
public class If2
{
public static void main(String[] args)
{
double a, b, c, x1, x2; // Define 5 variable
Scanner in = new Scanner(System.in); // Construct Scanner object
a = in.nextDouble(); // Read in next number into a
b = in.nextDouble(); // Read in next number into b
c = in.nextDouble(); // Read in next number into c
x1 = x2 = 0; // Default solution
/* ------------------------------------------------------
Only compute solution if determinant b*b - 4*a*c >= 0
------------------------------------------------------ */
if ( b*b - 4*a*c >= 0 )
x1 = ( -b - Math.sqrt( b*b - 4*a*c ) ) / (2*a);
x2 = ( -b + Math.sqrt( b*b - 4*a*c ) ) / (2*a);
System.out.print("x1 = ");
System.out.println(x1);
System.out.print("x2 = ");
System.out.println(x2);
}
}
|
Structure diagram of this erroneous program:
|
Notes:
|
Example output: (use: a = 1, b = 1, c = 1)
x1 = 0.0 x2 = NaN <----- x2 was computed with sqrt(neg. value) |
How to run the program:
|