analyze the following code. Is count < 100 always true, always false, or sometim
ID: 3632537 • Letter: A
Question
analyze the following code. Is count < 100 always true, always false, or sometimes true or sometimes false at Point A, Point B, and Point C?int count = 0;
while (count < 100)
{
// Point A
cout << "welcome to C++! ";
count++;
// Point B
}
// Point C
Explanation / Answer
Scanner scan = new Scanner(System.in); System.out.print("Enter a non-negative integer: "); int num = scan.nextInt(); // x >= 0... true sometimes, always, never? while(x < 0) { // x >= 0... true sometimes, always, never? System.out.print("Invalid input. Enter a non-negative integer: "); num = scan.nextInt(); } // x >= 0... true sometimes, always, or never? Reasoning about Assertions Inside an if/else structure, you may know the truth of an assertion based on the condition(s) for the structure: if(x > 100) { // point A } else { // point B } Assertion x > 100: At point A? At point B? Assertion x < 200: At point A? At point B? A while or for loop condition may also indicate the truth of an assertion: while(!x.equals("stop")) { // point A } // point B Assertion x equals "stop": At point A? At point B? Right after a variable is initialized, you know the truth value of assertions about that variable: int number = 14; // assertion number < 20 is ALWAYS true Typically assertions about the value of parameters have unknown truth values: public static void mystery(int parm1, int parm2) { // assertion: parm1 < 0 SOMETIMES true // any assertion about the value of parm2: SOMETIMES true At the top of a loop body, the loop condition must be true: while(a > 5) { // is a > 5? ALWAYS true } After a loop, the loop condition must be false: while(a > 5) { ... } // is a > 5? NEVER true public static void mystery(int x, int y) { int z = 0; // Point A x = x - y; // Point B while (x > y) { // Point C z++; // Point D } // Point ERelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.