The conditional expression is just as cryptic as the last two, but once again it can be very useful so it would pay you to understand it. It consists of three expressions within parentheses separated by a question mark and a colon. The expression prior to the question mark is evaluated to determine if it is not true, the expression following the colon is evaluated. The result of the evaluation is used for the assignment. The final result is identical to that of an "if" statement
with an "else" clause. This is illustrated by the second example in this group. The conditional expression has the added advantage of more compact code that will compile to fewer machine instructions in the final program.
The final two lines of this example program are given to illustrate a very compact way to assign the greater of two variables "a" or "b" to "c", and to assign the lessor of the same two variables to "c". Notice how efficient the code is in these two example.
To Be Cryptic Or Not To Be Cryptic
Several students of C have stated that they didn’t like these three cryptic constructs and that they would simply never use them. This would be fine if they never have to read anybody else’s program, or use any other programs within their own. I have found many functions that I wished to use within a program but needed a small modification to use it, requiring me to understand another person’s code. It would therefore be to your advantage to learn these new constructs, and use them. They will be used in the remainder of this tutorial, so you will be constantly
exposed to them. This has been a long chapter but it contained important material to get you started is using C. In the next chapter, we will go on to the building blocks of C, the functions. At that point, you will have enough of the basic materials to allow you to begin writing meaningful programs.
Google Search
Custom Search