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

  •  

      Controlling Program Flow - While loop

    The small ASCCHRT program you wrote last time was kind of useful on its own, but generally, applications don't just dump you back in the OS when they have finished doing something. Most start off with a menu, where the user can select a number of options. And when you have selected an option, and finished working with it, the program returns you to the menu, where you can make another chose, or decide to quit entirely.

    Now, lets rewrite the ASCCHRT, and ask the user if he would like to go again, or quit, after he has finished a conversion. For this we will use a while loop, which takes the following form:

    while(condition)
    {
    statements;
    statements;
    statements;
    }

    If you were to say that in English, it would mean 'while the condition is true, execute the statements and repeat, until the condition is false'. Get it? Well lets show by rewriting the program using a while loop.

     /* Tutorial 6 - ASCII chart - ASCCHRT2.C */
    #include <stdio.h>
    
    int main()
    {
    char ChrVal, again;
    
    again = 'A';

    while(again == 'A') { printf("\nWhat character do you want the ASCII value of > "); scanf("\n%c", &ChrVal); printf("The ASCII value of %c is %d \n", ChrVal, ChrVal); printf("\n\nPress A to look up another "); printf("\nor any other key to exit "); scanf("\n%c", &again); } }

    OK, first you made again equal to A. Then you said, 'while again is still equal to A, repeat all the code in between the brackets'. Say the user typed Q, instead of A, the while loop would check if again is A, however, because it isn't, the condition again == 'A' will evaluate to FALSE, and the program would the skip the code in the loop, and continue down.

    Note the double equal sign (==) in the statement while(again == 'A'). Do not confuse this with a single equal sign, they are two completely different things! While the single sign is used to assign a value to a variable, the double equal sign is a logical operator. What that means is when C sees the ==, it evaluates the statement to see if its true or false. So what again == 'A' means is, 'if again equals A then its true, if not, then its false'. There are also other operators, that check for other things, such as the opposite of ==, !=. That mean 'is not equal to'. So if in our example, a user typed 'A' to this statement: again != 'A', the result would be FALSE!

    OK, what if the user typed in 'a', but really meant 'A'? It would be real stupid if the program dumped him into the OS. Now, to fix this, we have to have two statements, and then check to make sure at least one is TRUE. To do this we use the || operator. || really mean 'OR'. Consider:

    while((again == 'A') || (again == 'a'))

    See that? It just checks if again is 'A' or 'a', and if it is, then the whole thing is true. Note the two sets of brackets, they aren't necessary, however, are used for clarity, and to make sure the compiler doesn't misinterpret the code. There is one more operator. &&. As the symbols suggest, this means 'AND'. Consider:

    while((again == 'A') && (again == 'a'))

    This would check, to see if again equals 'A' and 'a'. If one of the statements are false, the whole thing is false. Both sides need to be true, so in this program, that would stink, because you would never be able to do another, since your one-letter character, can't be 'A' and 'a' at once:)

    That's enough for today, take a break:)

    Tutorial by Puke


    Back To Tutorial 5 Forward To Tutorial 7
                   
    
    
    
    

     

    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