Chapter 2

Flow of control

All computer programs have a flow of logic in them. This flow is enabled by your logic as well as some logical tools which are supplied along with the language’s compiler as part of the syntax of the language. Let us briefly go through these simple but very powerful logical tools.

Logical constructs

If

A conditional evaluator which evaluates a condition within parentheses and if the condition is satisfied executes the block of code within it.

                                                                if (x==0){

                                                                    printf("%s",string);

                                                                    x =2;}

If only one statement exists within the if construct, then there is no need to place open & close braces.

                                                                if (x==0)

                                                                 printf("%s",string);

If else

What if x evaluates to other than 0 and some task to be performed. We use ‘if else’ for this purpose.

                                                                if (x==0){

                                                                     printf("%s",string);

                                                                    x =2;}

else{

                                            printf("%d",x);

}

for…

A logical loop construct. A loop is a circular process that is carried out until a condition is satisfied. The syntax is as follows

                                                                        for(x=0;x<10;x++){

                                                                    printf("%d",x);}

The flow of control in the for…loop is as follows. The first parameter within parentheses initializes the loop and once the loop starts, will be subsequently ignored. The loop will be performed until the condition in the second parameter which is x<10. That is, the loop will stop once the value of x becomes 10. The third parameter increments the condition variable.

Once the loop starts, the first parameter is ignored subsequently. That is, after reaching the last statement within the loop, the control will go to the third parameter and increment the condition variable.

while…

A loop. While has a single evaluator within parentheses.

                                                                while (x<0){

                                                                …statements

                                                                    x--;

                                                                    }

 

The above loop will execute until x’s value is zero. Once x’s value becomes zero, the loop will exist.

Switch…

The switch statement evaluates a condition within parentheses. Within the switch construct the value of the given condition is evaluated through the case keyword.

switch(x){

case 0:

printf("%d",x);

break;

case 1:

pPrintf("%s","Hello");

 

default:

pPrintf("%s","Hi");

break;}

The break statement breaks control from the current case and exits the switch construct. The switch construct is very much similar to the if construct. That is, "case 0" is equal to saying "if (x = = 0)", "case 1" is equal to saying "if (x = = 1)"and so on. The "default case" occurs when none of the condition stated in the case’s are true.

 

goto…

The goto statement branches control from one block of code to another block which is labeled as some name.

If (x==0)

{

goto errnum;

}

else{

goto try;}

errnum:

x=2;

try:

x=3;

 

continue…

The continue statement is much like the break statement in that it causes a break in control. It is usually used within a for…loop.

 

Operators

Operators operate on an expression or a condition. x=y+z, is an expression where "+" & "=" are arithmetic operators. "If (x= =0)" is a condition where "= =" is a relational operator which tests for equality whereas the "=" is an assignment operator which assigns a value to another entity or variable. "! =" is a not equal to operator and works much like " = = ". The other arithmetic operators (+, -, *, /) behave in much the same way as in mathematics except for the "%" operator which is used in C & C++ for obtaining the modulus value. The division operator in C & C++ is the "/" operator. >, <, >=, <= (all function the same way as in mathematics).

Logical operators

When two or more expressions need to be compared and evaluated, logical operators are used.

AND        -       ;p;    &&            OR                -       ;p;         ||

If (x = = 0  &&  y = = 0) ||  (x = = 1  &&  y = = 1){

….statements}

The above if statement evaluates to if x is equal to zero and y is also equal to zero or if x is equal to I and y is also equal to 1, then execute the statements that follow. Only if both the conditions in the first instance is satisfied or only if both the conditions in the second instance is satisfied then execute the statements.

Increment operators

(++, --, +=, -=, /=, *=)

· ++ increments the value of a variable by 1.

· decrements the value of a variable by 1.

· a+= 2 is the same as a=a+2.

· a-=5 is the same as a=a-5.

· a/=7 is the same as a=a/7.

· a*=9 is the same as a=a*9.

Simple to use but you must take one important aspect about increment and decrement operators very seriously. That being where they are placed – before or after a variable name. That is, if you type cout<<++a; the value in a will be incremented first and then displayed and similarly, if you type cout<<a++; then the value in a will be displayed first and then incremented.

Ternary operators

a=(b>c)?b:c; A conditional operator which uses the same logic as the if else logical construct. The above statement can be understood as

if (b>c){

a=b;

}

else{

a=c;

}

Simple !

Bitwise operators

& | >> << ~ ^

AND OR RIGHT-SHIFT LEFT-SHIFT ONE’S COMPLEMENT OR

C allows many low-level interaction with the computer with the computer which, otherwise, can be done only through assembly language or lower. C supports the above bitwise operators which can be applied to variables of type int, unsigned, char.

&

The difference between the bitwise AND (&) and the logical AND (&&) is that, whereas the logical AND (&&) evaluates two expressions and produces a True or False, the bitwise AND will actually perform a logical AND for each of the bits in a variable or value when provided with the corresponding bits of a second variable or value. The most common application of the bitwise AND is for masking which we shall come across in programming with the mouse. If you are familiar with truth-table or logic gates as applied in electrical and electronic circuits, then these operators will be fairly easy to tackle.

>>

The right and left shift operators shift n bits to the left or right . a=a<<5 or a=a>>5 will shift 5 bits to the left or right.

~

One’s complement ~ inverts each bit in a byte i.e. 0 becomes 1 and 1 becomes 0 when complemented.

In exclusive OR ^, 1 ^ 0 produces 1, 0 ^ 1 produces 1, 0 ^ 0 produces 0, 1 ^ 1 produces 0.

In bitwise AND &, 0 & 0 = 0, 0 & 1 = 0, 1&0=0 and 1&1=1.

In bitwise OR | 0 & 0=0, 0&1=1, 1&0=1 and 1&1=1.

 

Register variables

As mentioned above, C provides us with many phantom, low-level tools. Through register variables C allows us to direct the compiler to place specific variables in registers, as opposed to the slower process of continually referencing main memory for the values. For more on registers, look into the chapter "Data structures". To utilize this feature in C, declare the variable as register int a.

Type conversion

If we declare a variable as int and another as float and try to assign the value of one to the other, there will be a datatype mismatch. To overcome this error, C provides typecasting i.e. casting a type to another type which is done as follows

int a=5;

float b=6;

a=(int)b;    // Variable b whose type is float is casted to int through typecasting

               

Qualifiers

short  int  a;

long  int  y;

unsigned int  z;

A short qualifier limits the size of variable a between –32768 to 32767. The long qualifier modifies the int variable y to contain between –2147,483,648 to 2147,483,647. The unsigned qualifier specifies that int z shall contain only positive values.

Octal & hexadecimals

The C compiler assumes that all numbers are decimal unless it is told otherwise. To represent an octal (base 8) or hexadecimal (base 16) number in C, we must inform the compiler that the value is not decimal. To do this, we place a leading 0 in front of the number. Hence 14 is represented as 014. If hexadecimals are to be used the 0 is still used but followed by "x" 0x14.

Pre-processors

As the name suggests, a pre-processor is a directive operator that is executed before processing i.e. during compile-time.

#define #include #if 0 #endif #undef #ifndef #ifdef

#define is a directive used to give a constant definition to a variable. #define ALT 1 will assign the value of 1 to ALT (the ALT key) and whenever ALT is used, the compiler will assume it to be 1.

#include is a directive which includes a filename that follows it. #include <dos.h> will include the "dos.h" header file onto your source code file. The header file is literally copied and pasted in place of the #include statement during compile time itself so that library functions used within your program can be executed.

#if 0 informs the compiler not to compile the code placed within the #if 0 and #endif block.

#ifndef compiles code only if the identifier referenced has not been previously defined.

#ifndef NULL

#define NULL ‘\0’

#endif

#undef will redefine a previously defined constant

 

#ifdef EOF

#undef EOF

#endif

#define EOF   "#"