i just dont get c++ especially while and for loops, functions and how to declare an array argument, as well as arrays |
Here's a quick review.
Let's start with loops:
Loops (like the name suggests) are control structures, which loop through and execute a section of code until or while a condition is true.
Here's the syntax for a
while
loop:
1 2 3
|
while(<condition>) {
//code
}
|
Like the name suggests,
while
loops executes the code within the brackets while the supplied condition is true.
An example of using a while loop:
1 2 3 4
|
int counter = 0;
while(counter != 5) {
counter++;//increment
}
|
The while loop will increment the counter variable until the counter reaches five.
Here's the syntax for a
for
loop:
1 2 3
|
for(<init>; <condition>; <increment>) {
//code
}
|
This one is a bit more confusing for a beginner, but it's very useful.
They're designed with counter variables in mind.
expects a declaration and initialization of a counter variable.
is the condition - loop while this condition is true
increments the counter variable
an example of a
for
loop:
1 2 3
|
for(int counter = 0; counter != 5; ++counter) {
}
|
This code does the same as the
while
loop above. There are some differences, however. Notice that the body of the
for
loop is empty. It's empty in this example, because it doesn't need to do anything other than increment a counter variable, which it does internally.
Another difference is that the counter variable of the
for
loop ceases to exist once the
for
loop terminates, whereas the counter variable for the
while
loop continues to exist past the termination of the
while
loop. This is because the
for
loop counter variable's life is bound by the
for
loops body scope. This means that the counter cannot be used after the
for
loop is over, and any attempt to use it will result in a compilation error.
However, you do not need to declare a
for
loop counter variable on the same line it is initialized. The following example uses a
for
loop counter variable, which continues to exist past the
for
loop's termination:
1 2 3 4
|
int counter;
for(counter = 0; counter != 5; ++counter) {
}
|
Now arrays:
Because arrays and pointers are related in some ways, they can be daunting for beginners. Right now I'll neglect to share the pointer-side of arrays for simplicity.
If a variable is like a crayon, an array is like a crayon box.
A crayon box can hold a certain amount of crayons, it depends on how big the box is.
An array can hold a certain amount of variables, it depends on how big the array is.
A box of crayons has a slot for each crayon to go into. Similarly, an array has a slot for each variable that it holds. Each variable that is contained within an array is called an "element" - which makes sense, because it's an element of the array.
An array needs to know how many elements it should hold when it is declared. The size of the array cannot be changed later on. An array places the elements next to one another - contiguously in memory.
Here's a simple visual representation of how an array holds it's elements in memory:
0 1 2 3
[ 51 ][ 123 ][ 16 ][ 1337 ] |
Notice the upper numbers 0 - 3. They represent the index location of each element, which is how we access an element - through an index.
When we are declaring an array, the syntax is:
type name[number of elements];
Example of an array with 4 integer elements:
int array[4];
Note, that an array also can only hold variables of one type. You can't have an array hold, for instance, a
char
and an
int
. What type of variable an array can hold is also permanent, and cannot be changed later on.
Now we have an array with four integers. However, none of the integers have a value. We can either choose to initialize the array with values for each of it's elements like so :
int array[4] = {1, 2, 3, 4};
Or, we can access each individual element at a time, and give it a value:
1 2 3 4 5 6
|
int array[4];
array[0] = 1;
array[1] = 2;
array[2] = 3;
array[3] = 4;
|
Notice, that the first element of an array is located at index position zero.
Therefore,
it's a mistake to assume that array[1] is the first element, and that array[4] is the last element. It's counter intuitive, but it's just something you'll have to learn. If you attempt to access an element which doesn't exist, like so:
array[4] = 5;
You'll probably get a run-time out-of-bounds error. That's a big no-no.
Now, to combine loops and arrays - here's an example of how you might initialize an array with certain computed values:
1 2 3 4 5 6 7 8 9
|
int number_of_elements = 4;
int array[number_of_elements];
int counter = 0;
while(counter < number_of_elements) {
array[counter] = counter * 2;
}
|
We could have expanded the
while
loop, and instead could have written:
1 2 3 4 5 6 7 8
|
int number_of_elements = 4;
int array[number_of_elements];
array[0] = 0 * 2;
array[1] = 1 * 2;
array[2] = 2 * 2;
array[3] = 3 * 2;
|
Now functions:
Functions are a way of splitting up your code, and making it more reusable.
This is a good thing, because you don't need to re-type the same thing over and over again. Let's say that you want to print a very long sentence twice.
1 2 3 4 5 6
|
int main() {
std::cout << "This is a very long sentence which goes on and on." << std::endl;
std::cout << "This is a very long sentence which goes on and on." << std::endl;
return 0;
}
|
You could have written the above. Or, you could have used a function which prints the sentence.
1 2 3 4 5 6 7 8 9
|
void printSentence() {
std::cout << "This is a very long sentence which goes on and on." << std::endl;
}
int main() {
printSentence();
printSentence();
return 0;
}
|
In this example, the amount of time you save typing isn't really apparent, but for larger and more complex projects it's impossible to get anything done without functions.
Like a lot of things, functions have their quirks and their learning curve.
When you want to create a new function, you typically declare and define it all at once. You can chose to declare and function, but not define it - but that's a whole different story.
This explanation turned out very long, and it took me a while to write it. Now it's late and I'm tired, so for now I'll just redirect you towards the tutorials on this website:
http://www.cplusplus.com/doc/tutorial/functions/
There's a lot of stuff I'm omitting. I'll probably edit this later for completeness.