In: Computer Science
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!
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.