This chapter introduces students to the arithmetic operators, the concept of operator precedence, and the meaning of the terms expressions and statements. The objectives important to this chapter include:
Concepts:This chapter introduces the while loop. The structure of this loop is as follows: while (test condition is true) { do the following statements } If the test condition is false to begin with, the entire while loop is skipped. The statements placed inside the braces of the while loop represent a block (as in a block of code). C uses operators to represent arithmetic operations. The symbols assigned to operators are the following: = assignment + addition - subtraction * multiplication / division There is an operator precedence in C. Operators in order of descending precedence are: () + - (when used as unary operators) * / + - (when used as binary operators) = C features data objects, lvalues, rvalues, and operands. A data object is a general term for a region of data storage that can be used to hold values. The lvalue (for left value) is a name or expression that identifies a particular object. The lvalue is found on the left side of the assignment operator. The rvalue refers to values that can be assigned to modifiable lvalues. This means that rvalues are the values that get assigned to the lvalues. The rvalue is found on the right side of the assignment operator. Finally, operands are what operators operate on. There are binary and unary operators. Binary operators require two operands, as found in addition and subtraction. Unary operators require a single operand. A unary operator is used to change the algebraic sign of a value. Besides the five operators listed above, other C operators include:
The increment and decrement operators have a very high precedence of association. Only parentheses are higher. There are two forms of these two operators. These are:
With prefix, the variable is incremented (or decremented) before an operation takes place. With postfix, the variable is incremented (or decremented) after an operation takes place. Students tend to be confused between statements and expressions. An expression consists of a combination of operators and operands, whereas a statement is a complete C instruction with necessary punctuation. Three types of statements are the assignment statement, the function statement, and the while statement. The while statement is also known as a structured statement. C produces side effects, which are the modifications of a data object or file. An example is the assignment statement: states = 50; A compound statement is two or more statements grouped together, by enclosing them in braces. This group of statements is known as a block. Type conversions can be handled automatically or by the C programmer. The automatic rules are (in short form):
The C programmer can cast a variable to a designated type by using a cast operator. The syntax is: (data_type) variable_name Function statements can include those with no arguments and those with arguments. When arguments are passed to a function, the function heading must include a formal parameter, as in: void pound(int n) /* Where n is the name of the parameter */ /* and int represents the type */ In addition to the function heading, the function prototype should include a type. The prototype for the function above is: void pound(int n); Before leaving this chapter, students should understand the difference between a function prototype, a function call, and a called function. A prototype is a pattern for the function that is handed to the compiler early in the program. A call is when you use the function's name to get something done. A called function is one that runs when it is called by something else (usually another function.) |