Question

Q7. While (sum <100) or (flag - 1) add 5 to sum endWhile

911. for count = 20 down to 1 add 1 to value endFor;

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

The given questions are basically loops.

Question 7)

Here, a while loop has been implemented. The general structure of the a while loop is:

while (loop conditions)

{

...code inside loop...

}

Here, first the loop condition will be checked. If the condition satisfies then the code inside the loop will be executed. After the execution of the code inside the loop, the condition will be checked again and if satisfies then the inside code will be executed. Thus, till the loop condition fails or becomes FALSE, the code inside loop will be under execution. So, it is better to be cautious to update the loop condition in the insider code, else it may fall in an infinite loop.

The given problem:

While (sum<100) or (flag=1)

add 5 to sum

endWhile

Comparing the general notation of while loop, here, the conditions of the while loop are: sum<100 OR flag=1. Since OR is there, if any of the condition satisfies then the loop condition will be consider to be TRUE. Inside the loop, 5 is added to sum (a variable). Thus, till sum is less than 100 OR flag is equal to 1, 5 will be added to sum variable continuously. Obviously it depends on initial value of sum too. If initial value of sum is more than or equal to 100 and flag is not equal to 1 then the loop will not execute for once. But if sum is less than 100 and even if flag is not equal to 1, the loop will go on till the condition satisfies (since OR is there).

It has to notice that flag is not being updated inside the loop, but sum is being updating. So if flag is equal to 1 before entering the loop, the while loop will be under execution for infinite time. Since flag=1 will always satisfy, the loop condition will become independent of the value of sum due to presence of OR. Only if flag is not equal to 1 then the loop will terminate if sum is not less than 100.

Thus, it is better to update the flag variable inside the loop in order to avoid infinite loop. Also flag is no updated inside the loop code, so it can be ignored in the condition in order to avoid the infinite loop. In that case, the new while loop condition will look like: while (sum<100)

Question 11)

Here, for loop has been implemented. It is different from while loop. In while loop, the condition is checked first, and then the rest of the work is done. But in for loop, a loop variable is initialised inside for the first time (in general) and after that the variable is being updated till it fails the condition of the loop. Till the condition is satisfied, the code inside the loop will be under execution.

for( initialization; loop condition check; loop variable update)

{

...code inside loop...

}

Generally, a loop variable is used to check the loop condition in for loop. First it is initialised, whether inside the for notation or outside. Then the loop condition is checked and goes to execute the codes inside the loop if the condition is TRUE. After that the loop variable is updated. The loop goes on till the condition is satisfied.

The given problem:

for count =20 down to 1

add 1 to value

endFor;

Thus, in terms of C language, the equivalent for loop would look like:

for (count=20 ; count >=1 ; count--)

{

value=value+1;

}

Thus, the loop variable here is count, which is initialised to 20. The loop condition will then checked, which is count>=1 or down to 1. Lastly, each time the loop is being executed, the count variable or loop variable is decreased by 1. Inside the loop, the 1 is added to variable value each time.

This loop will not create any problem like infinite loop since the loop variable is being updated and only one condition is there, count>=1. Though the final value of variable 'value' will depend on its initial value. For 20 times, 1 will be added to variable value here. For example, if initially, value=10, then after the execution of the loop, the new value will become, value=10+20=30.

Add a comment
Know the answer?
Add Answer to:
Q7. While (sum <100) or (flag - 1) add 5 to sum endWhile 911. for count...
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