How to write computer code repeating
Entering, changing, or deleting a value.That is to say that functions and procedures are initiated by user actions, such as one of the following: With modern graphical user interfaces, program flow is often controlled through the use of events. ArraysĪrrays can also be used for selection, as I explain in this The exact syntax of this varies according to the programming language you're using, but generally the code would look something like this:Īt the time of writing the latest version of Python does not support case/switch, but it has been announced that it will be included in a future release. The most common way to do this is using an if. This is also known as selection (especially for GCSE), or making a decision.
Conditional BranchingĪnother way to control program flow is to use conditional branching - that is, to perform an action or jump to another section of the program, depending on some sort of condition. The inner loops ensure that the six values are unique - repeatedly generating pseudorandom numbers and checking them against the numbers drawn previously until a new (i.e. The outside loop (with the counter ball_no) is used to iterate through the six balls, to generate the six values. Jball = Math.round(Math.random() * 49) + 1 įor (previous = 0 previous < ball_no previous++) loop that counts to 10 and prints the results:įor (ball_no = 0 ball_no < 6 ball_no++) loop - some languages also have a repeat.until loop which behaves in a similar way (except that the condition will be reversed). Both BASIC and JavaScript/C/C++ have a while. For example, the following loops count from 1 to 10, printing the numbers as they go:įor(loop_counter=1 loop_counter= 0 n = n - 5) Condition-Controlled Loops - While.Īnother type of loop repeats a section of code, not a pre-determined number of times, but while a particular condition exists. For loops have associated with them an integer variable which is either incremented or decremented between two limits - this determines the number of times that the code within the loop is executed. The most common type of loop is the for loop, which seems to exist in most programming languages, including BASIC, C/C++, JavaScript and Pascal. Looping is the name given to repeated commands within a program - the commands can either be repeated a pre-determined number of times (known in GCSE exams at count-controlled loops), or they can be repeated until a certain condition occurs (known in GCSE exams at condition-controlled loops). The final method, used with modern graphical user interfaces, to control flow using the user's input. There are two main ways of controlling the flow within a program - we can other repeat sections of the program until a process is completed, or we can jump to another section of the program to perform another function. With modern multi-tasking, event-driven environments, this tends not to be the case, but some of these techniques can still be used within individual procedures and functions. Back in the good old days, program execution began at the top of the code and proceeded a line at a time towards the bottom.