cs31 lecture 17

Bar 13 instructions in a c program x each instruction

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: use letters (upper or lower case)
, numbers, _ • Examples: foo, foo42, _myfoo, Examples: • Incorrect: 42foo, foo bar 13 Instructions in a C++ program x Each instruction ends by ; Each x Usually, one instruction per line (good programming habit)
 x A variable declaration must occur before its first use x Standard mathematical operators are supported Example: int main()
 { int bar = 42; int int foo; int foo = bar + 12 – (bar * 2)
; foo return 0; return } 14 More on assignments x Syntax: <lhs> = <rhs>; x <lhs> must be a variable, storing the result of the <lhs> expression <rhs> expression x <rhs> is an expression, made of variables, function calls, <rhs> and arithmetic operators and x Examples: foo = bar * 2; // ok 12 = 12; // incorrect 15 List of assignment operators x = (corresponds to := in our algorithmic language)
 x +=, -=, *=, /= <lhs> += <rhs> is evaluated as <lhs> = <lhs> + <rhs> Examples: foo += 2; // equivalent to foo = foo + 2; foo *= bar; // equivalent to foo = foo * bar; x &=, |=, ^=, %=, … x Roughly, any binary operator op has a op= form Roughly, 16 Example int main()
 { int foo; int int bar = 42; int foo = bar * 2; foo foo /= 2; foo /// value of foo here: 42 / return 0; return } 17 List of unary operators x ++ to increment by 1 and assign the result to the variable Example: foo++ or ++foo foo++ is equivalent to foo = foo + 1 bar = foo++ + 1 is equivalent to bar = fo...
View Full Document

This note was uploaded on 04/03/2014 for the course CS 31 taught by Professor Melkanoff during the Fall '00 term at UCLA.

Ask a homework question - tutors are online