Question

In: Computer Science

in c++ >>When is beneficial to use entry condition loop (for loop) and when is it...

in c++ >>When is beneficial to use entry condition loop (for loop) and when is it beneficial to use exit condition loop (while loop). Give an example for each loops.

Solutions

Expert Solution

`Hey,

Note: If you have any queries related the answer please do comment. I would be very happy to resolve all your queries.

for loop is used when we know how much time we have to run the iterations for example sum of first 10 numbers. While loop is used when we know the exit condition which means calculating the sum until the sum reaches a tolerance level.

For example

#include <iostream>

using namespace std;

  

int main()

{

  

    int i = 0;

  

    for (i = 5; i < 10; i++) {

        cout << "hello\n";

    }

  

    return 0;

}

While loop

#include <iostream>

using namespace std;

  

int main()

{

  

    int i = 5;

  

    while (i < 10) {

        i++;

        cout << "GFG\n";

    }

  

    return 0;

}

Kindly revert for any queries

Thanks.


Related Solutions

C language and it has to be a while loop or a for loop. Use simple...
C language and it has to be a while loop or a for loop. Use simple short comments to walk through your code. Use indentations to make your code visibly clear and easy to follow. Make the output display of your program visually appealing. There is 10 points deduction for not following proper submission structure. An integer n is divisible by 9 if the sum of its digits is divisible by 9. Develop a program that: Would read an input...
C# language. Answer in a discussion format. What is a loop? When do you use a...
C# language. Answer in a discussion format. What is a loop? When do you use a loop versus a selection statement? What are some examples when an infinite loop is appropriate? Describe one of the following three types of loops supported in C#: while loop for loop do loop (or do-while loop) Describe the steps necessary to make a while loop end correctly: Explain the difference between incrementing and decrementing loop control variables. Explain the benefits of using both pretest...
Which of the following statements is TRUE? A for loop executes indefinitely if the loop condition...
Which of the following statements is TRUE? A for loop executes indefinitely if the loop condition is always false. If the loop condition in a for loop is initially false, the loop body does not execute. C++ does not allow you to use fractional values for loop control variables that are real numbers. In a for statement, if the loop condition is omitted, it is assumed to be false.
Why is an exit condition so important for a while loop?
Why is an exit condition so important for a while loop?a.An exit condition is not vital.  A while loop works fine without one.b.So the programmer knows when it is okay to leave the room.c.If no exit condition is provided, while loops cannot continue beyond one iterationd.It allows the loop to end instead of running indefinitely.e.While loops are unstable, and must know how to exi
3. (5pts.) When should a Count controlled loop be used? C++
3. (5pts.) When should a Count controlled loop be used? C++
Use C++ The ADT UnsortedType List function ‘DeleteItem(ItemType item)’ creates an endless loop error when trying...
Use C++ The ADT UnsortedType List function ‘DeleteItem(ItemType item)’ creates an endless loop error when trying to delete a word or key that is not in the list. Example: Delete the word “letter” from the unsorted list. Here are the lists contents before the DeleteItem(ItemType item) call: super     formula travel     free        thick      Josephine            Clara      education The question is ‘how can I exit gracefully from the DeleteItem(itemType Item) call when the word or key is not in the unsorted list and...
Find a loop invariant (I) for the following while loop with the post-condition {Q: S=1}, where...
Find a loop invariant (I) for the following while loop with the post-condition {Q: S=1}, where S is an integer and the operator / represents an integer division. Show your work step by step to receive full credit. while (S > 1) do S = S / 2; }
Describe the entry/exit condition in a perfectly competitive market. Further, detail why this condition hinges on...
Describe the entry/exit condition in a perfectly competitive market. Further, detail why this condition hinges on the relative position of MC and ATC. Why is AVC not important for the entry/exit condition? Can you tell what happens to revenues for a given firm in this market as more firms enter?
This is for C++ You must use either a FOR, WHILE, or DO-WHILE loop in your...
This is for C++ You must use either a FOR, WHILE, or DO-WHILE loop in your solution for this problem. Write a quick main console program to output the following checker pattern to the console: #_#_#_#_# _#_#_#_#_ #_#_#_#_# _#_#_#_#_ #_#_#_#_#
CODE MUST BE IN C++ (please use for loop) write a program that loops a number...
CODE MUST BE IN C++ (please use for loop) write a program that loops a number from 1 to 10 thousand and keeps updating a count variable (count variable starts at 0 ) according to these rules: n1 = 14 n2 = 54 n3 = 123 if the number is divisible by n1, increase count by 1 if the number is divisible by n2, increase count by 2 if the number is divisible by n3, increase count by 3 if...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT