12)
Program 11 - While and logic
Program 11 | While and logic | Source code - prog11.c |
The program below demonstrates the while
loop, which can be used to make a program repeat
a statement or block of statements over and over again
until a condition is met. The use of the gotoxy
statement is also shown in the program, so that text can
be positioned anywhere on a DOS text screen. The third
new feature that is shown is the use of logic, with the
'C' or operator (||). The program itself is simple, it loops continuously until the user presses the X key on the keyboard. |
Program code- new parts shown in red |
|
Description of the program code |
The first new part
of this program is the gotoxy statement.
This statement can be used to position the cursor
anywhere on a text screen. A text screen is laid out in a
grid, with 80 columns across and 25 rows down. The axis
across, like a graph, is called the x axis
and the axis down is called the y axis.
The top left of the grid (screen ) is x=0,
y=0 and the bottom right is x=79,
Y=24. The command gotoxy(10,5); will position the
cursor 10 character squares along the top, and 5 down.
The next thing to be displayed using a printf statement
will start at this position on the screen. It's probably
better understood by using the example below and changing
the gotoxy values and seeing what happens. :
Keep changing the two values in the gotoxy line, re-compile and run the program to see the effects. The second new part shows the while loop. Firstly note the variable called done is set to 0 when it is declared. When the program reaches the line while(done==0) it tests to see if the condition done==0 is true. In this case it is, because the program has initialised the done variable to zero, so it executes the next line or block. In this case it's a block of code because there are curly brackets defining it. It needed to be programmed as a block because more than one statement is needed. Within this block, the program gets the key pressed by the user and stores it in the k variable ( k=getch(); ). It then checks to see if the key pressed is the X key ( if(k=='x'||k=='X') ). This is different to previous examples of the if statement because the or logical operator is used (||). The line can be read as if k equals 'x' or k equals 'X'. If you remember back to the ASCII character set, upper and lower case letters had a different code. If the user of the program has the CAPS LOCK on, then the ASCII code for the upper case X will be stored in the k variable, if not, the ASCII code for the lower case x will be stored there. This program doesn't mind either way as it checks for both by using the || ( 'or' ) logicial operator. More on logical operators in a minute, back to the while loop .. If the user does press the x key then the value of the done variable is set from 0 to 1. If the user doesn't press the x key then the value of the done variable remains at unchanged at 0. After this line the code block is ended, at this point a test is made on the condition of the while statement at the start of the block. If the condition ( done==0 ) is true, then the program goes back to the top of the block and repeats. If the condition ( done==0 ) is false then it "drops out of the while loop" and continues down. The red arrows in the following graphic show the program flow.
Besides the "logical or" ( || ), you can use the "logical and" ( && ) in conditions. There is an example below of the logical and being used in an if statement :
With a "logical and" only if all the conditions result in true, is the overall result true. The "logical or" will return true if any of the conditions result in true. |
Summary |
The gotoxy
statement is really useful with text based programs as
they allow the positioning of text on the screen, however
there are very few computer games around that use text
displays, so it isn't widely used now. The while loop and
logic is used everywhere though, which is the most
important information to take from this page. The while loop is not the only loop in C, there is another more controlled loop called the for loop, which is covered with a simple example on the next page. |
Tasks |
11.1) Study the program below to see if you can work out what it does, then type it in, and see if you are correct.
|
(c) J.C.Spooner 2001