cs31 lecture 17

Of a value example foo x examples int foo 42 18

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: o + 1; foo = foo + 1 bar = ++foo + 1 is equivalent to foo = foo + 1; bar = foo + 1 x -- to decrement by 1 and assign the result to the variable Example: foo-- or --foo x - to take the opposite of a value Example: -foo x Examples: int foo = 42; 18 Summary x A variable must be declared before it is used x Declarations can occur (almost)
 anywhere in the program x Good habit: put all declarations at the beginning x Each instruction ends by ; x Good habit: one instruction per line x Variable names are case-sensitive x Assignment of value can be done at declaration x Be careful to the variable type! 19 Mixing types x What about using floats and ints in the same expression? Example: foo = 12 + 24.2; x The type of the expression depends on the type of the The <lhs> <lhs> Example: int foo = 12 + 24.2; // foo has value 36 float foo = 12 + 24.2; // foo has value 36.2 x Be careful: floats are truncated to int, not rounded Example: int foo = 12 + 24.9; // foo has value 36 20 Integer division x Be careful: if division is used on integer data (variable or Be value)
, then integer division is used value)
, x Example:...
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