asked 94.4k views
1 vote
For the code below, which combination of for-loop parameters will print characters "fd" char[] letters = {'a','b','c','d','e','f'}; for (int i = INIT; i > THRESH; i = i+INCR) {print(letters[i]);}

asked
User Kokosing
by
9.3k points

1 Answer

2 votes

Final answer:

To print the characters 'fd' from the array {'a','b','c','d','e','f'}, the for-loop parameters should be set to start at index 5 ('f'), end before index 3 ('d'), and decrement by 2. The correct for-loop setup is for (int i = 5; i > 3; i = i-2).

Step-by-step explanation:

The code provided signifies the objective of printing specific characters from an array by manipulating the for-loop parameters.

The array char[] letters = {'a','b','c','d','e','f'} contains the letters a to f, and the goal is to print the characters "fd" using a loop that decrements.

To print the characters 'f' followed by 'd', we need to start from the end of the array and move backwards.

Considering the array indices start at 0, the index of 'f' is 5 and the index of 'd' is 3. Thus, we need to set the loop to start at index 5 and decrement until it reaches index 3.

The correct for-loop parameters are as follows:

  • INIT: Initial index, should be 5 for 'f'
  • THRESH: Threshold index, the loop should terminate before reaching index 3
  • INCR: Incrementation, should be set to -2 to move back two indices per loop iteration

Therefore, the for-loop should be set up as:

for (int i = 5; i > 3; i = i-2) {
print(letters[i]);
}

This loop configuration will print the characters "f" and then "d".