11)
Program 10 - If and else + indentation
Program 10 | If and else + indentation | Source code - prog10.c |
Three new features of C programming are included in this next program which shows the if statement with an else as well. Also indentation of programs is covered at this point which makes programs a lot neater and more readable. If you have been downloading the sample programs so far, then all of these have been indented. The use of curly brackets is covered in more detail than before, with the program below extended in the description section showing them. |
Program code- new parts shown in red |
|
Description of the program code |
A quick glance at
the program above will reveal that it has a slight look
that is different from previous programs, in that the
bottom two printf statements do not line up with the rest
of the code. This is called indentation
and it makes programs a lot easier to follow and read. An
explanation will follow shortly, first though, besides
the indentation, the only new part to this program is the
use of the else statement. The program asks the users age on the scanf("%d",&age); line which stores the input in the variable called age. It then goes on to check to see if the age entered is 18 or over if(age>=18). If this condition is true it processes the printf("\nYou are an adult"); line, if it isn't it processes the printf("\nYou are a child"); line. By using the else a program can not only set the action to take if the condition is true, it can also set an action to take if the tested condition is false. You do not have to have an else with every single if statement as the previous page proved, but it is there to use if needed. Sometimes a program may want to perform more than one action when the condition in an if statement is true or false. Take a look at the program below, and note the extra curly braces that have been added.
What this program does, is not only say if the user is an adult or child, but if (s)he is an adult, it then tests to see if they can claim a pension or not ( over 50 in this case ). If the if(age>=18) results in a true condition then the code between the curly braces is executed, if not, it skips past the curly braces to the else statement and executes the associated action. Like was said before an else statement doesn't have to be included. If the program needs to perform more than one action with the else condition then curly braces can be used again following the else to define a block of code to execute. So in conclusion the format of an if -> action -> else -> action statement is :
Finally back to indentation. A 'C' compiler isn't bothered by things like spaces or lines in program code. The only reason the programs so far have been written with each statement on a new line is because it is neater to read and follow. The program below is exactly the same as the program at the top of the page, however this time, it's been written all on a single line. This will still compile and work fine, try copying the program below and compiling and running it to prove it. #include
<stdio.h> When you compare the two programs though, this one, and the one at the top of the page, it is far easier to follow and understand the first one than this, but to a C compiler, they are both the same thing. 'C' isn't that fussy about spaces in programs either, as long as spaces aren't included in the middle of statements ( like printf ), and variable names, it's not bothered how many you put in. Programmers of all languages use this to good effect, by indenting programs to make them easier to read. Looking at the program above it is easy to see what code gets executed when the age is greater than or equal to 18, because the lines have been indented to the right by 2 spaces.
All programmers use different ways of indenting programs, the most common way is to use TAB key, however, I prefer two spaces, but it's all down to personal preference. A basic rule to follow is, if there is an open curly bracket indent the program to the right for the following lines. If there is a close curly bracket, indent the following lines to the left. Indentation of programs isn't required, but it is considered "good programming practice". |
Summary |
Three things have
been covered on this page : the use of the else command,
code blocks with curly brackets, and indentation. Out of
these three the use of the curly brackets to seperate
blocks of code is the most important to understand, as
they are used throughout C programs as will be seen
later. The next page introduces another extension to the if statement as well as shows the first iterative statement, the while loop. |
Tasks |
No tasks for this section... try and think a task up and implement it. |
(c) J.C.Spooner 2001