<<< Program structure and organization | Index | Expressions >>> |
// compute an area of a rectangle: int length = 20; // the simplest expression: a literal (here, 20) // (here used to initialize a variable) int width = 40; int area = length*width; // a multiplication int average = (length+width)/2; // addition and division
The usual rules of precedence apply:
a*b+c/d means (a*b)+(c/d) and not a*(b+c)/d.
If in doubt, parenthesize. If complicated, parenthesize.
Don't write absurdly complicated expressions:
a*b+c/d*(e-f/g)/h+7 // too complicated
Choose meaningful names!
<<< Program structure and organization | Index | Expressions >>> |