Question

In: Computer Science

1) Describe a real-world situation for which a for loop would provie an appropriate simulation. 2)...

1) Describe a real-world situation for which a for loop would provie an appropriate simulation.

2) Describe a real-world situation for which a while loop would be more appropriate.

3) How could a while loop be coded to run forever?

4) How could a for loop be coded to run forever?

5) Describe a testing strategy for ensuring that your loop conditions are properly coded.

*These are conceptual questions, I don't need any actual code. Please give me conceptual answers NOT CODE!

Solutions

Expert Solution

Answer 1)

Real life simulation for a for-loop can be described as follows:

Let's say that there is a person working as a security personnel during the night shift. Let's say he works from 11pm to 7 am. He is given specific instructions to which he has to adhere to. There is a beginning and an end to his shift and he has been given some other small tasks (checking rooms or other areas where he works). Let’s say his shift is for 8 hours and in between he has to take a walk for 20 mins every 2 hours until his shift is over. This can simply be programmed by for-loops.

Hence in the above example the beginning and the end of his shift is pre-defined with a certain iteration.

Answer 2)

Real life simulation for a while-loop can be described as follows:

Let's assume that you are filling a bucket with a glass. Now you cannot know how many glasses it takes to fill up the entire bucket but at one point the bucket will be full and you will stop. This is the perfect example of a while loop.

You keep on doing the task until your desired state is reached.

Answer 3)

In a simple While-loop there may be a situation where the loop might run forever, also knowns as Infinite loop or Endless loop. This happens when there no specific condition which determines the end of the loop i.e. no final state is mentioned where we have to reach to end the repeating task. For example, we can mentioned a TRUE condition in a while loop which never turns to false like while moon revolves around the earth, there are waves created in the ocean . Here we assume that the moon revolves around the earth endlessly, so the waves will never cease to exist.

Answer 4)

There are various syntaxes for the FOR-loop in different languages, in which we almost mention a starting and an ending state with sometimes an iterative step. Now a for loop will run endlessly when there is a starting condition but there is a conflict in the ending condition or the ending condition is not mention properly. For example if we write a for loop with a starting condition as 0 and the ending condition as the highest prime number with +1 as the iterative step then the for loop may never end and the statements in the for loop will execute forever.

Answer 5)

To ensure a proper functioning loop we can always perform loop testing. Loop Testing is defined as a software testing type, that completely focuses on the validity of the loop constructs. This is a simple WHITE BOX unit testing technique which checks the structure of the loop including the starting condition, the ending condition and the number of times the loop is executed.

Apart from the WHITE BOX testing we can also make some changes to our own code to make sure we are not coding any loop that runs forever. To avoid the infinite loop we need to create a separate variable or another data field that updates when a flow runs. Then we simply add a condition to only run if the value for that variable is not equal to whatever we mention.


Related Solutions

• Describe one situation with real-world examples in which a programmer might want to create a...
• Describe one situation with real-world examples in which a programmer might want to create a loop that tests its condition in the beginning of the loop and one situation in which the condition is tested at the end of the loop. • There are many situations where infinite loops may occur. Discuss those situations and provide best practices for each of the loop types that help avoid writing infinite loops.
Topic: Real-World Monopolies Describe an example of a real-world industry or market that would be considered...
Topic: Real-World Monopolies Describe an example of a real-world industry or market that would be considered by economists to be a natural monopoly. What characteristics of the industry make it a monopoly? What is the impact of the monopoly power on its customers? Why might government want to regulate natural monopolies? How might such regulation be structured?
Describe a real-world business situation that could be addressed by collecting and analyzing a set of...
Describe a real-world business situation that could be addressed by collecting and analyzing a set of data using the t-test. What open source would you use to collect that data?
Describe a real world situation where the properties of a solution are different from those of...
Describe a real world situation where the properties of a solution are different from those of its solvent and where this is beneficial.
Describe a research situation where survival analysis would be an appropriate technique
Describe a research situation where survival analysis would be an appropriate technique
Question 1 Find a ‘real world situation’ in which a non-cooperative game occurs in politics, business,...
Question 1 Find a ‘real world situation’ in which a non-cooperative game occurs in politics, business, commerce, group work etc Identify the Nash equilibria (if any). Discuss the merits of various stratagies. In the ‘real world’ is your game played once between the participants or played again and again? How might this influence the strategies used.
Describe a situation that you have seen that would be appropriately modeled using simulation. Explain why...
Describe a situation that you have seen that would be appropriately modeled using simulation. Explain why modeling using simulation would be appropriate.
2-1 Discussion: Two-Dimensional Motion, Forces, and Newton's Third Law Choose a real world situation, such as...
2-1 Discussion: Two-Dimensional Motion, Forces, and Newton's Third Law Choose a real world situation, such as diving off a diving board, rocket propulsion, or birds flying, and select a motion within that situation that does not occur in a straight line, and is set in motion by the interaction of two objects. Motion that does not occur in a straight line indicates that more than one force is acting on the object and they are responsible for what you are...
Describe an example of a real-world industry or market that would be considered by economists to...
Describe an example of a real-world industry or market that would be considered by economists to be a natural monopoly. What characteristics of the industry make it a monopoly? What is the impact of the monopoly power on its customers? Why might government want to regulate natural monopolies? How might such regulation be structured?
Describe an example of a real-world industry or market that would be considered by economists to...
Describe an example of a real-world industry or market that would be considered by economists to be a natural monopoly. What characteristics of the industry make it a monopoly? What is the impact of the monopoly power on its customers? Why might government want to regulate natural monopolies? How might such regulation be structured?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT