0% found this document useful (0 votes)
86 views

AP Computer Science - Exploring Boolean Expressions and Conditional Statements

The document contains examples of Java code using boolean expressions and comparison operators. It demonstrates the logical operators &&, ||, and !, as well as comparison operators like <, <=, ==, >, >=, and !=. It also shows if/else conditional statements. The examples are meant to predict the output and understand how boolean expressions and comparison operators work in Java. Questions are provided after each code example to check understanding.

Uploaded by

Anish Buddolla
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
86 views

AP Computer Science - Exploring Boolean Expressions and Conditional Statements

The document contains examples of Java code using boolean expressions and comparison operators. It demonstrates the logical operators &&, ||, and !, as well as comparison operators like <, <=, ==, >, >=, and !=. It also shows if/else conditional statements. The examples are meant to predict the output and understand how boolean expressions and comparison operators work in Java. Questions are provided after each code example to check understanding.

Uploaded by

Anish Buddolla
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

AP Computer Science A - Exploration of boolean expressions with boolean operators

and comparison operators

For each program, predict the result, then type the program into netbeans and run
to confirm, then answer the questions

0. public static void main( String [] args ) {


boolean b0 = true;
boolean b1 = true;
boolean b2 = false;
boolean b3 = false;

System.out.println( b0 );
System.out.println( b1 );
System.out.println( b2 );
System.out.println( b3 );

boolean b4 = b0 && b1;


boolean b5 = b0 && b2;
boolean b6 = b3 && b1;
boolean b7 = b2 && b3;

System.out.println( b0 + " && " + b1 + " = " + b4 );


System.out.println( b0 + " && " + b2 + " = " +b5 );
System.out.println( b3 + " && " + b1 + " = " +b6 );
System.out.println( b2 + " && " + b3 + " = " +b7 );
}

Questions:
0. What logical operator is && ?

1. public static void main( String [] args ) {


boolean b0 = true;
boolean b1 = true;
boolean b2 = false;
boolean b3 = false;

boolean b4 = b0 || b1;
boolean b5 = b0 || b2;
boolean b6 = b3 || b1;
boolean b7 = b2 || b3;

System.out.println( b0 + " || " + b1 + " = " + b4 );


System.out.println( b0 + " || " + b2 + " = " +b5 );
System.out.println( b3 + " || " + b1 + " = " +b6 );
System.out.println( b2 + " || " + b3 + " = " +b7 );
}

Questions:
0. What logical operator is || ?

2. public static void main( String [] args ) {


boolean b0 = true;
boolean b1 = false;

boolean b2 = !b0;
boolean b3 = !b1;

System.out.println( "!" + b0 + " = " + b2 );


System.out.println( "!" + b1 + " = " + b3 );
}

Questions:
0. What logical operator is ! ?

3. public static void main( String [] args ) {


int x = 5;
int y = 7;
int z = 7;

boolean b0 = x < y;
boolean b1 = x <= y;
boolean b2 = x == y;
boolean b3 = x > y;
boolean b4 = x >= y;
boolean b5 = x != y;

boolean b6 = z < y;
boolean b7 = z <= y;
boolean b8 = z == y;
boolean b9 = z > y;
boolean b10 = z >= y;
boolean b11 = z != y;

System.out.println( x + " < " + y + " = " + b0 );


System.out.println( x + " <= " + y + " = " + b1 );
System.out.println( x + " == " + y + " = " + b2 );
System.out.println( x + " > " + y + " = " + b3 );
System.out.println( x + " >= " + y + " = " + b4 );
System.out.println( x + " != " + y + " = " + b5 );
System.out.println( z + " < " + y + " = " + b6 );
System.out.println( z + " <= " + y + " = " + b7 );
System.out.println( z + " == " + y + " = " + b8 );
System.out.println( z + " > " + y + " = " + b9 );
System.out.println( z + " >= " + y + " = " + b10 );
System.out.println( z + " != " + y + " = " + b11 );
}

Questions:
0. What do <, <=, >, >= do?
1. What does == do? How is it different from =?
2. What does != do? Explain how the symbols for != make sense.
3. What would this expression test:

int x = ...;
boolean b0 = x < 10 || x > 20;

4. Suppose you had a program with several variables and you want to test
if one of the variables is between the other two:

public static void main( String [] args ) {


int x = ...;
int y = ...;
int z = ...;

boolean b = // type code here so that b is true if y is between x


and z, false if otherwise
// Hint: you'll need to use one of the boolean operators in
conjunction with a comparison operator

System.out.pritnln( b );
}

Test your program with several sets of values for x, y, and z to make
sure that it works as expected.

4. public static void main( String [] args ) {


double x = 100.0 / 300.0;
double y = 1.0 / 3.0;
double z = 100.0 / 3.0;

boolean b0 = x == y;
System.out.println( x + " == " + y + " = " + b0 );

boolean b1 = z / 100.0 == y;
System.out.println( (z/100.0) + " == " + y + " = " + b1 );

Questions:
0. Were you surprised by the output of this program? What do you think is
going on?

5. public static void main( String [] args ) {


String fn = "David";

boolean b0 = fn == "David";
System.out.println( fn + " == \"David\" = " + b0 );

System.out.println( "Type \"Stigant\" (without the \"s) and press


enter");
Scanner kb = new Scanner( System.in );
fn = kb.next();

boolean b1 = fn == "David";
System.out.println( fn + " == \"David\" = " + b1 );

boolean b2 = fn == "Stigant";
System.out.println( fn + " == \"Stigant\" = " + b2 );
}

Questions:
0. Were you surprised by the output of this program? What do you think is
going on? We will talk about this one in class.

6. Predict the output of this program, then run it to see if you are correct:

public static void main( String [] args ) {


int x = 5;
int y = 7;

if ( x < y ) {
System.out.println( "x < y" );
} else {
System.out.println( "y < x" );
}
if ( 2 * x < y ) {
System.out.println( "yay" );
} else if ( 2*x - 4 < y ) {
System.out.println( "boo" );
} else {
System.out.println( "blah" );
}
}

0. Predicted output:

1. Explain what if does. Start by talking about the type of the


expression in the parentheses if ( A ) ...
2. Explain what else does.
3. Explain what else if does.
4. In the code

int x = ...;

System.out.print( "A" );
if ( x < 10 ) {
System.out.print( "B" );
} else if ( x < 20 ) {
System.out.print( "C" );
} else if ( x < 30 ) {
System.out.print( "D" );
} else {
System.out.print( "E" );
}
System.out.println( "F" );

For which values of x will an A appear in the output? B? C? D? E? F?


What are all the possible output results (ie is ABC possible?
What outputs are possible?)

You might also like