In: Computer Science
JAVA programming language:
For refrence:
nextInt ( )
Scans the next token of the input as an int.
How many times does the while loop execute, if the input is 38 20 4 0?
Assume a Scanner object scnr has been instantiated, and that the tokens in the input are delimited by the space character. That means there are 4 tokens in the input.
int num = 2000;
while (num >= 20) {
//Do something
num = scnr.nextInt();
}
select one:
a. 2
b. 1
c.0
d.3
SOLUTION-
Correct answer is "(d) 3 "
Loop starts with 2000,
In the loop next assigned value for number is 38, from the given input and 38>=20 (meets condition)
In next iteration 20 will be assigned to num and that value also satisfies the condition.20>=20
In next iteration num will be 4 which breaks the loop condition because (4 >=20 is false) and breaks, so the loop repeats 3 times before exiting.
You can refer to the code below .
IF YOU HAVE ANY DOUBT PLEASE COMMENT DOWN BELOW I WILL
SOLVE IT FOR YOU:)
----------------PLEASE RATE THE ANSWER-----------THANK
YOU!!!!!!!!----------