Computer Applications
How many times will the following loop execute? What value will be returned?
int x = 2;
int y = 50;
do{
++x;
y -= x++;
}
while(x <= 10);
return y;
Java Iterative Stmts
ICSE Sp 2024
115 Likes
Answer
The loop will execute 5 times and the value returned is 15.
Working
Iteration | X | Y | Remark |
---|---|---|---|
2 | 50 | Initial values | |
1 | 3 | 47 | x = 4 (3 + 1), y = 47 (50 - 3) |
2 | 5 | 42 | x = 6 (5 + 1), y = 42 (47 - 5) |
3 | 7 | 35 | x = 8 (7 + 1), y = 35 (42 - 7) |
4 | 9 | 26 | x = 10 (8 + 1), y = 26 (35 - 9) |
5 | 11 | 15 | x = 12 (11 + 1), y = 15 (26 - 11) |
12 | 15 | Condition becomes false. Loop terminates. |
Answered By
63 Likes
Related Questions
The following code segment should print "You can go out" if you have done your homework (dh) and cleaned your room (cr). However, the code has errors. Fix the code so that it compiles and runs correctly.
boolean dh = True; boolean cr= true; if (dh && cr) System.out.println("You cannot go out"); else System.out.println("You can go out");
Sam executes the following program segment and the answer displayed is zero irrespective of any non zero values are given. Name the error. How the program can be modified to get the correct answer?
void triangle(double b, double h) { double a; a = 1/2 * b * h; System.out.println("Area=" + a); }
Write the output of the following String methods:
(a) "ARTIFICIAL".indexOf('I')
(b) "DOG and PUPPY".trim().length()
Name any two jump statements.