Lecture #10: Operators, Expressions, and Statements

 

Bob Pitha

bpitha@cs.ucsd.edu

http://www-cse.ucsd.edu/classes/fa99/cse5a

equality operators

==  !=

•      Test whether operands are equal or not equal

•      Return 1 or 0

precedence and associativity


 


equality operator issues

•      Don’t count on testing floating-point types with ==

–   why?

•   real numbers in the computer can differ by an insignificant amount

• == tests for exactly equal

–   so instead, test the difference between them

Text Box: if (abs (a – b) < 0.01){
   …
 


important!

•      Don’t confuse == with =


 


–   the worst part is, this is legal C code; it just doesn’t do what you (probably) want

•   the expression i = 0

–   it sets the value of i to 0
–   its value is 0

•   the expression i == 0

–   doesn’t set the value of anything
–   its value is 1 or 0 depending on whether i is 0

–   This can cause bugs that are very hard to find!

logical operators

&& ||

•      Logical AND and OR

(a && b) is true if both a and b are true

(a || b) is true if either a or b are true, or both.

•      Evaluation stops when truth value is known

     ((a != b) && (c <= d))

precedence and associativity


 

Text Box: expr1 && expr2 || expr3 && expr4
 


expressions

what is an expression?

•      Nobody has a good, clear definition.

•      A sequence of operators and operands that has a value and/or does something interesting

•      There are a bunch of types

how are expressions used?

•      As operands in larger expressions

Text Box: •i = j++
•5 * (fahrenheit – 32) / 9
•(a < b) || (c > d)
 


•      As parts of statements

Text Box: while (i > 0)
for (i = 0; i < 10; ++i)
 


side effects in expressions

•      Some operators have “side effects”

–   change the value of their operands

•      You must be careful not to combine side effects

–   no expression should modify the same object twice

Text Box: i = i++;
i = (j = 5) + j++;
 


we’ve finally finished chapter 2!

•      2.1 Variable Names

•      2.2 Data types

•      2.3 Constants

•      2.4 Declarations

•      2.5 Arithmetic operators

•      2.6 Relational operators

•      2.7 Type conversions

•      2.8 Increment/decrement operators

•      2.10 Assignment operators

•      2.12 Precedence

statements

what’s a statement?

•      A specification of some action to be performed

•      Executed sequentially

what kinds of statements do we have?

•      Expression statements

•      Compound statements

•      Selection statements

•      Iteration statements

•      Jump statements

what kinds of statements do we have?

•      Expression statements

•      Compound statements

•      Selection statements

•      Iteration statements

•      Jump statements

expression statements

•      Any expression can be a statement

–   just stick a semicolon (;) on the end

–   but they’re only evaluated for their side effects

•   the expression’s value is ignored

Text Box: i = 5;
i++;
++i;
celsius = 5 * (fahrenheit – 32) / 9;
 


expression statements

•      Since the value is ignored, expressions that only have a value are not meaningful statements

–   they’re not illegal, just useless

–   some compilers will warn you about them

•   the assumption is that you forgot something

Text Box: a + b;
32;
 


•      Special case: the null statement

–   an empty statement – just a semicolon

–   used when the syntax requires a statement, but you don’t want to do anything.

Text Box: ;
 


what kinds of statements do we have?

•      Expression statements

•      Compound statements

•      Selection statements

•      Iteration statements

•      Jump statements

compound statements

•      Also called statement blocks

•      Groups one or more statements into a unit

–   syntactically equivalent to a single statement

–   that means that wherever it’s legal to have a single statement, you can have a compound statement.

•      Can have declarations and initializations

–   but only at the beginning of a block

compound statement syntax

•      Statements surrounded by braces { }

–   no semicolon at the end

–   although the statements and declarations in the block use semicolons as normal

use of compound statements

•      Impossible to use C without using compound statements

•      We’ve already seen them a lot

Text Box: /*
 * HelloWorld: A simple C program
 */

#include <stdio.h>

int main (void)
{
printf (“Hello, world!\n”);
return 0;
}