Выбрать главу

If the expression evaluates to TRUE, Statement1 is executed, otherwise Statement2 is executed. The else keyword is optional and may be omitted. In the following example, if the value of x is greater than MAX then variable P is incremented by 1, otherwise it is decremented by 1:

if (x > MAX) P++;

else P--;

We can have more than one statement by enclosing the statements within curly brackets. For example:

if (x > MAX) {

 P++;

 Cnt = P;

 Sum = Sum + Cnt;

} else P--;

In this example, if x is greater than MAX then the three statements within the curly brackets are executed, otherwise the statement P-- is executed. Another example using the if statement is:

if (x > 0 && x < 10) {

 Total += Sum;

 Sum++;

} else {

 Total = 0;

 Sum = 0;

}

switch Statement  The switch statement is used when a number of conditions and different operations are performed if a condition is true. The syntax of the switch statement is:

switch (condition) {

case condition1:

 Statements;

break;

case condition2:

 Statements;

 break;

.....................

.....................

case condition:

 Statements;

 break;

default:

 Statements;

}

The switch statement functions as follows: First the condition is evaluated. The condition is then compared to condition1 and if a match is found, statements in that case block are evaluated and control jumps outside the switch statement when the break keyword is encountered. If a match is not found, condition is compared to condition2 and if a match is found, statements in that case block are evaluated and control jumps outside the switch statements, and so on. The default is optional, and statements following default are evaluated if the condition does not match any of the conditions specified after the case keywords.

In the following example, the value of variable Cnt is evaluated. If Cnt = 1, A is set to 1. If Cnt = 10, B is set to 1, and if Cnt = 100, C is set to 1. If Cnt is not equal to 1, 10, or 100 then D is set to 1:

switch (Cnt) {

case 1:

 A = 1;

 break;

case 10:

 B = 1;

 break;

case 100:

 C = 1;

 break;

default:

 D = 1;

}

Because white spaces are ignored in C language we can also write the preceding code as:

switch (Cnt) {

case 1:

 A = 1;

 break;

case 10:

 B = 1;

 break;

case 100:

 C = 1;

 break;

default:

 D = 1;

}

Example 3.1

In an experiment the relationship between X and Y values are found to be:

X Y

1 3.2

2 2.5

3 8.9

4 1.2

5 12.9

Write a switch statement that will return the Y value, given the X value.

Solution 3.1

The required switch statement is:

switch (X) {

case 1:

 Y = 3.2;

 break;

case 2:

 Y = 2.5;

 break;

case 3:

 Y = 8.9;

 break;

case 4:

 Y = 1.2;

 break;

case 5:

 Y = 12.9;

}

Iteration Statements

Iteration statements enable us to perform loops in a program, where part of a code must be repeated a number of times. In mikroC iteration can be performed in four ways. We will look at each one with examples:

• Using for statement

• Using while statement

• Using do statement

• Using goto statement

for Statement  The syntax of a for statement is:

for(initial expression; condition expression; increment expression) {

 Statements;

}

The initial expression sets the starting variable of the loop, and this variable is compared against the condition expression before entry into the loop. Statements inside the loop are executed repeatedly, and after each iteration the value of the increment expression is incremented. The iteration continues until the condition expression becomes false. An endless loop is formed if the condition expression is always true.

The following example shows how a loop can be set up to execute 10 times. In this example, variable i starts from 0 and increments by 1 at the end of each iteration. The loop terminates when i=10, in which case the condition i<10 becomes false. On exit from the loop, the value of i is 10:

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

 statements;

}

This loop could also be started by an initial expression with a nonzero value. Here, i starts with 1 and the loop terminates when i = 11. Thus, on exit from the loop, the value of i is 11:

for(i = 1; i <= 10; i++) {

 Statements;

}

The parameters of a for loop are all optional and can be omitted. If the condition expression is left out, it is assumed to be true. In the following example, an endless loop is formed where the condition expression is always true and the value of i starts with 0 and is incremented after each iteration:

/* Endless loop with incrementing i */

for(i=0; ; i++) {

 Statements;

}

In the following example of an endless loop all the parameters are omitted:

/* Example of endless loop */

for(; ;) {

 Statements;

}

In the following endless loop, i starts with 1 and is not incremented inside the loop:

/* Endless loop with i = 1 */