Home

Video: Variables & types 1

Notes for this video are found below.

Types

Variable naming

Here are the rules for naming variables:

Arithmetic

Here are somewhat-unique math operators that work with integers:

Note that / works as you would expect with floating point numbers (e.g. 1.2 / 5.6 is about 0.214).

Precedence works the same as you would expect: * / % happen before + - so (5 + 6 % 4 - (3 + 4) / 2) equals 4 as you would expect. You can use parentheses to clarify the math.

Arithmetic shorthand

You can use the following shorthand for changing the values of variables. The shorthand form is shown, then the equivalent form is described in comments.

float a = 0.0;
int x = 0;
x++;            // same as: x = x + 1;
x--;            // same as: x = x - 1;
x += 2;         // same as: x = x + 2;
x -= 2;         // same as: x = x - 2;
x *= 2;         // same as: x = x * 2;
x /= 2;         // same as: x = x / 2;
    
a += 2.0;       // same as: a = a + 2.0;
// etc.

Boolean operators

Variables of type boolean have the following special operators:

boolean p = true;
boolean q = false;
boolean r;
r = !p;        // "!" means "not" or "opposite", so r == false
r = p || q;    // "||" means "or", so r == true
r = p && q;    // "&&" means "and", so r == false
r = q || (!p)  // r == false

Math functions

CSE 1211 material by Joshua Eckroth is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License. Source code for this website available at GitHub.