AP Computer Science - Exploring Boolean Expressions and Conditional Statements
AP Computer Science - Exploring Boolean Expressions and Conditional Statements
For each program, predict the result, then type the program into netbeans and run
to confirm, then answer the questions
System.out.println( b0 );
System.out.println( b1 );
System.out.println( b2 );
System.out.println( b3 );
Questions:
0. What logical operator is && ?
boolean b4 = b0 || b1;
boolean b5 = b0 || b2;
boolean b6 = b3 || b1;
boolean b7 = b2 || b3;
Questions:
0. What logical operator is || ?
boolean b2 = !b0;
boolean b3 = !b1;
Questions:
0. What logical operator is ! ?
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;
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:
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.
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?
boolean b0 = fn == "David";
System.out.println( fn + " == \"David\" = " + b0 );
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:
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:
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" );