Programming for Engineers: Operators,, and 28 January 2011 31 January 2011 Programming for Engineers: Operators,, and Statem
The While Loop A test expression is at top of loop The program repeats loop as long as expression is true Example: while(shoe < 18.5) { foot=scale*shoe+adjust; printf( %10.1f %15.2f inches n shoe,foot); shoe=shoe+1.0; } The program must modify test variable, or the loop is endless. Programming for Engineers: Operators,, and Statem
The Structure of a While Loop Programming for Engineers: Operators,, and Statem
Arithmetic Operators Precedence and Associativity More Arithmetic Operators Statement Terminology Data object: This is a general term for a region of data storage that can be used to hold values. Lvalue: This a name or expression that identifies a particular data object. Rvalue: This can be a constant, variable, or any expression that can yield a value. Operand: This is an rvalue on which an operator operates. Example: shoe = shoe + 1.0; Programming for Engineers: Operators,, and Statem
Arithmetic Operators Arithmetic Operators Precedence and Associativity More Arithmetic Operators Assignment Operator: = shoe = shoe +1.0; Addition Operator: + shoe = shoe + 1.0; Subtraction Operator: - DownCount = DownCount - 1; Sign Operators: + or - Lower = -5+Count; Multiplication Operator: * foot = SCALE *shoe + ADJUST; Division Operator: / shoe = shoe/0.356; Programming for Engineers: Operators,, and Statem
Arithmetic Operators Precedence and Associativity More Arithmetic Operators Precedence and Associativity Precedence Operator Associativity Highest () Left to Right + (unary) Right to Left / Left to Right + (binary) Left to Right Lowest = Right to Left Programming for Engineers: Operators,, and Statem
Arithmetic Operators Precedence and Associativity More Arithmetic Operators Precedence and Associativity Example One Consider the following line of code: y=6*12+5*20; The computer makes the calculation as follows: y=6*12+5*20; y=72+5*20; y=72+100; y=172; Programming for Engineers: Operators,, and Statem
Arithmetic Operators Precedence and Associativity More Arithmetic Operators Precedence and Associativity Example Two Consider the following line of code: z=12/3*2; The computer makes the calculation as follows: z=12/3*2; z=4*2; z=8; Programming for Engineers: Operators,, and Statem
Arithmetic Operators Precedence and Associativity More Arithmetic Operators Precedence and Associativity Example Three Consider the following line of code: top = score = -(2 + 5) * 6 + (4 + 3 * (2 + 3)); The computer makes the calculation as follows: top = score = -(2 + 5) * 6 + (4 + 3 * (2 + 3)); top = score = -(7) * 6 + (4 + 3 * (2 + 3)); top = score = -(7) * 6 + (4 + 3 * (5)); top = score = -(7) * 6 + (4 + 15); top = score = -(7) * 6 + 19; top = score = -7 * 6 + 19; top = score = -42 + 19; top = score = -23; top = -23; Programming for Engineers: Operators,, and Statem
Arithmetic Operators Precedence and Associativity More Arithmetic Operators The sizeof() Operator sizeof( ) is operator and not a function. sizeof( ) returns number of bytes storage for operand. sizeof return value of type size t Example 1: size t n; n=sizeof(long double); // n is 10 Example 2: float pi = 3.14159; n=sizeof(pi); // n is 4 Programming for Engineers: Operators,, and Statem
Arithmetic Operators Precedence and Associativity More Arithmetic Operators More Arithmetic Operators Modulus operator: % remainder = number%10; Increment operator: ++ number = shoe++*5 number = ++shoe*5 Decrement operator: number = shoe *5 number = shoe*5 Programming for Engineers: Operators,, and Statem
Arithmetic Operators Precedence and Associativity More Arithmetic Operators Precedence and Associativity Revisited Precedence Operator Associativity Highest (), expr ± ± Left to Right +,, sizeof(),(typecast), ± ± expr Right to Left + + expr, expr, /, % Left to Right +, (binary) Left to Right Lowest = Right to Left Programming for Engineers: Operators,, and Statem
Arithmetic Operators Precedence and Associativity More Arithmetic Operators Confusing Code with Increment Operators Consider the following code num = 5; while(num < 21) printf( %10d %10d n,num,num*num++); There are three options for output displayed: 1. 2. 3. 5 25 6 25 6 30 Programming for Engineers: Operators,, and Statem
Arithmetic Operators Precedence and Associativity More Arithmetic Operators Confusing Code with Increment Operators Consider the following code num = 5; while(num < 21) printf( %10d %10d n,num,num*num++); There are three options for output displayed: 1. 2. 3. 5 25 6 25 6 30 Programming for Engineers: Operators,, and Statem
Arithmetic Operators Precedence and Associativity More Arithmetic Operators More Confusing Code with Increment Operators Consider the following code n = 3; y= n++ + n++ Here, there are two options for the variables after this code: 1. 2. y=6 n=5 y=7 n=5 Programming for Engineers: Operators,, and Statem
Arithmetic Operators Precedence and Associativity More Arithmetic Operators More Confusing Code with Increment Operators Consider the following code n = 3; y= n++ + n++ Here, there are two options for the variables after this code: 1. 2. y=6 n=5 y=7 n=5 Programming for Engineers: Operators,, and Statem
Arithmetic Operators Precedence and Associativity More Arithmetic Operators ANSI-C and ++ or Sometimes ANSI-C standard fails to define what the result should be so that compiler writer may increase efficiency. Dont use increment and decrement operators on a variable that is part of more than one argument of a function. Dont use increment or decrement operators on a variable that appears more than once in an expression. Programming for Engineers: Operators,, and Statem
Expression consists of a combination of operators and operands. Every expression has a value Examples: Expression Value 4 + 6 2 C = 3 + 8 11 5 > 3 1 6 + (C = 3 + 8) 17 Programming for Engineers: Operators,, and Statem
are the building blocks of program. Each simple statement is ended with a semicolon. Any expression is a statement. Examples: Nonsensible Sensible 8; x=25; 3+4; ++x; y=sqrt(x); x=6+(y=5); Programming for Engineers: Operators,, and Statem
Statement Types The assignment statement is the workhorse of many programs; it assigns a value to a variable. The function statement returns what ever the function does to a variable. The while statement is a structured statement. Programming for Engineers: Operators,, and Statem
Side Effects and Sequence Points A side effect is the modification of a data object or file. states = 50; A sequence point is a point in program execution at which all sides effects are evaluated before going on to the next step. Programming for Engineers: Operators,, and Statem
Full A full expression is one thats not a subexpression of a larger expression. A test condition is an example of a full expression. Programming for Engineers: Operators,, and Statem
Compund Two or more statements grouped together by braces which is called a block. Example: while(index ) { sam = 10* index +2; printf( sam = %d n,sam); } Programming for Engineers: Operators,, and Statem
char and short promoted to int if necessary. Float automatically promoted to double (K&R only). With operation of two types, both values are promoted to higher rank. The final result is converted to the variable being assigned a value. char and short are converted to int, and float is converted to double when passed as argument to function, though this may be overridden. Programming for Engineers: Operators,, and Statem
Rank from Highest Rank to Lowest Rank long double double float unsigned long long long long unsigned long long unsigned int int Programming for Engineers: Operators,, and Statem
The Cast Operator The typecast operator, (type), is used to demand a particular type conversion Consider the following code: int mice; mice=1.6+1.7; mice=(int) 1.6+ (int)1.7; Programming for Engineers: Operators,, and Statem
The Cast Operator The typecast operator, (type), is used to demand a particular type conver Consider the following code: int mice; mice=1.6+1.7;=3 mice=(int) 1.6+ (int)1.7;=2 Programming for Engineers: Operators,, and Statem
Three Parts of a Function 1. Prototype void pound(int n); 2. Invocation int times = 5; char ch =!; pound(times); pound(ch); 3. Declaration void pound(int n) { while (n > 0) printf( # ); printf( n); } Programming for Engineers: Operators,, and Statem
Programming for Engineers: Operators,, and Statem
Complete Chapter 5 Exercise 2 Complete Chapter 5 Exercise 8 Due Date: 09 February 2009 Programming for Engineers: Operators,, and Statem