Determine the results of executing the following Java statements
LAB
WEEK 7
JAVA PROGRAMMING
Using the Selection Control Structure if
Determine the results of executing the following Java statements. To do so, step through the statements, and then write what should be displayed.
Use the following two assignment statements for the exercises of this lab.
int x = 6;
Boolean found = false;
1. if (x> 10)
System.out.println(“x is greater than 10.\n”);
System.out.println(“Selection allows decision making.\n”);
Answer:
Selection allows decision making.
2. if (x = = 6)
System.out.println(“ A match is found.\n”);
System.out.println(“ System.out.println(“Sequence continues after selection is complete.\n
Answer:
Syntax Error.
3. if (x < 8)
{
System.out.println (“x is within the range.\n”);
System.out.println(“This is a true statement.\n”);
}
System.out.println(“And, after selection, the next statement is executed.\n”);
Answer:
x is within the range.
This is a true statement.
And, after selection, the next statement is executed.
4. if (x = = 6 | | found)
System.out.println(”Problem 4 is true.\n”);
System.out.println(“End of Problem 4\n”);
Answer:
Problem 4 is true.
End of Problem 4
5. if (x = = 6 && found)
System.out.println(“Problem 5 is true.\n”);
System.out.println(“ End of Problem 5\n”);
Answer:
End of Problem 5
6. if (x ! = 6&&!found)
System.out.println(“Problem 6 is true,\n”);
System.out.println(“End of Problem 6\n”);
Answer:
End of Problem 6
7. if (x > 0 &&x< 10);
System.out.println(“x is in range\n”);
System.out.println(“The value of x is “ + x + “.\n”);
Answer:
x is in range
The value of x is 6.
8. if (x > 10)
System.out.println ( “ x is greater than 10.\n”);
else
System.out.println(“x is less than or equal to 10.\n”);
System.out.println(“ Selection allows decision making.\n”);
Answer:
x is less than or equal to 10.
Selection allows decision making.
9. if ( x = = 6)
System.out.println( “A match is found.\n”);
else
System,out.println(“ A match was not found.\n”);
System.out.println(“Sequence continues after selection is complete.\n”);
Answer:
A match is found.
Sequence continues after selection is complete.
10. if (!(x < 8) )
{
System.out.println(x is within the range.\n”);
System.out.println(“This is a true statement.\n”);
}
else
{
System.out.println(“x is out of range.\n”);
System.out.println(“This is a false statement.\n”);
}
System.out.println(“After selection, the next statement is executed.\n”);
Answer:
x is out of range.
This is a false statement.
After selection, the next statement is executed.
11. if (x > = 5)
{
}
else
System.out.println(“Have a good day.\n);
System.out.println(“The value of x is “ + x + ‘\n’);
Answer:
The value of x is 6
12. if (x = = 6 && found)
System.out.println(“Problem 5 is true.\n);
else
{
System.out.println(“Problem 5 is false.\n”);
System.out.println(“Both condition must be true.\”);
}
System.out.println(“End of Problem 5\n”);
Answer:
Problem 5 is false.
Both condition must be true.
End of Problem 5
13. if ( x ! = 6 && ! found)
System.out.println(“Problem 6 is true.\n”);
else
System.out.println(“Problem 6 is false.\n”);
System.out.println(“End of Problem 6\n”);
Answer:
Problem 6 is false.
End of Problem 6
14. if (x > 0 && x < 10)
System.out.println(“ x is in range\n”);
else
System.out.println(“ x is not in range\n”);
Answer:
x is in range
12 years ago
Purchase the answer to view it
- lab_java__week7.docx