{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

# Lec3 - Lecture 3 Arithmetic and Casting PIC 10A Todd...

This preview shows pages 1–4. Sign up to view the full content.

1 Lecture 3: Arithmetic and Casting Lecture 3: Arithmetic and Casting Arithmetic and Casting PIC 10A PIC 10A Todd Todd Wittman Wittman Sec 2.4 : Constants Sometimes we want a variable that does not vary. Declare it with the reserved word const . The value of a constant cannot change. You’ll get an error if you try. Generally use capital letters for constants. Declare at the very top with the variables. const double PI = 3.14159; const int NUMBER_OF_HOBBITS = 4;

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
2 Sec 2.5 : Basic Arithmetic x++; or ++x; Add one ++ x = 9 % 2; Mod % x = 3 / 2; Division / x = 3* -2; Multiplication * x = -2 – 5; Subtraction - x = 2 + 3; Addition + Example Code Operation Symbol Assignment In C++, the = sign means assignment not an equation. x = y + z; Sometimes we put a variable on both sides of the = sign. x = x + 2; Add the values in y and z, then store the result in x. Take the old value of x and add 2, then this becomes the new value of x.
3 Example: Circles Compute the area and circumference of a circle, given the radius. const double PI = 3.14159; double radius; double area; double circumference; cout << "Enter the radius of the circle: "; cin >> radius; area = PI*radius*radius; circumference = 2*PI*radius; cout << "The area is " << area << ".\nThe circumference is " << circumference << ".\n\n"; Order of Operations * and / get preference over + and To change the order use parantheses.

This preview has intentionally blurred sections. Sign up to view the full version.

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

{[ snackBarMessage ]}