From a08ebf8d7a52a9e16c6b8c3d34270946f678d399 Mon Sep 17 00:00:00 2001
From: Lucas Rufkahr
The main function looks like:
+ std::cout << "Hello world" << '\n';
+ return 0;
+}
The int main() {
- std::cout << "Hello world" << '\n';
- return 0;
- }int main() { is the declaration of the main function denoting it returns type int. return 0; is the return statement for the function. It is returning the integer 0. This tells the operating system, OK Quit the program safely.
const in their type definition. These are not allowed to be modified after they are declared and must be initialized at declaration.
-
+ Just like in mathematics, operators let you perform functions on elements. We'll start by viewing arithmetic operators and assignment.
+
+ The assignment operator, denoted by = allows you to assign values to a variable. Assigning a variable with a value of a different type will give you a warning. This can be solved by casting the value being assigned to the same type as your variable: static_cast. Assignment also works from right to left. Values on the right of the assignment operator get assigned to the identifier on the left of the assignment operator.
+
+ Arithmetic operators allow you to do operations such as addition, subtraction, multiplication, division, and remainder division.
+
+ When dividing with numbers, it is important to know the type of numbers you are dividing. Division between integers will only return a whole number. For example 5 / 2 = 2. Suppose the 5 was a floating point type then, 5.0 / 2 = 2.5. It is important to know what types you are using when performing division.
+
+ When wanting to perform division and only return the remainder, you can use the modulus operator. For example, 5 / 2 = 2 whereas 5 % 2 = 1. 2 is the quotient and 1 is the remainder.
+
x++x--
+ ++x--x
+ x = x (operation) y:
+ x += yx -= yx *= yx /= yx %= y| 1st | ++ | + | + | + | + | + | + | + | + | Last | +
|---|---|---|---|---|---|---|---|---|---|---|
| (a) | +x++, x++ | +++x, --x | +! | +* / % | ++ - | +< > <= >= | +== != | +&& | +|| | += | +
+ An if statement will execute instructions if the condition evaluates to true. You can remember this as if something is true, then my program will do this. For example:
+
+ will execute "1 is equal to 1".int main() {
+ int x = 1;
+ if (x == 1) {
+ std::cout << x << " is equal to 1.\n";
+ }
+ return 0;
+}
+
+ An if-else statement will execute instructions if the condition is true and execute different instructions when the condition is false.
+ int main() {
+ int x = 1;
+ if (x == 1) {
+ std::cout << x << " is equal to 1.\n";
+ }
+ else {
+ std::cout << x << " is not equal to 1.\n";
+ }
+ return 0;
+}
+ One thing to note about these types of control statements, is that they can be nested to create a decision branch. A branch, like of a tree, splits off into many smaller branches. The more nested if-else statements you have, the more and more branches you add to your decision tree.
+