cs31 lecture 3

Mark the start of a comment line 13 rules for

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: ction returns 0, indicating it terminated without an The error error xWe use // to mark the start of a comment line 13 Rules for variable naming x A variable is referred to by its name used in the declaration variable statement, and is case-sensitive statement, Example: int main() { int foo; int foo = 42; foo } x Restrictions on the name: Starts by a letter, or _ Can use letters (upper or lower case), numbers, _ • Examples: foo, foo42, _myfoo, Examples: • Incorrect: 42foo, foo bar 14 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 } 15 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 16 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, 17 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 } 18 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 = foo + 1; foo = foo + 1 bar = ++foo + 1 is equivalent to foo = foo +...
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