Computer Applications
Give the output of the following program segment and mention how many times the loop is executed.
int K = 1;
do
{K += 2;
System.out.println(K);
} while (K <= 6);
Java Iterative Stmts
2 Likes
Answer
Number of times the loop is executed: 3 times
Output
3
5
7
Explanation
- The
do-whileloop executes the block first and then checks the condition. - Initial value:
K = 1- 1st iteration:
K = 1 + 2 = 3→ printed - Condition:
3 <= 6→ true
- 1st iteration:
- 2nd iteration:
K = 3 + 2 = 5→ printed- Condition:
5 <= 6→ true
- Condition:
- 3rd iteration:
K = 5 + 2 = 7→ printed- Condition:
7 <= 6→ false → loop stops
- Condition:
Hence, the loop executes 3 times.
Answered By
1 Like
Related Questions
Users must be above 10 years to open a self-operated bank account. Write this logic using a ternary operator and store the result (the eligibility message) in a String variable named idStatus and print it.
Give the output of the following program segment:
String S = "GRACIOUS".substring(4); System.out.println(S); System.out.println("GLAMOROUS".endsWith(S));The following program segment calculates and displays the factorial of a number. [Example : Factorial of 5 is 1 x 2 x 3 x 4 x 5 = 120]
int p, n = 5, f = 0; for(p = n; p > 0; p--) f *= p; System.out.println(f);Name the type of error if any; correct the statement to get the desired output.
int X[][] = {{4, 5}, {7, 2}, {19, 4}, {7, 4}};
Write the index of the maximum element and the index of the minimum element of the array.