Left Quote    Maybe this world is another planet's Hell.
- Aldous Huxley    
Right Quote
[login] | [Register]

C Tutorial

by: Jester
Page: 5 of 10
(View All)

Boolen Expressions

Any expression that returns TRUE will cause a conditional statement to execute the code contained within its brackets. The computer sees a boolean expression as either true or false. 0 is false, and anything else is true.


The while function is a loop, while the expression is true it will continue to execute the code in its brackets.

This simply means, while the expression is true, execute the code between the curly brackets. The while function checks the expression, if it's true it executes the code then jumps back and tests the expression again, looping until it checks and finds the expression is false, then it continues executing any code after the while() function. In the example above it keeps checking and adding one to a each time, when a reaches 10, it is no longer less than 10, as stated in the expression, so it will discontinue its loop. So the example will print out the numbers 0 to 9.

We can also do:

Which is the same thing, do the code between the curly braces while the expression in the while() statement is true. If the statement is never true, the code in the braces will not be executed at all. Remember when checking for equality in a boolean expression, don't confuse a = b with a == b. = is the operator we use to assign a value to a variable, so if we use a single = it will assign b to a and then check whether a is true or not (effectively checking if b is true).


We could do the same as we did in the while() examples with for(). For() allows us to include everything in one line.

This is neater, it allows us to initialise the variable with a value, then test the expression, then perform an action on the variable. The code will execute while the expression is true, and if it is true, it performs the action and then executes the code between the braces. So we initialise a at zero, test if it's les than 10, if it is increment it, print out the value in a, and then go back and check the expression again, until it's no longer true. For() allows more:

Seperating them with commas, we can include multiple initialisation variables and perform multiple actions, we cannot use a comma in the boolen expression, although we can test multiple conditions by using the AND (&&) sign. This will initialise both variables at zero, check to see whether a is less than 10 AND b is less than 20, while this expression is true execute. When the expression is false discontinue the loop, effectively counting from 0 - 9 with a, and counting up in two's with b.

Remember, although you initialise the variables with a value in for(), you still have to declare them first. Have a play around with these functions, get used to them, and when you're ready read on...


In this chapter we're going to use a new variable arrangement called an array. An array lets you declare and work with many variables of the same type. Say we wanted four integers:

int a, b, c, d;

Pretty easy, but what if we wanted 500? An easier way to do this is:

int a[4];

This is an array. The four integers inside the array are accessed with an index, it works like so:

Remember, although we define the array with four items, the index starts at 0. So the last item in the array is accessed with an index value one lower than the number of items in the array.

We can loop through the items in an array, the following code initialises all the values in the array as 1:

Remember the for() loop from the last chapter? It loops through while i is less than 6, incrementing i each time and assigning 1 to each item in the array, i represents the incrementing array index. C has no range checking, so if you index past the end of an array you will be manipulating memory beyond the range of that array, effectively overwriting data in memory that you shouldn't be. This will cause a crash or garbled data.

Compared to a normal variable.

Array "a" index
1 a[0]
1 a[1]
1 a[2]
1 a[3]
1 a[4]
1 a[5]

variable "b" index
1 b

You would change the value in the array with something like a[3] = 5; yet for the variable, simply b = 5;. An array takes up consecutive addresses in memory, it is an array of variables.

You will see more of arrays as you progress, it's hard to understand many concepts until you see the advantages when you actually use them.

1  |  2  |  3  |  4  |  5  |  6  |  7  |  8  |  9  |  10  |  
Next »

No Comments for this page.

You Must be logged in or a member to comment.

Tutorial Stats

Tutorial Stats

0 Total Comments
3.33 Rating of 5 (3 Votes)


Tutorial Options

· Login to Rate This Article
· Login to Post a Comment
· Read more by this author
Digg This Article! Bookmark This Article Reddit: Bookmark This Article BlinkList: Blink This Article! YahooMyWeb BlogMarks: Add This Mark! Furl: Save This Article Spurl: Mark This Article



· Beginning Visual C++ 6.0
Your Article Here

"" Copyright © 2002-2018; All rights lefted, all lefts righted.
Privacy Policy  |  Internet Rank