Question

A for loop is given below. Use a while loop to perform the same task. for(...

A for loop is given below. Use a while loop to perform the same task. for( i=0;i<=7;i+=2) { Sum=Sum + i; } b: What is value of variable Sum after the end of loop?

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer

Corresponding while loop code for the given 'for' loop.
i=0
while(i<=7)
{
Sum = Sum + i;
i+=2;
  
}

while loop we go till 4th iteration only because after 4th iteration i will be equal to 8 which does not satisfy the condition i.e i<=7. So, loop will stop after 4th iteration.

Value of i and Sum in each iteration
1st : i = 0 , Sum = 0
2nd : i = 2 , Sum = 2
3rd : i = 4 , Sum = 6
4th : i = 6 , Sum = 12

So value of Sum after end of loop is 12.

Add a comment
Know the answer?
Add Answer to:
A for loop is given below. Use a while loop to perform the same task. for(...
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
  • For the while loop below, use the loop invariant given to show that if the pre-condition...

    For the while loop below, use the loop invariant given to show that if the pre-condition is the loop.In each step, clearly state true before the loop then the post-condition is true after what facts are assumed and what facts will be proven. (computes the sum of a list of numbers.) fa ,an While (<n) sum := sum + am j j+1 End-while ° Pre-condition: j- 1, sum a, n is a positive integer, a,,.. a, is a list of...

  • Convert the following while loop into a for loop. int 1 - 50: int sum-07 while...

    Convert the following while loop into a for loop. int 1 - 50: int sum-07 while (sum < 1000) sum - sum + 1; Question 35 (2 points) Saved Given an int variable k that has already been declared, use a while loop to print a single line consisting of 80 dollar signs. Use no variables other than k. int sum = 0; for(int i = 100;sum < 10000;1-- sum = sum + i;

  • DO NOT use for loop and while loop Goal: Learn how to use recursive functions. General...

    DO NOT use for loop and while loop Goal: Learn how to use recursive functions. General requirement: Use cout to fully demonstrate your works for all tasks. The output must clearly show how each of these functions work. If no output is displayed for a task below, 0 point will be given to the task. If the output is not clear, some points may be deducted.    Task 1 – Reverse array (3 pts) Given an integer array like: const...

  • USE JAVA. Just a basic code. We these in Blue J 2) Write a while loop...

    USE JAVA. Just a basic code. We these in Blue J 2) Write a while loop that lets the user enter a number. The number should be multiplied by 10 and the result stored in the variable product. The loop should iterate as long as product contains a value less than 100. 3) Write a do-while loop that asks the user to enter two numbers. The numbers should be added and the sum displayed. The user should be asked if...

  • Four people working independently on an assembly line all perform the same task. The time (in...

    Four people working independently on an assembly line all perform the same task. The time (in minutes) to complete this task for person i, for i=1,2,3,4, has a uniform distribution on the interval [0, i]. Suppose each person begins the task at the same time. a) What is the probability that person 2 takes less than 90 seconds to complete the task? b) What is the average completion time for each person? c) What is the probability that all four...

  • Use Modelsim to debug and compile a while loop statement syntax: while (< expression >) <...

    Use Modelsim to debug and compile a while loop statement syntax: while (< expression >) < statement > module while_example (); reg [5:0] location; reg [7:0] data; always @ (data or location) begin location = 0;//If Data is 0, then location is 32 (invalid value) if (data == 0) begin location = 32; end else begin while (data [0] == 0) begin location = location + 1; data = data >> 1; end end $display ("DATA = %b LOCATION =...

  • Given the following while loop, what is the value assigned to variable z for the given...

    Given the following while loop, what is the value assigned to variable z for the given values of variables a, b and c? mult = 0; while (a < 10) { mult = b * a; if (mult > c) { break; } a = a + 1; } z = a; a = 4, b = 5, c = 20

  • How many times is the loop body of the while statement executed?

     How many times is the loop body of the while statement executed? a. once b. forever C. 49 times d. 50 times e. until a number 50 or larger is entered The value stored in variable s at the end of the execution of the loop could best be described as: a. the average of the numbers scanned b. the sum of the numbers scanned c. the largest of the numbers scanned d. how many numbers were scanned e. the sentinel value The value stored in variable z at the end of the...

  • How many Iterations will this while loop perform? int ico), j(10); cout << "i = "...

    How many Iterations will this while loop perform? int ico), j(10); cout << "i = " << i << endl; cout << "j = " << j << endl; while (i > j) { cout << "j-" « j << endl; j += 2; cout << "i = " << i << endl; } cout << "i = << i << endl; cout << "j = " << j << endl; 5 6 C 8 10 Infinite times Does the...

  • Objectives: Use the while loop in a program Use the do-while loop in a second program...

    Objectives: Use the while loop in a program Use the do-while loop in a second program Use the for loop in a third program Instructions: Part A. Code a for loop to print the Celsius temperatures for Fahrenheit temperatures 25 to 125. C = 5/9(F – 32).Output should be in a table format: Fahrenheit Celsius 25 ? . . . . . . . . Turn in the source and the output from Part A. Part B. Code a while...

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