Question
Please answer both questions thank you.
Rewrite the following for loop into a while loop: int s S = 0; for (int i = 1; i <= 10; i++) { S += i; int s = 0; int i = whi
Question 40 (1 point) Consider the following loop that uses ArrayLists to collect elements that match a condition ArrayList<I
0 0
Add a comment Improve this question Transcribed image text
Answer #1

REWRITING THE FOR LOOP INTO A WHILE LOOP:

Given for loop:

Syntax of for loop

for(variable initialization; condition; increment/decrement)

{

STATEMENTS

}

int s=0;

for(int i=1;i<= 10; i++)

{

s+=i

}

CODING USING WHILE LOOP:

Syntax of while loop:

while(condition)

{

STATEMENTS

}

//Let sfirst initialize th erequired variables before the starting of the while.

int s=0,i=1;
//Next lets write the required condition in the while loop

while(i<=10)

{

STATEMENTS:

//s+=i is nothing but s=s+i

s=s+i;

//incremention

i++;

}

Hence option 2 is the correct option.

QUESTION40:

Given that the lloop uses ArrayLists to collect elements that match a condition.

When we trace the given code,

we see that ArrayList of integers named matches is made

In the for loop variable element is initialized and intakes the given values

In the for loop we have a if condition where the elements greater than 100 are added to the ArrayList matches.

When the ArrayList values contains the elements 17,410,134,151,28,56,100,167,840

The values that enter the matches list are 410,134,151,167,840

They are indexed from 0 to 4 as they are total of 5 integers .

Hence when matches.get(4) is called it returns 840.

If you are satisfied with the answer please give it a THUMBS UP!!!

Add a comment
Know the answer?
Add Answer to:
Please answer both questions thank you. Rewrite the following for loop into a while loop: int...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT