For my exhibition piece I have created a poster to explain the function while-loop. A while-loop is a control flow statement that allows code to be executed repeatedly based on a condition. The while-loop consists of a block of code and a condition. The condition is evaluated, and if true, the code runs. This repeats until the condition becomes false. If the condition is false from the beginning then the code never runs.
The metaphor I have chosen to use is one I feel many university students will be able to relate to. The kitchen server at the hostel serves dinner to the students and as long as there is student in line, the server continues to serve. This means the condition is true. The grey circuit of photos on the poster represents this. The black circle with the question mark represents the question - ‘is there students in the line?’ Once there are no students in the line the server stops serving, as the condition has become false. This is represented on my poster by the small red arrow going through the question. And if there were no students in the line from the beginning, the server never begins serving. The red line on the left of my poster shows this, as it skips out the loop.
This example of a loop in code can be modelled as:
· While (there are students in line)
o Serve the students.
A while-loop is a very handy function to use when it is needed. It can save a lot of time, and code writing.
*Because I wrote the write up for my poster before I had finished my final poster, I had to fit my final poster to my write up which meant I had some restrictions when deciding on my final layout for my poster.
No comments:
Post a Comment