Monday, December 12, 2011

Coding lessons: C and conditions (Lessons 11)

So far I've discussed how to get some data into a program, and how to display data out of a program.
I looked at adding number data together. Different ways to store and group data.

but so far that's pretty useless without being able to make decisions about data pin software.

So in this lesson I'm going to discuss conditions and statements in C.

so first lets have a look at the kind of operators we use when looking at logical conditions.

Does A equal B?
Does A not equal B?
Is A greater than B?
Is A less than B?
Is A greater than or equal to B?
Is A greater and B or Greater than C?
Is A greater than B and greater than C?

in short we have, equal, not equal, greater than, less than, greater than or equal, less than or equal.
and we want a way of glueing multiple conditions together in an if this AND this, or and if this OR this kind of way.

we also want to know what to do:
if This then do this, otherwise do that instead.

what we're going to look at is IF

if is a function, and like all functions we have a function name, and then parameters to pass to it that are contained inside brackets.


our conditions are all logic based

if 1 = 1 then...

except, we already have a function that happens when we write a=1;

so for the logical is a = b we say

if (a==b)

(double equals signs)

instead of saying then, we open new curly brackets

if (1==1)
printf("one equals one");

notice that the ilne with the if statement is not terminated with a semi colon

for not equal we use the symbol !=

if (1!=1)
printf("Oh dear, one doesn't equal one any more");

in some circumstances we may want to have alternative paths.
for this we use the else statement

if (1!=1)
printf("Oh dear, one doesn't equal one any more");
printf("It's OK, maths still works");

I also mentioned that we may like to use the greater than > or less than <
or greater than or equal to >= or less than or equal to <=

if (1<=5)

or more useful for programs would be saying is a variable something


Now there may be times when we want to glue a few things together.

In this case we say
is A equal to be, and equal to C?

if ((a==b)&&(a=c))
printf("A, B and C are all equal");

you can understand how the & symbol is used for AND statements, (well two of them)

for OR statements we use two pipe characters ||

if ((a>=b)||(a>=c))
printf("A is greater than or equal to either B or C");

you use brackets to contain statements in exactly the same way you would in regular mathematical expressions.

Finally, you may wish to have a lot of possible outcomes, in this case rather than writing out.

if (a==1)
if (a==2)

you use a switch function.

the switch function compares a variable against a series of outputs and runs code against a chosen output.

if no pre-defined output matches the variable then a default set of code can be run.

Lets say that variable A is some error code information.
where the following statements describe the error

if the error code is 0, the program is fine.
if the error code is 1, the program ran out of memory

If anything else happens then this is bad, because some kind of unknown condition has occurred.

so the code would look something like this:

switch (a)
case '0':
printf("no errors have been reported");
case '1':
printf("This program requires more memory, you must have at least some memory to run this program");
printf("this program encountered an unexpected condition and should be shut down");


This should be enough information to get you writing some code that you can use to start manipulating data with.

a good example would be the body mass calculator made in a previous lab, rather than telling people what their body mass index is, then telling them to figure out for themselves if they have a problem you can instead use a nested if statement.

if (bmi>25)
else if(bmi > 20)
printf("ideal weight") /*at this point we know it's less than 25 else the previous statement would have been executed*/
else if(bmi>15)

No comments: