Do-While Loop

Shown above is a simple do-while loop. The basic concept of a do-while is to do { a set of instructions } until a condition has been met in the while (). In the above example the user is prompted to enter a number. The do-while loop will continue until the user inputs the value of 0. This is a good form of controlling the flow of your program because it can be used to execute a set of instructions until the end of an input file for example.
For Loop

This block of code is a utilizing an import and very commonly used loop for beginner programmers. The for loop is commonly used to increment a set of instructions a specific amount of times. In the above code it states (int n=10; n>0; n--) what this is doing is declaring n=10 and then decrementing until n is no longer greater than zero. An easy way to look at a for loop is for(initialization, condition, increment) statement. Each time through the loop the value of n is show on the screen. At the end of the loop the word "FIRE!" is show on screen. Incrementing a counter is essential for most programs.
No comments:
Post a Comment