C is probably the simplest usable programming language. It’s procedural which means the program is executed in linear order. You start with input and end up with some output.
The logic of your program is made up of the following types of code structures:
- Blocks – Anything between { and } is a block
- Conditional Logic. Use If, if-else and switch to control how logic is managed
- Loops – these let you repeat blocks of code.
I’ve put loops in another tutorial as there’s four different types of loop plus continue and break to worry about.
Blocks are very simple. Anything you put between { and } is a block and is executed as if it were a single statement.
Your program will have to make decisions and we do that by using if, if else and switch statements. Of these if is probably the commonest.
If statement
The syntax of an if statement is very simple.
if (expression) statement;
Expression is any computation that evaluates to a non-zero value. This is the logic equivalent of true. A zero (0) value is the logical equivalent of false.
Here is a very trivial program showing if statements.
#include <stdio.h>
int main() {
int a = 5;
if (a > 10)
printf("A is more than 10\n");
if (a < 10)
printf("A is less than 10\n");
}
In this case as a is less than 10, it will always print out A is less than 10. If you change the line a = 5 to a = 11 and recompile and run it wit will print out A is more than 10.
It really is as simple as that but always remember. The expression must always be inside brackets.
And the syntax says statement but this could be a block of several statements.
if else statement
The syntax is
if (expression) statement1; else statement2;
So if your expression works out to be 0 then statement2 will be done instead. We can simplify the logic of the previous example by using else as well.
#include <stdio.h>
int main() {
int a = 10;
if (a > 10)
printf("A is more than 10\n");
else
if (a < 10)
printf("A is less than 10\n");
else
printf("A is equal to 10\n");
}
This also fixes a minor bug in the first example. Try setting A= 10 in that and see what it outputs. Nothing at all!
Here, all three cases are covered. With A == 10, the first if fails and it then does the else branch which has the if (a <10) and that fails. There’s no need to do an if (a== 10) comparison in that case as the only value that would get to that point has to be 10.
You can mix and match statements and statement blocks in an if.. else like this:
#include <stdio.h>
int main() {
int a = 10;
if (a > 10)
{
printf("A is more than 10\n");
a--
}
else
if (a < 10)
{
printf("A is less than 10\n");
a++;
}
else
printf("A is equal to 10\n");
}
So for the < and > comparisons, if the result is true, the value of a is either decremented or incremented.
The switch statement
A switch statement is effectively a lot of if elses all bundled together. Here’s an example program. Try changing the value assigned to a and recompile and run it. Each case needs a break after the statement or else it will drop through. So 1,2 and 3 will drop through to 4 and print “a<5”.
I’ve deliberately left a bug in. Can you see it?
#include <stdio.h>
int main() {
int a = 10;
switch (a) {
case 1:
case 2:
case 3:
case 4:
{
printf("a<5\n");
break;
};
case 5:
case 6:
printf("a ==5 or a ==6\n");
break;
case 7:
case 8:
case 9:
printf("a<9\n");
case 10:
{
printf("a==10\n");
break;
}
default:
printf("a not in range 1-10\n");
break;
}
}
In the case of 7,8 or 9 it will print “a<9” then immediately also print “a==10”, because there’s no break on the 9 case. If you try assigning a value outside of -20, you’ll hit the catch all (called the default case) and print out “a not in range 1-10”. Unless you can guarantees that the switch value will always hit a case you have defined you should add the default to catch unexpected values.
Also if you look closely and compare the 4 case and the 6 case, you’ll notice that the braces are optional. Both work the same.
In the next tutorial we’ll look at the various loop statements.
Link to previous tutorial.
Link to Next Tutorial