How to Terminate a Finite Loop

One fundamental concept in Computer Science is the "loop." Loops are block instructions a machines reads and executes over and again, until some termination condition is met. Said another way, the computer continues to execute that block until something tells it to stop. Loops without any instructions for stopping are called infinite loops, because they go on forever, or until the computer is turned off. Loops that can stop, or terminate, are called finite loops, because the number of times they can execute is finite.

Instructions

    • 1

      Create the loop in your program using a loop control structure of your choice. For example; you might choose the while loop control structure:

      while ( ) {

      }

    • 2

      Input the block of code to be executed in the loop. For example; print increasingly large numbers in your loop:

      while ( ) {

      print a++;

      }

    • 3

      Put a break statement into your loop to terminate it when certain conditions are met. For example:

      while () {

      print a++;

      if ( a > 10)

      {

      break;

      }

      }

      This terminates the loop when the variable "a" is great than 10.

Tips & Warnings

  • Most loop control structures include termination parameters in the control structure itself. The example above is normally written:

  • while (a<10)

  • {

  • print a++;

  • }

Related Searches:

References

Comments

Related Ads

Featured