|
|||||||||||||||||||||||||||
Home
Books
SimTel
C/C++
|
Home / C Tutorials The C language Branching and Looping by Marshall Brain Thursday, May 27, 1999
Published with kind permission of DevCentral If statements and while loops in C both rely on the idea of Boolean expressions, as they do in Pascal. In C, however, there is no Boolean type: You use plain integers instead. The integer value 0 in C is false, while any other integer value is true. Here is a simple translation from Pascal to C. First, the Pascal code:
if (x = y) and (j > k) then z := 1 else q := 10; The C translation looks very similar, but there are some important differences, which we will discuss next.
if ((x == y) && (j > k)) z = 1; else q = 10;Notice that = in Pascal became == in C. This is a very important difference, because C will accept a single = when you compile, but will behave differently when you run the program. The and in Pascal becomes && in C. Also note that z=1; in C has a semicolon, that C drops the then, and that the Boolean expression must be completely surrounded by parentheses. The following chart shows the translation of all boolean operators from Pascal to C:
The == sign is a problem because every now and then you may forget and type just =. Because integers replace Booleans, the following is legal in C: void main() { int a; printf("Enter a number:"); scanf("%d", &a); if (a) { blah blah blah } } if a is anything other than 0, the code that blah blah blah represents gets executed. Suppose you take the following Pascal statement: if a = b then and incorrectly translate it to C as: if (a = b) /* it SHOULD be "if (a==b)" */ In C, this statement means "Assign b to a, and then test a for its Boolean value." So if a becomes 0, the if statement is false; otherwise, it is true. The value of a changes as well. This is not the intended behavior (although this feature is useful when used correctly), so be careful with your = and == conversions. While statements are just as eay to translate. For example, the following Pascal code: while a < b do begin blah blah blah end; in C becomes: while (a < b) { blah blah blah } C also provides a "do-while" structure to replace Pascal's "repeat-until," as shown below: do { blah blah blah } while (a < b); The for loop in C is somewhat different from a Pascal for loop, because the C version is simply a shorthand way of expressing a while statement. For example, suppose you have the following code in C: x=1; while (x<10) { blah blah blah /* x++ is the same as saying x=x+1. It's an increment. */ x++; } You can convert this into a for loop as follows: for(x = 1; x < 10; x++) { blah blah blah } Note that the while loop contains an initialization step (x=1 ), a test step (x<10), and an increment step (x++ ). The for loop lets you put all three parts onto one line, but you can put anything into those three parts. For example, suppose you have the following loop: a=1; b=6; while (a < b) { a++; printf("%d\n",a); } You can place this into a for statement as well: for (a = 1,b = 6; a < b; a++,printf("%d\n",a)); It is confusing, but it is possible. The comma operator lets you separate several different statements in the initialization and increment sections of the for loop (but not in the test section). Many C programmers like to pack a lot of information into a single line of C code. I think it makes the code harder to understand, so I break it up. C Errors to avoid
for (x = 1; x < 10; x++); printf("%d\n", x); only prints out one value because of the semicolon after the for statement. |
This Article Introduction A simple program Branching/looping Arrays C Details Functions Libraries/makefiles Text files Pointers Parameters Dynamic structures Pointers and arrays Strings Operator precedence The command line Binary files
NetLinks
|
|||||||||||||||||||||||||
Next:
Arrays and the Bubble Sort
|