# 6 October

``// LOOPS( repetition )while( condition) // if false, skip the body{    //statements, will loop around as long as the condition stays true.}int x = 3;while(x<5){    printf("A");   // infinite loop: AAAAAAA.....}printf("B")int x = 3;while(x < 5)      // this version prints: AAB{    printf("A");    x = x + 1;}printf("B")// Types of loops:// 1. counter-controlled loop - # of iterations of loop is known// before we start. //// 2. condition - controlled loop - we dont know in advance how many// iterations.//// 2a. Sentinel controlled - one specific value is requried to to// terminate the loop.//// 2b. Input validation - terminates when a value in a valid range is// entered./*    input validation    your choice: 9    Invalid , try again    your choice :4    invalid , try again    your choice: 2    ok    sentinel    Enter 0 to quit    your choice: 3    subtract    your choice: 2    mutiply    your choice: 0 ( Sentinel)    bye !*/// Example input validationprintf(" Enter Month ( 1 - 12 ): " ); scanf ( " %d" , &month);while (month<1 || month>12) // as long as condition is true,{                           // keep repeating    printf("Invalid, try again." );    printf("Enter month ( 1 - 12 ):");    scanf( "%d", &month);   // updates variable used in condition}// counter controlled - usually an integer.// Identify start point, stop point, direction/step.// start: 0 stop: 5 step : 1int i = 0; // startwhile (i< 5) // stop{    printf("%d n", i*i);   // prints 0 1 4 9 16    i=i+1; // step.}/* Exercise: use a counter loop tocompute the sum of all integers from 1 to 100(answer should be 5050)story of Gauss (but note the subsection title)http://en.wikipedia.org/wiki/Carl_Friedrich_Gauss#Mythology*/``

©2011 Christopher League · some rights reserved · CC by-sa