Logo designed by CO2 - http://www.planetquake.com/CO2
Logo designed by CO2 - http://www.planetquake.com/CO2
Logo designed by CO2 - http://www.planetquake.com/CO2
Logo designed by CO2 - http://www.planetquake.com/CO2
   
Back to HomePage

Back to Tutorial menu

Credits

  • Puke
  • SumFuka
  • Saqquara

  •  

      More While loops...

    Right, lets continue improving our ASCCHRT program!

    It is obviously not perfect to have the user type one key to continue, while pressing any other key to quit. Most DOS programs like this often have 'Y' for yes, 'N' for no, and if any other key is entered, the question would repeat itself, until it gets a Y or N from the user. Lets rebuild our program to do this:

    /* tutorial 7 - ASCCHRT3.C - */
    #include <stdio.h>
    
    int main()
    {
    char ChrVal, again;
    
    again = 'Y';
    
    while((again == 'Y') || (again == 'y'))
       { 
       printf("\nWhat character do you want the ASCII value of > ");
       scanf("\n%c", &ChrVal);
       printf("\nThe ASCII value of %c is %d\n", ChrVal, ChrVal);
              
       again = 'x';
    
       while ((again != 'Y') && (again != 'y') && (again != 'N') && (again != 'n')) 
          {
          printf("\nDo you want to look up another (Y/N) > ");
          scanf("\n%c", &again);
          }
       }
    }
    

    Now, this code is the same as before, except instead of asking the user to press A key to continue, we ask the user for a yes or no, to continue or quit. To do this we made had a while loop nested(inside) in another while loop. The first loop , as before, checks to see if again equals Y or y, then, the second loop repeats the question 'Do you want to look up another?' until the user presses 'Y', 'y', 'n', or 'N' to make the loop return FALSE. If a user enters 'N' or 'n', both loops are false, so the program just quits, however, if 'Y' or 'y' is typed, then the first loop returns TRUE, and the whole program starts again. Since you are using the && operator in the second while loop all conditions must be true, for the whole loop to evaluate to true.

    The again = 'x' statement is used before the inner loop, because otherwise again would still hold 'Y' from before, the inner while loop would never execute in the first place, and the outer loop would never end, because again would always hold 'Y'.

    OK, now lets make your program even fancier! Some programs clear the screen before executing, so lets write a new function to do just that, and gain some more loop experience on the way:) This program will clear the screen every time the first while loop is executed.

    /* tutorial 7 - ASCCHRT4.C - */
    #include <stdio.h>
    
    void clrscrn(void)
    {
    int counter;
    
    counter = 0;
    while(counter != 25)
       {
       printf("\n");
       counter = counter + 1;
       }
    }
    
    int main()
    {
    char ChrVal, again;
    
    again = 'Y';
    
    while((again == 'Y') || (again == 'y'))
       { 
       clrscrn();
       printf("\nWhat character do you want the ASCII value of > ");
       scanf("\n%c", &ChrVal);
       printf("\nThe ASCII value of %c is %d\n", ChrVal, ChrVal);
              
       again = 'x';
    
       while ((again != 'Y') && (again != 'y') && (again != 'N') && (again != 'n'))  
          {
          printf("\nDo you want to look up another (Y/N) > "); 
          scanf("\n%c", &again); 
          } 
       }
    clrscrn();
    }
    
    
    

    Note how clrscrn() can be used like any other function such as printf and scanf. We made this code into a new function to save time, since we call the code up twice. Instead of all those loops etc, the same effect could have been done be the following statement:

    printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n")

    But that way you wouldn't learn anything would you??!? Anyway, enough of that, join me next tutorial and we'll make your program even cooler(joy!). Oh yeah, more damn loops too:)

    Tutorial by Puke


    Back To Tutorial 6 Forward To Tutorial 8
                   
    
    
    
    

     

    Logo designed by CO2 - http://www.planetquake.com/CO2

    This site, and all content and graphics displayed on it,
    are ©opyrighted to the Quake DeveLS team. All rights received.
    Got a suggestion? Comment? Question? Hate mail? Send it to us!
    Oh yeah, this site is best viewed in 16 Bit or higher, with the resolution on 800*600.
    Thanks to Planet Quake for there great help and support with hosting.
    Best viewed with Netscape 4 or IE 3