A computer program can make a 2 way decision:
import java.util.Scanner; // Demo in BlueJ
public class ComputeArea
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
double radius;
double area;
System.out.print("Enter a radius: ");
radius = input.nextDouble();
if ( radius < 0 )
{
System.out.println("Illegal radius");
}
else
{ // Print area of circle
area = 3.14159 * radius * radius;
System.out.println(area);
}
}
}
|
DEMO: demo/03-selections/01-intro/ComputeArea.java
|
|
DEMO: demo/03-selections/01-intro/BooleanVar.java
|
What are the outputs of this Java program:
public class Quiz
{
public static void main(String[] args)
{
double x = 1; // Breakpoint here
System.out.println( x > 0 );
System.out.println( x < 0 );
System.out.println( x != 0 );
System.out.println( x >= 0 );
System.out.println( x != 1 );
System.out.println( x <= 1 );
System.out.println( x > 1 );
}
}
|
DEMO: demo/03-selections/01-intro/Quiz.java
Step through the program in BlueJ