|
|
Chapter 2conditions, if-statement and loopsThis chapter will teach you about the basic ways of controlling the flow of your program. On one hand these are conditions, which control the execution of certain parts of a program. On the other hand we have repetitions of code, again depending on the status of one or more conditions. Every decision inside your program is logically, it can be only true or false. Nothing in between. For your program it is the same if you want to check if one number is larger than another one, or if you want to test if just one single bit is set inside a variable.
For the sake of completeness let's start with the basic operations between numbers: "*", "/", "+", "-" and "%". The Modulo operator "%" is only valid in the case of integer divisions and returns the rest: 7%3 = 1. The division operator "/" often leads to errors as it returns an integer, if you divide one integer by another. Even worse, this result is always rounded down. E.g. 2/3 will result in 0! In Chapter 3 we will research this. Of course, if you want to define a new value you have to use the assignment operator "=", where always the result of the right hand side is assigned to the left hand side. For example, a=1/2; assigns the value of one half to a and increases the value of b by one. Since the increase or decrease of a value (by one) is very often used, there are shorter versions of this:
While "++" and "--" change the value by one, "+=" and "-=" are able to change the value of the variable by any number. Both kinds of operators are not only shorter in writing (write some hundred lines of code and you will agree to that :) ), but also produce faster code, because the compiler is able to improve this kind of calculations. [A detailed discussion of that is a bit too deep for the moment, but (hopefully) we are going to investigate speed and optimization in a later chapter.]
For comparison between numbers we have the following operators:
Take care that one single "=" is an assignment and not a comparison operator! So the expression stu = pida is false if pida equals 0, otherwise its true. Furthermore, after the execution of that expression, the value of stu equals that one of pida! Finally we have logical operators:
The result of such a comparison is always true or false. These are (as usual) represented by 1 (true) and 0 (false). The following program gives an impression on that: #include <iostream> First, realize a small detail at the declaration of the two integer's: we initialize them already with some value. This is possible but of course not necessary. When you run the program, you will see that the first expression is true and results in 1, while the second one is false and returns 0. In C++ (but not in C) therefore exists an own data type: bool. Variables of this type can only be in one of two states: true or false.
To test the result of such a comparison or any other logical value we have the if statement. It has the general form
The condition is always included in ( ) brackets, and followed by an expression. The expression may also be a block of commands, included in { } brackets. Optionally you may give an alternative expression (else expression2), which is executed if the condition is false. While this is very simple, the if-statement (together with loops) probably is the most important control-structure in every C/C++ program.
The for loop is your basic tool to repeat one or more commands. The syntax of the for loop is for (initialization; condition; increment) statement; Note that there are 3 parts inside the brackets, although these parts may be empty, there must be always the 2 semicolons dividing the parts. The part statement is executed as long as the condition is true and statement may also be a block included in { }. The condition is checked before every execution of the statement part. To provide the loop with well defined starting conditions one puts these usually in the initialization part. Since most for loops are incremental loops we find a third region especially for that. The increment part is executed after every completed loop. There have already been examples for for loops in the last chapter, including the exercises. One of them was for (i=1;i<=20;i++) cout << i << "\t" << i*(i-1) << "\n"; Here you find a standard initialization i=1. Before the first execution (this is the output of the running variable i, a tab, the product i*(i-1) and a new line character) the condition i<=20 is checked, this is (due to the initialization) true, therefore the statement is executed. After that we increase our counting variable i by one, check the condition again and execute the statement if condition is true. This is repeated as long as statement is true. The only fixed thing about for loops is the general syntax given above. Beside that you are free to check whatever condition you want, you can combine several initializations by coma and so on. Thus the for loop is very flexible, although i claim the upper given example as a standard usage of that loop.
Another kind of loop is the while loop which checks a condition and thereafter executes one or more commands. Then it checks the condition again and so on. The syntax in the same notation as above is: while (condition) statement; If you have understood the for loop hopefully there will be no problem with this and the next one, the do-while loop, so the translation of the previous example with the for loop on these both types of loops will be one of you exercises. The do-while loop in contrary to the while loop checks the condition after the execution of the statement. The syntax is: do statement; while (condition); This way the statement is at least once executed. Do i really have to write that statement again also includes blocks of code in { } brackets?
An example for the usage of the if condition and the while loop gives the following program. I admit, the program is not too useful. Beside that it is just an example demonstrating you the use of these important elements. #include
<iostream> Much like a for loop this example calculates the product of i*13 and names it scemo for the range i Î [0, 30[. If this product is in the range [100, 250] the program prints out i and scemo separated by a tab. |
email me: Daniel Schürmann |