Repetition and Looping

If we are expecting a certain condition to be met, how do we ensure that the condition is met before the program proceeds?

Testing for the condition, you may need to test for a given condition many times, and thus need a loop.

What is a loop?

A loop is when a certain portion of a program is repeated until an exit condition is produced and/or met.

Why would we want to repeat a certain portion of a program?

  • to test for a condition
  • to do a particular calculation for different available values

    How is loop implemented in C?

    There are three types of loops in C.
  • for
  • while
  • do while

    What should be noted when using a loop?

    A loop consists of three parts:
  • entry condition
  • task to be repeated
  • exit condition

    When a loop is used four phases take place.
  • test entry condition
  • perform the task
  • update the condition
  • test exit condition


    /* Project:	Lab5 */
    /* File:	loop.C */
    /* Aim:		Looping or Repetition */
    /* Keywords:	for */
    
    #include 
    
    main ()
    {
    	int i=500;
    
    	for (i=500; i<505; i=i+1)
    	{
    		printf ("This is the result of the program:   %d \n", i);
    	}
    }