3.1. The Task aheadIn this chapter we look at the various ways that the control of flow
statements can be used in a C program, including some statements that
haven't been introduced so far. They are almost always used in conjunction
with logical expressions to select the next action. Examples of
logical expressions that have been seen already are some simple
ones used in 3.1.1. Logical expressions and Relational OperatorsAll of the examples we have used so far have deliberately avoided using complicated logical expressions in the control of flow statements. We have seen expressions like this
if(a != 100){...
and presumably you have formed the idea that C supports the concept of ‘true’ and ‘false’ for these relationships. In a way, it does, but in a way that differs from what is often expected. All of the relational operators shown in Table 3.1 are used to compare two operands in the way indicated. When the operands are arithmetic types, the usual arithmetic conversions are applied to them.
Be extra careful of the test for equality, Now, that usefully introduces the question ‘why?’. Why are both
valid? The answer is simple. C's concept of ‘true’ and
‘false’ boils down to simply ‘non-zero’ and ‘zero’,
respectively. Where we have seen expressions involving relational
operators used to control The relational operators work by comparing their operands and giving
zero for false and (remember this) one for true. The result is of type
#include <stdio.h>
#include <stdlib.h>
main(){
int i;
i = -10;
while(i <= 5){
printf("value of i is %d, ", i);
printf("i == 0 = %d, ", i==0 );
printf("i > -5 = %d\n", i > -5);
i++;
}
exit(EXIT_SUCCESS);
}Example 3.1Which produces this on its standard output: value of i is -10, i == 0 = 0, i > -5 = 0 value of i is -9, i == 0 = 0, i > -5 = 0 value of i is -8, i == 0 = 0, i > -5 = 0 value of i is -7, i == 0 = 0, i > -5 = 0 value of i is -6, i == 0 = 0, i > -5 = 0 value of i is -5, i == 0 = 0, i > -5 = 0 value of i is -4, i == 0 = 0, i > -5 = 1 value of i is -3, i == 0 = 0, i > -5 = 1 value of i is -2, i == 0 = 0, i > -5 = 1 value of i is -1, i == 0 = 0, i > -5 = 1 value of i is 0, i == 0 = 1, i > -5 = 1 value of i is 1, i == 0 = 0, i > -5 = 1 value of i is 2, i == 0 = 0, i > -5 = 1 value of i is 3, i == 0 = 0, i > -5 = 1 value of i is 4, i == 0 = 0, i > -5 = 1 value of i is 5, i == 0 = 0, i > -5 = 1 In this probably mistaken piece of code, what do you think happens? if(a = b)... The value of In all of the statements that test the value of an expression, the
We will look at each one in turn. |
The C BookThis book is published as a matter of historical interest. Please read the copyright and disclaimer information. GBdirect Ltd provides up-to-date training and consultancy in C, Embedded C, C++ and a wide range of other subjects based on open standards if you happen to be interested. |
|||||||||||||||
West Yorkshire Office
GBdirect Ltd
Training: 0800 651 0338 Please call between 0900 and 1700 (UK time) on Monday to Friday South East Regional Office
GBdirect Ltd
Training: 0800 651 0338 Please call between 0900 and 1700 (UK time) on Monday to Friday Please note: |