Updated cPP.html
This commit is contained in:
185
notes/cpp.html
185
notes/cpp.html
@@ -1,38 +1,45 @@
|
||||
<!DOCTYPE html>
|
||||
<head>
|
||||
<style>
|
||||
body {
|
||||
margin:0;
|
||||
padding:0;
|
||||
}
|
||||
main {
|
||||
margin:2em;
|
||||
}
|
||||
header {
|
||||
width: 100%;
|
||||
background-color:black;
|
||||
padding:1em;
|
||||
}
|
||||
header * {
|
||||
body {
|
||||
margin:0;
|
||||
padding:0;
|
||||
}
|
||||
main {
|
||||
margin:2em;
|
||||
}
|
||||
header {
|
||||
width: 100%;
|
||||
background-color:black;
|
||||
padding:1em;
|
||||
}
|
||||
header * {
|
||||
display: inline;
|
||||
color:white;
|
||||
}
|
||||
color:white;
|
||||
}
|
||||
|
||||
nav {
|
||||
display:block
|
||||
display:block
|
||||
}
|
||||
nav * {
|
||||
padding-right: 1em;
|
||||
padding-right: 1em;
|
||||
}
|
||||
code, pre {
|
||||
whitespace: normal;
|
||||
}
|
||||
.cblock {
|
||||
padding-left: 1em;
|
||||
padding-right: 1em;
|
||||
display: inline-block;
|
||||
margin-left: 5em;
|
||||
background-color:lightgray;
|
||||
}
|
||||
table,td, th {
|
||||
text-align:left;
|
||||
width: 50em;
|
||||
border: 1px solid black;
|
||||
border-collapse: collapse;
|
||||
}
|
||||
code, pre {
|
||||
whitespace: normal;
|
||||
}
|
||||
.cblock {
|
||||
padding-left: 1em;
|
||||
padding-right: 1em;
|
||||
display: inline-block;
|
||||
margin-left: 5em;
|
||||
background-color:lightgray;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
@@ -67,9 +74,9 @@
|
||||
<br>
|
||||
The main function looks like:
|
||||
<code class="cblock"><pre>int main() {
|
||||
<!-----> std::cout << "Hello world" << '\n';
|
||||
<!-----> return 0;
|
||||
<!----->}</pre></code>
|
||||
<!---------> std::cout << "Hello world" << '\n';
|
||||
<!---------> return 0;
|
||||
<!--------->}</pre></code>
|
||||
The <code>int main() {</code> is the declaration of the main function denoting it returns type int. <code>return 0;</code> is the return statement for the function. It is returning the integer 0. This tells the operating system, OK Quit the program safely.
|
||||
</p>
|
||||
<h3 id="basics-variables">II. Variables</h3>
|
||||
@@ -153,7 +160,123 @@
|
||||
<br>
|
||||
Constant types are variables identified by <code>const</code> in their type definition. These are not allowed to be modified after they are declared and must be initialized at declaration.
|
||||
</p>
|
||||
<h3 id="#basics-operators">Operators</h3>
|
||||
<h3 id="#basics-operators">III. Operators</h3>
|
||||
<p>
|
||||
Just like in mathematics, operators let you perform functions on elements. We'll start by viewing arithmetic operators and assignment.<br>
|
||||
<br>
|
||||
The assignment operator, denoted by <code>=</code> 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: <code>static_cast<type>(var)</code>. 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.<br>
|
||||
<br>
|
||||
Arithmetic operators allow you to do operations such as addition, subtraction, multiplication, division, and remainder division.<br>
|
||||
<br>
|
||||
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 <code>5 / 2 = 2</code>. Suppose the 5 was a floating point type then, <code>5.0 / 2 = 2.5</code>. It is important to know what types you are using when performing division.<br>
|
||||
<br>
|
||||
When wanting to perform division and only return the remainder, you can use the modulus operator. For example, <code>5 / 2 = 2</code> whereas <code>5 % 2 = 1</code>. 2 is the quotient and 1 is the remainder.<br>
|
||||
<ul>
|
||||
<li>+ Addition</li>
|
||||
<li>- Subtraction</li>
|
||||
<li>* Multiplication</li>
|
||||
<li>/ Division</li>
|
||||
<li>% Modulus</li>
|
||||
</ul>
|
||||
There are also ways to quickly perform arithmetic operations using "fast operators".
|
||||
<ul>
|
||||
<li>
|
||||
Post increment/decrement:<br>
|
||||
This will return the current value and then perform the operation.<br>
|
||||
<code>x++</code><br>
|
||||
<code>x--</code>
|
||||
</li>
|
||||
<li>
|
||||
Pre increment/decrement:<br>
|
||||
This will perform the operation, then return the current value.<br>
|
||||
<code>++x</code><br>
|
||||
<code>--x</code>
|
||||
</li>
|
||||
<br>
|
||||
The following is short for <code>x = x (operation) y</code>:
|
||||
<li><code>x += y</code></li>
|
||||
<li><code>x -= y</code></li>
|
||||
<li><code>x *= y</code></li>
|
||||
<li><code>x /= y</code></li>
|
||||
<li><code>x %= y</code></li>
|
||||
</ul>
|
||||
|
||||
Next we'll approach the relational and logical operators. These allow you to evaluate comparisons between two or more variables.<br>
|
||||
<br>
|
||||
Relational Operators
|
||||
<ul>
|
||||
<li>Less than: <</li>
|
||||
<li>Greater than: ></li>
|
||||
<li>Less than or equal to: <=</li>
|
||||
<li>Greater than or equal to: >=</li>
|
||||
<li>Is equal to: ==</li>
|
||||
<li>Is not equal to: !=</li>
|
||||
</ul>
|
||||
Logical Operators
|
||||
<ul>
|
||||
<li>AND: &&</li>
|
||||
<li>OR: ||</li>
|
||||
<li>NOT: !</li>
|
||||
</ul>
|
||||
When using multiple operators, it is important to know that operators have a precedence, just like in mathematics.<br>
|
||||
<br>
|
||||
Operators with the same precedence are evaluated in the statement from left to right.
|
||||
<table>
|
||||
|
||||
<tr>
|
||||
<th>1st</th>
|
||||
<th></th>
|
||||
<th></th>
|
||||
<th></th>
|
||||
<th></th>
|
||||
<th></th>
|
||||
<th></th>
|
||||
<th></th>
|
||||
<th></th>
|
||||
<th></th>
|
||||
<th>Last</th>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>(a)</td>
|
||||
<td>x++, x++</td>
|
||||
<td>++x, --x</td>
|
||||
<td>!</td>
|
||||
<td>* / %</td>
|
||||
<td>+ -</td>
|
||||
<td>< > <= >=</td>
|
||||
<td>== !=</td>
|
||||
<td>&&</td>
|
||||
<td>||</td>
|
||||
<td>=</td>
|
||||
</tr>
|
||||
</table>
|
||||
</p>
|
||||
<h2 id="control_flow">Control Flow</h2>
|
||||
<h3 id="control_flow-branchinig">I. Decision Branching</h3>
|
||||
<p>
|
||||
An <code>if</code> 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:
|
||||
<code class="cblock"><pre>int main() {
|
||||
<!---------> int x = 1;
|
||||
<!---------> if (x == 1) {
|
||||
<!---------> std::cout << x << " is equal to 1.\n";
|
||||
<!---------> }
|
||||
<!---------> return 0;
|
||||
<!--------->}</pre></code>
|
||||
will execute "1 is equal to 1".<br>
|
||||
<br>
|
||||
An <code>if-else</code> statement will execute instructions if the condition is true and execute different instructions when the condition is false.<br><br>
|
||||
<code class="cblock"><pre>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;
|
||||
<!--------->}</pre></code><br><br>
|
||||
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.
|
||||
</p>
|
||||
<h3 id="control_flow-loops">II. Loops</h3>
|
||||
</main>
|
||||
</body>
|
||||
|
||||
Reference in New Issue
Block a user