10)

Program 9 - A menu and the IF statement

Program 9 A menu and the IF statement Source code - prog9.c
This next program shows the all important if statement, which allows programs to make decisions and take different actions. The if statement in any programming language is what gives a program the appearance of "intelligence". The sample program below displays a menu with three options and asks the user to press a key for one of the options. The program responds by telling the user which option they have pressed. If the user doesn't select one of the presented options, then the program doesn't respond with anything. It's not very useful as a program in its own right, but is good for showing how the if statement works.

 

Program code- new parts shown in red

#include <stdio.h>
#include <conio.h>

void main(void)
{

unsigned char k;
printf("Menu\n\n");
printf("1 .. Option one\n");
printf("2 .. Option two\n");
printf("3 .. Option three\n\n");
printf("Please select an option from above\n\n");
k=getch();
if(k==49)printf("You selected the first option");
if(k==50)printf("You selected the second option");
if(k==51)printf("You selected the third option");

}

 

Description of the program code
The only new lines are the three lines shown in red, all of which are very similar. The if statement has the format :

if ( condition ) perform action

The condition is something which results either as true or false. The three if statments test to see if each of the keys 1, 2 or 3 have been pressed. If you refer to the ASCII character set on the previous page, you find that the code for the character 1 is 49, the code for 2 is 50, and the code for 3 is 51.

What may appear strange is the use of the double equals (== ) in the if statement, this is not a typing error, but the way it is meant to be. It's difficult to explain why, but here goes, don't worry if it doesn't make that much sense yet though.

k=49 can be read out load as "Set the value of the k variable to 49", whereas k==49 can be read as "Is the value of the variable k equal to 49"

The first example ( k=49 ) results in a change being made to the value held in the variable k, even if it is included in an if statement. The second example ( k==49 ), does not result in any changes to the value of the variable k, it results in either a true or false condition ( or yes and no if you prefer ).

The condition you are testing for is enclosed in brackets following the if. If that condition is true then it performs the following action.

It may help to read the first if line as : "if the value of k equals 49 then display you've selected the first option"

If the condition is false ( in other words the value of the variable k is not 49 ) then the following action isn't performed and the program continues onwards.

You are not restricted to just testing for equality with the ==, the list below contains some more examples

Type Description Example Comment
== Equal to a == b If a is the same as b, the condition is true, otherwise it's false
!= Doesn't equal a != b If a and b are two different values the condition is true, otherwise it's false
> Greater than a > b If a is bigger than b then the condition is true, otherwise false
>= Greater than or equal to a >= b If a is bigger than or equal to b then the condition is true, otherwise it's false
< Less than a < b If a is smaller than b then the condition is true, otherwise it's false
<= Less than or equal to a <= b If a is smaller than or equal to b then the condition is true, otherwise it's false

All of these are at your disposal to test for various things.

Going back to the program here, it is not very readable and can be improved upon, the following code shows how to make it more readable, with an explanation following :

#include <stdio.h>
#include <conio.h>

void main(void)
{

unsigned char k;
printf("Menu\n\n");
printf("1 .. Option one\n");
printf("2 .. Option two\n");
printf("3 .. Option three\n\n");
printf("Please select an option from above\n\n");
k=getch();
if(k=='1')printf("You selected the first option");
if(k==
'2')printf("You selected the second option");
if(k==
'3')printf("You selected the third option");

}

The changes are shown in red as normal, and it shows that the ASCII codes that were used in the first version of the program have gone and been replaced with something which makes it far more readable. When a single character is enclosed in single quotation marks ( ' ), the compiler will find and use the ASCII code for the character in single quotes. In case you are wondering, there is no performance difference between using this method or the first method.

 

Summary
Phew that took some describing, but it is worth mentioning that at this point that's about 30% of C programming covered ! Also, if you are still understanding things and had no prior knowledge of programming at the start, scan over this page, and think of the horror you would have faced if it all at started at this point ! There is a bit more to the if statement which is covered on the next page. The menu program will be returned to later to make it more functional and useable.

 

Tasks

9.1) Write a program that asks the user for his/her age and responds by telling them if they are an adult ( over 18 ) or not. The next page will give the solution, but before moving on, try to write it.

 

Go back a page Continue on to next page >>

(c) J.C.Spooner 2001