In the first lesson I talked a bit about what the processor is doing in terms of programs. If you recall, I briefly pointed out that the processor handles only one line of instruction at a time. Programs are linear in this way, starting at a certain point and reading line by line until an end of the program is reached. For the time being, think of programs in this linear fashion.
However, programs don't have to be pictured as a straight line. Programs can be split into "blocks" and can run different blocks of code in the middle of any given block. For instance, your main program may call for another block of code to be executed before it ends. In this way, think of the program as being able to branch in any direction. It starts some place, and reads each line of code line by line, but it may jump to another section of code. If you'd like an analogy then imagine you start a walk from your house and intend to return to your house. You walk one step at a time but you don't have to go a specific path around your block, you can choose to go to the store or somewhere else before returning home.
Blocks are designated in C++ by two symbols: { }. { signifies the start of a block of code and } signifies the end. An example:
{
int a = 1;
}
{
int b = 2;
}
This represents two different blocks of code, one which declares an integer "a" and sets it's value to 1 and another block of code which declares an integer "b" and sets it's value to 2. Note as well that blocks of code don't require a ; symbol at the end, the } symbols works in place of the ; to tell the compiler that the block of code is done.
There's several ways programs can be split in this way, and I'll go over 3 specific methods over the course of the next several lessons.
Estimated future lessons:
Lesson 3: Functions
Lesson 4: Programming logic
Lesson 5: Loops
Please feel free to leave comments if you find my lessons too hard, too easy, just right, stupid, or anything else
Wednesday, January 16
as provided to you by Bob @~~~ 8:08 PM
Subscribe to:
Post Comments (Atom)
1 \comm\e.nts:
Thanks for writing this.
Post a Comment