C Programming - Course #4
Operators and Statements (Part II)
ThinkIT! Win 50 FC...
Look around the course for ThinkITs and answer them. The one who has the most ThinkITs at the end of the course (the entire course, not just course #2) gets 50 FC. If more of you have the maximum amount of ThinkITs, I'll choose two of you with random.org
Please comment your answers and try not to copy others' answers. I can figure out who copied who.
Catching up...
So last time we talked about operators and statements. In this course we'll continue pointing out some C-language keywords and try to explain them before moving on to something new.
break statement
In the last course, we used the statement in order to stop a switch from executing further instructions when meeting our required case. However, break has more uses than this. break is actually used to STOP a repetitive statement from further executing.
- Code: Select all
for (i = 1;i<200;i++) {
if (i == 50) {
break; // this will stop the for at the 50th step
}
}
other_statement; // after break, this will be executed
- Code: Select all
while (some_condition) {
...
...
if (some_other_condition) {
break;
}
...
...
}
Therefore, you see, break can be used to quit a repetitive instruction before it's finished. You may find this useful in numerous cases.
Example problem: Given a series of integers (not 0), print the sum of all the integers until the first 0 is read. (add all read numbers until 0 is read)
continue statement
The continue statement is basically the opposite of break. continue jumps to the next step of the repetitive instruction preventing the following instructions to be executed. Why is this useful?
Example problems: Print the amount of odd and even integers in a non-0 int series.
goto statement
The goto statement is used to jump from a certain statement to another.
- Code: Select all
...
...
goto myDestination;
...
...
myDestination: some_statement;
some_other_statement;
...
...
This can be done the other way around:
- Code: Select all
myDestination: some_statement;
...
...
...
if (apples.color == GREEN) {
goto myDestination;
}
...
...
...
In general, the usage of goto is avoided because it can result in "spaghetti" code, which is a bad coding habit due to it's hard readability. When is it useful, though?
Well, it can be useful for all sorts of things, but the most useful thing I can think off right now is "fast exiting" a multi-level repetitive instruction.
- Code: Select all
while (some_condition) {
...
for (exp_1;exp_2;exp_3) {
...
while (some_other_condition) {
...
if (BIRDS_ARE_SINGING)
goto outOfLoops;
...
...
if (BIRDS_ARE_JUST_FLYING)
break;
...
...
}
// break will set the code here;
...
}
...
}
outOfLoops: next_statement; // goto jumps all the way out of the loop
rest_of_the_program;
...
Bitwise operators
For these types of operators, we require special types of operands:
- integers (signed/unsigned)
- characters
The full list of bitwise operators is:
- ~ (NOT) - negates all the bits of the number
- << - shift to left (below)
- >> - shift to right
- & (AND) - logic AND between the bits of the 2 numbers
- ^ (XOR) - logic XOR between the bits of the 2 numbers
- | (OR) - logic OR between the bits of the 2 numbers
- Code: Select all
1: a<<b;
2: a>>b;
In the first case (a<<b), all the bits in a's binary representation will move to the left by b places. The equivalent (numeric) result is a*(2^b), where '^' means 'to the power'.
In the second case (a>>b), all the bits in a's binary representation will move to the right by b places. The equivalent (numeric) result is a/(2^b), where '^' means 'to the power'.
Here is an example of bitwise shifting:

Example: Multiply a number by 10 without using the * operator.
Operator &
Bit-level AND operation:
105 = 0000 0000 0110 1001
186 = 0000 0000 1011 1010
- Code: Select all
105 & 186 = 0000 0000 0010 1000 = 40;
Operator |
Bit-level OR operation:
- Code: Select all
105 | 186 = 0000 0000 1111 1011 = 251;
Operator ^
Bit-level XOR operation:
Logical XOR is an OR operation where identical bits result in a false output.

Type conversion operators
- explicit - special operator: (TYPE)expression;
- implicit - by the compiler
Implicit data conversion works like this:
- Code: Select all
integer_variable = float_variable; // the float is approximated to the closest integer
float_variable = integer_variable; // the integer is represented as a floating number (integer.0000)
Explicit data conversion:
- Code: Select all
int k, m;
float average;
average = (float)(k+m)/2; // arithmatic average of k and m
In the end...
This sums up everything about operators and statements. There were no ThinkITs this course, but stay tuned!






