Updated cpp.html
This commit is contained in:
107
notes/cpp.html
107
notes/cpp.html
@@ -6,7 +6,7 @@
|
||||
padding:0;
|
||||
}
|
||||
main {
|
||||
margin:2em;
|
||||
margin:0em 20em 0em 20em;
|
||||
}
|
||||
header {
|
||||
width: 100%;
|
||||
@@ -63,6 +63,13 @@
|
||||
<li><a href="#basics-operators">Operators</a>
|
||||
</ul>
|
||||
<li><a href="#control_flow">Control Flow</a></li>
|
||||
<ul>
|
||||
<li><a href="#control_flow-branching">Decision Branching</a></li>
|
||||
<li><a href="#control_flow-loops">Loops</a></li>
|
||||
<li><a href="#control_flow-switches">Switches</a></li>
|
||||
<li><a href="#control_flow-ternary">Ternary Operator</a></li>
|
||||
</ul>
|
||||
<li><a href="#other">Other Topics</a></li>
|
||||
</ul>
|
||||
<hr>
|
||||
<h2 id="basics">Basics</h2>
|
||||
@@ -278,5 +285,103 @@
|
||||
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>
|
||||
<p>
|
||||
Loops are good when you need to repeat a segment of instruction multiple times. There are a few different types of loops to be aware of.
|
||||
<ul>
|
||||
<li>While loop</li>
|
||||
<li>Do-While loop</li>
|
||||
<li>For loop</li>
|
||||
</ul>
|
||||
The while loop is used when the number of iterations is based on a condition that can change each time the program is run. Before the section of code within the loop body executes, first the condition must be checked. If the condition evaluates to false, the loop does not execute, similar to the if statement.
|
||||
<br><br>
|
||||
<code class="cblock"><pre>int main() {
|
||||
<!----------> int x = true;
|
||||
<!----------> while (x == true) {
|
||||
<!----------> std::cout << "Hello!";
|
||||
<!----------> }
|
||||
<!---------->}</pre></code><br>
|
||||
<br>
|
||||
This while loop will run infinitively many times because x will always equal true. There is no where in this program that the value of x changes so the loop will never leave the brackets of the while loop.<br>
|
||||
<br>
|
||||
The do-while loop will always evaluate once. This is because, unlike the while loop, the condition is checked after the main body executes.<br>
|
||||
<br>
|
||||
<code class="cblock"><pre>int main() {
|
||||
<!----------> int x = true;
|
||||
<!----------> do {
|
||||
<!----------> std::cout << "Hello!";
|
||||
<!----------> } while (x == false);
|
||||
<!---------->}</pre></code><br>
|
||||
<br>
|
||||
Note the slight change to the condition. This loop will repeat when x is false though as you can see, x is initialized to be true. However, unlike the while loop previously, this will output just one "Hello". You can see that the condition is placed after the <code>do { .. }</code> so the condition gets checked after it executes.<br>
|
||||
<br>
|
||||
The final type of loop is the for loop. This is used when the number of iterations of the loop is determine before the loop starts. You should not change the number of iterations after you create the loop.<br>
|
||||
<br>
|
||||
<code class="cblock"><pre>int main() {
|
||||
<!----------> int x = true;
|
||||
<!----------> for (int i = 0; i < 10; i++) {
|
||||
<!----------> std::cout << "Hello!";
|
||||
<!----------> }
|
||||
<!---------->}</pre></code><br>
|
||||
<br>
|
||||
You can see here, the for loop starts at <code>i = 0</code>, checks if <code>i < 10</code>, then outputs <code>Hello!</code>. Then <code>i++ </code>executes and i increases by 1. Then the loop repeats. The loop checks if <code>i < 10</code>. i is 1 now so this is true and the program outputs <code>Hello!</code>. This repeats until <code>i < 10</code> is false which is when <code>i = 10</code>. Pretty simple stuff.
|
||||
</p>
|
||||
Now suppose you want to leave a loop before the condition is false or skip an iteration quickly. Then you can use the <code>break</code> and <code>continue</code> statements. <code>break</code> will BREAK out of a loop causing is to cease executing. <code>continue</code> will SKIP everthing after the continue and start back at the top of the loop.
|
||||
|
||||
<h3 id="control_flow-switches">III. Switch</h3>
|
||||
<p>
|
||||
Suppose you have a large array of if-else statements.
|
||||
<code class="cblock"><pre>if (a == 1) {
|
||||
<!----------> ...
|
||||
<!---------->} else if (a == 2) {
|
||||
<!----------> ...
|
||||
<!---------->} else if (a == 3) {
|
||||
<!----------> ...
|
||||
<!---------->} else if (a == 4) {
|
||||
<!----------> ...
|
||||
<!---------->} else if (a == 5) {
|
||||
<!----------> ...
|
||||
<!---------->} else if (a == 6) {
|
||||
<!----------> ...
|
||||
<!---------->} else if (a == 7) {
|
||||
<!----------> ...
|
||||
<!---------->} else { ... }</pre></code>
|
||||
Instead of writing that, you could've used a switch:<br>
|
||||
<br>
|
||||
<code class="cblock"><pre>switch (a) {
|
||||
<!----------> case 1:
|
||||
<!----------> ...
|
||||
<!----------> break;
|
||||
<!----------> case 2:
|
||||
<!----------> ...
|
||||
<!----------> break;
|
||||
<!----------> case 3:
|
||||
<!----------> ...
|
||||
<!----------> break;
|
||||
<!----------> case 4:
|
||||
<!----------> ...
|
||||
<!----------> break;
|
||||
<!----------> case 5:
|
||||
<!----------> ...
|
||||
<!----------> break;
|
||||
<!----------> case 6:
|
||||
<!----------> ...
|
||||
<!----------> break;
|
||||
<!----------> case 7:
|
||||
<!----------> ...
|
||||
<!----------> break;
|
||||
<!----------> default:
|
||||
<!----------> ...
|
||||
<!----------> break;
|
||||
<!---------->}</pre></code><br>
|
||||
<br>
|
||||
One caveat is that the case must be a literal value. It cannot be another value. Another is that a <code>break;</code> statement must be added to the end of every case or the statements will waterfall down from the case they started at to the next break statement. This is a feature of switches.
|
||||
</p>
|
||||
<h3 id="control_flow-ternary">IV. Ternary Operator</h3>
|
||||
<p>
|
||||
The ternary operator lets you condense a simple if-else statement into one line.
|
||||
<code class="cblock"><pre>x = (a > b) ? 1 : 0;</code></pre>
|
||||
This means that if a is greater than b, assign x with 1. If a is not greater than b, assign x with 0. The ternary operator however needs the values in the if and else part to be of the same type. If they are not, the program will fail to compile.
|
||||
</p>
|
||||
<h2 id="other">Other Topics</h2>
|
||||
</main>
|
||||
</body>
|
||||
|
||||
Reference in New Issue
Block a user