updated hugo site
This commit is contained in:
@@ -2,8 +2,8 @@
|
|||||||
date = '{{ .Date }}'
|
date = '{{ .Date }}'
|
||||||
draft = true
|
draft = true
|
||||||
title = '{{ replace .File.ContentBaseName "-" " " | title }}'
|
title = '{{ replace .File.ContentBaseName "-" " " | title }}'
|
||||||
layout = 'note'
|
layout = ''
|
||||||
type = 'notes'
|
type = 'book'
|
||||||
tags = 'cpp'
|
tags = 'cpp'
|
||||||
chapter = ''
|
chapter = ''
|
||||||
+++
|
+++
|
||||||
149
content/cpp/basics.md
Normal file
149
content/cpp/basics.md
Normal file
@@ -0,0 +1,149 @@
|
|||||||
|
+++
|
||||||
|
date = '2026-02-27T23:57:42-06:00'
|
||||||
|
draft = true
|
||||||
|
title = 'Basics'
|
||||||
|
layout = 'chapter'
|
||||||
|
type = 'book'
|
||||||
|
tags = 'cpp'
|
||||||
|
chapter = '01'
|
||||||
|
+++
|
||||||
|
|
||||||
|
## I. Introduction
|
||||||
|
|
||||||
|
A C++ program generally consists of preprocessor directives and the main function.
|
||||||
|
|
||||||
|
A preprocessor directive tells the C++ preprocessor what to do before compiling. You can use this to include files, create macros, and determine compiling based on conditions. For example, include a file using `#include` This will include the iostream file from the C++ standard library.
|
||||||
|
|
||||||
|
The main function looks like:
|
||||||
|
|
||||||
|
```c++
|
||||||
|
int main() {
|
||||||
|
std::cout << "Hello world" << '\n';
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
The `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.
|
||||||
|
|
||||||
|
## II. Variables
|
||||||
|
|
||||||
|
A variable lets you allocate some memory in the computer and use it to store values. You can also recall the values for later. Variables contain a memory address and an identifier. An identifier can be \*almost\* anything you wish it to be.
|
||||||
|
|
||||||
|
Variables naming rules:
|
||||||
|
|
||||||
|
- Cannot start with a number.
|
||||||
|
- Can only contain alphanumeric characters and underscores.
|
||||||
|
- Cannot contain any reserved keywords [(see here)](https://en.cppreference.com/w/cpp/keyword.html).
|
||||||
|
- If starting with an underscore, it can only start with one.
|
||||||
|
|
||||||
|
All variables have denoted types and they refer to what kind of variable it is. All variables also have a bit size and this denotes the range of values that can be stored.
|
||||||
|
|
||||||
|
Variable types:
|
||||||
|
|
||||||
|
- Integers:
|
||||||
|
- short int:
|
||||||
|
- Size: 2 bytes.
|
||||||
|
- Range: -32,768 to 32,767
|
||||||
|
- int:
|
||||||
|
- Size: 4 bytes.
|
||||||
|
- Range: -2 billion to 2 billion
|
||||||
|
- short:
|
||||||
|
- Size: 8 bytes.
|
||||||
|
|
||||||
|
- Floats:
|
||||||
|
- float:
|
||||||
|
- Size: 4 bytes.
|
||||||
|
- 6 significant figures.
|
||||||
|
- double:
|
||||||
|
- Size: 8 bytes.
|
||||||
|
- 15 significant figures.
|
||||||
|
- long double:
|
||||||
|
- Size: 16 bytes.
|
||||||
|
- 19 significant figures.
|
||||||
|
|
||||||
|
- Character:
|
||||||
|
- char
|
||||||
|
- Stores an integer value representative of an "ASCII" character.
|
||||||
|
- Size: 1 byte.
|
||||||
|
|
||||||
|
- String:
|
||||||
|
- string
|
||||||
|
- Stores an indexable array of characters.
|
||||||
|
- Variable creation: `std::string identifier = "string literal";`
|
||||||
|
|
||||||
|
- Boolean:
|
||||||
|
- bool
|
||||||
|
- Stores an integer value of 0 or 1 representative of false or true respectively.
|
||||||
|
- Size: 1 byte
|
||||||
|
|
||||||
|
|
||||||
|
Variables are always declared before they are initialized. This means you must determine the type and the identifier before you store any values into it.
|
||||||
|
|
||||||
|
```c++
|
||||||
|
int main() {
|
||||||
|
int a; // Declaration
|
||||||
|
a = 1; // Initialization
|
||||||
|
int b = 2; // Declaration then initialization
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
Constant types are variables identified by `const` in their type definition. These are not allowed to be modified after they are declared and must be initialized at declaration.
|
||||||
|
|
||||||
|
## III. Operators
|
||||||
|
|
||||||
|
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(var)`. 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.
|
||||||
|
|
||||||
|
- \+ Addition
|
||||||
|
- \- Subtraction
|
||||||
|
- \* Multiplication
|
||||||
|
- / Division
|
||||||
|
- % Modulus
|
||||||
|
|
||||||
|
There are also ways to quickly perform arithmetic operations using "fast operators".
|
||||||
|
|
||||||
|
- Post increment/decrement:
|
||||||
|
- This will return the current value and then perform the operation.
|
||||||
|
- `x++`
|
||||||
|
- `x--`
|
||||||
|
- Pre increment/decrement:
|
||||||
|
- This will perform the operation, then return the current value.
|
||||||
|
- `++x`
|
||||||
|
- `--x`
|
||||||
|
|
||||||
|
The following is short for `x = x (operation) y`:
|
||||||
|
- `x += y`
|
||||||
|
- `x -= y`
|
||||||
|
- `x *= y`
|
||||||
|
- `x /= y`
|
||||||
|
- `x %= y`
|
||||||
|
|
||||||
|
Next we'll approach the relational and logical operators. These allow you to evaluate comparisons between two or more variables.
|
||||||
|
|
||||||
|
Relational Operators:
|
||||||
|
- Less than: <
|
||||||
|
- Greater than: >
|
||||||
|
- Less than or equal to: <=
|
||||||
|
- Greater than or equal to: >=
|
||||||
|
- Is equal to: ==
|
||||||
|
- Is not equal to: !=
|
||||||
|
|
||||||
|
Logical Operators:
|
||||||
|
- AND: &&
|
||||||
|
- OR: ||
|
||||||
|
- NOT: !
|
||||||
|
|
||||||
|
When using multiple operators, it is important to know that operators have a precedence, just like in mathematics. Operators with the same precedence are evaluated in the statement from left to right.
|
||||||
|
|Precedence|Operator(s)|
|
||||||
|
|----------|-----------|
|
||||||
|
|1 |Parenthesis|
|
||||||
|
|2 |x++, x-- |
|
||||||
|
|3 |++x, --x |
|
||||||
|
|4 |! |
|
||||||
|
|5 |* / % |
|
||||||
|
|6 |+ - |
|
||||||
|
|7 |< > <= >= |
|
||||||
|
|8 |== != |
|
||||||
|
|9 |&& |
|
||||||
|
|10 |\|\| |
|
||||||
|
|11 |= |
|
||||||
|
|
||||||
@@ -1,21 +0,0 @@
|
|||||||
+++
|
|
||||||
date = '2026-02-27T22:22:10-06:00'
|
|
||||||
draft = true
|
|
||||||
title = 'Basics'
|
|
||||||
layout = 'note'
|
|
||||||
type = 'notes'
|
|
||||||
tags = 'cpp'
|
|
||||||
chapter = '01'
|
|
||||||
+++
|
|
||||||
|
|
||||||
This is the basics:
|
|
||||||
|
|
||||||
```c++
|
|
||||||
// This is a code block
|
|
||||||
|
|
||||||
int main() {
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
}
|
|
||||||
```
|
|
||||||
@@ -1,5 +0,0 @@
|
|||||||
+++
|
|
||||||
date = '2026-02-27T22:44:20-06:00'
|
|
||||||
draft = true
|
|
||||||
title = 'Contents'
|
|
||||||
+++
|
|
||||||
11
content/cpp/frontcover.md
Normal file
11
content/cpp/frontcover.md
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
+++
|
||||||
|
date = '2026-02-27T22:44:20-06:00'
|
||||||
|
draft = false
|
||||||
|
title = 'CPP Notes Front Cover'
|
||||||
|
type = 'book'
|
||||||
|
layout = 'frontcover'
|
||||||
|
tags = 'cpp'
|
||||||
|
chapterno = '00'
|
||||||
|
+++
|
||||||
|
|
||||||
|
This is the front cover.
|
||||||
@@ -1,11 +0,0 @@
|
|||||||
+++
|
|
||||||
date = '2026-02-27T22:23:00-06:00'
|
|
||||||
draft = true
|
|
||||||
title = 'Functions'
|
|
||||||
layout = 'note'
|
|
||||||
type = 'notes'
|
|
||||||
tags = 'cpp'
|
|
||||||
chapter = '02'
|
|
||||||
+++
|
|
||||||
|
|
||||||
This is all about functions
|
|
||||||
@@ -5,4 +5,4 @@ title = 'My New Hugo Site'
|
|||||||
[contentTypes.'text/markdown']
|
[contentTypes.'text/markdown']
|
||||||
[taxonomies]
|
[taxonomies]
|
||||||
tag = 'tags'
|
tag = 'tags'
|
||||||
chapter = 'chapter'
|
chapterno = 'chapter_no'
|
||||||
|
|||||||
@@ -4,7 +4,7 @@
|
|||||||
<head>
|
<head>
|
||||||
<meta charset="utf-8">
|
<meta charset="utf-8">
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||||
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bulma@1.0.4/css/bulma.min.css">
|
<!--<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bulma@1.0.4/css/bulma.min.css">-->
|
||||||
<title></title>
|
<title></title>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
|
|||||||
BIN
layouts/book/.frontcover.html.swp
Normal file
BIN
layouts/book/.frontcover.html.swp
Normal file
Binary file not shown.
@@ -1,4 +1,5 @@
|
|||||||
{{ define "main" }}
|
{{ define "main" }}
|
||||||
|
<h1>{{.Title}}</h1>
|
||||||
{{ .TableOfContents }}
|
{{ .TableOfContents }}
|
||||||
{{ .Content }}
|
{{ .Content }}
|
||||||
{{ end }}
|
{{ end }}
|
||||||
15
layouts/book/frontcover.html
Normal file
15
layouts/book/frontcover.html
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
{{ define "main" }}
|
||||||
|
<h1>{{ .Title }}</h1>
|
||||||
|
|
||||||
|
{{ .Content }}
|
||||||
|
|
||||||
|
{{ $chapters := where .Site.AllPages "Params.tags" .Params.tags }}
|
||||||
|
|
||||||
|
{{ range sort $chapters "Params.chapterno" "asc" }}
|
||||||
|
{{ if ne .Params.chapterno "00" }}
|
||||||
|
<li><a href="{{ .Permalink }}">{{ .Title }}</a></li>
|
||||||
|
{{ end }}
|
||||||
|
|
||||||
|
{{ end }}
|
||||||
|
|
||||||
|
{{ end }}
|
||||||
@@ -1,9 +1,3 @@
|
|||||||
{{ define "main" }}
|
{{ define "main" }}
|
||||||
{{ $chapters := where .Site.RegularPages "Params.tags" "cpp" }}
|
{{ .Content }}
|
||||||
|
|
||||||
{{ range sort $chapters "Params.chapter" "asc" }}
|
|
||||||
<ul>
|
|
||||||
<li><a href="{{ .Permalink }}">{{ .Title }}</a></li>
|
|
||||||
</ul>
|
|
||||||
{{ end }}
|
|
||||||
{{ end }}
|
{{ end }}
|
||||||
|
|||||||
321
placeholder.md
Normal file
321
placeholder.md
Normal file
@@ -0,0 +1,321 @@
|
|||||||
|
|
||||||
|
|
||||||
|
C++ Notes
|
||||||
|
=========
|
||||||
|
|
||||||
|
ToC
|
||||||
|
---
|
||||||
|
|
||||||
|
* [Basics](#basics)
|
||||||
|
|
||||||
|
* [Introduction](#basics-introduction)
|
||||||
|
* [Variables](#basics-variables)
|
||||||
|
* [Operators](#basics-operators)
|
||||||
|
|
||||||
|
* [Control Flow](#control_flow)
|
||||||
|
|
||||||
|
* [Decision Branching](#control_flow-branching)
|
||||||
|
* [Loops](#control_flow-loops)
|
||||||
|
* [Switches](#control_flow-switches)
|
||||||
|
* [Ternary Operator](#control_flow-ternary)
|
||||||
|
|
||||||
|
* [Other Topics](#other)
|
||||||
|
|
||||||
|
* [String Manipulation](#other_string-manipulation)
|
||||||
|
|
||||||
|
* * *
|
||||||
|
|
||||||
|
Basics
|
||||||
|
------
|
||||||
|
|
||||||
|
### I. Introduction
|
||||||
|
|
||||||
|
A C++ program generally consists of preprocessor directives and the main function.
|
||||||
|
|
||||||
|
A preprocessor directive tells the C++ preprocessor what to do before compiling. You can use this to include files, create macros, and determine compiling based on conditions. For example, include a file using `#include` This will include the iostream file from the C++ standard library.
|
||||||
|
|
||||||
|
The main function looks like:
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
std::cout << "Hello world" << '\n';
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
The `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.
|
||||||
|
|
||||||
|
### II. Variables
|
||||||
|
|
||||||
|
A variable lets you allocate some memory in the computer and use it to store values. You can also recall the values for later. Variables contain a memory address and an identifier. An identifier can be \*almost\* anything you wish it to be.
|
||||||
|
|
||||||
|
Variables naming rules:
|
||||||
|
|
||||||
|
* Cannot start with a number.
|
||||||
|
* Can only contain alphanumeric characters and underscores.
|
||||||
|
* Cannot contain any reserved keywords [(see here)](https://en.cppreference.com/w/cpp/keyword.html).
|
||||||
|
* If starting with an underscore, it can only start with one.
|
||||||
|
|
||||||
|
All variables have denoted types and they refer to what kind of variable it is. All variables also have a bit size and this denotes the range of values that can be stored.
|
||||||
|
|
||||||
|
Variable types:
|
||||||
|
|
||||||
|
* Integers:
|
||||||
|
|
||||||
|
* short int:
|
||||||
|
|
||||||
|
* Size: 2 bytes.
|
||||||
|
* Range: -32,768 to 32,767
|
||||||
|
|
||||||
|
* int:
|
||||||
|
|
||||||
|
* Size: 4 bytes.
|
||||||
|
* Range: -2 billion to 2 billion
|
||||||
|
|
||||||
|
* short:
|
||||||
|
|
||||||
|
* Size: 8 bytes.
|
||||||
|
|
||||||
|
* Floats:
|
||||||
|
|
||||||
|
* float:
|
||||||
|
|
||||||
|
* Size: 4 bytes.
|
||||||
|
* 6 significant figures.
|
||||||
|
|
||||||
|
* double:
|
||||||
|
|
||||||
|
* Size: 8 bytes.
|
||||||
|
* 15 significant figures.
|
||||||
|
|
||||||
|
* long double:
|
||||||
|
|
||||||
|
* Size: 16 bytes.
|
||||||
|
* 19 significant figures.
|
||||||
|
|
||||||
|
* Character:
|
||||||
|
|
||||||
|
* Type: char
|
||||||
|
Stores an integer value representative of an "ASCII" character.* Size: 1 byte.
|
||||||
|
|
||||||
|
* String:
|
||||||
|
|
||||||
|
Stores an indexable array of characters.* Usage: `#include`
|
||||||
|
* Variable creation: `std::string identifier = "string literal";`
|
||||||
|
|
||||||
|
* Boolean:
|
||||||
|
|
||||||
|
Stores an integer value of 0 or 1 representative of false or true respectively.* Type: bool
|
||||||
|
* Size: 1 byte
|
||||||
|
|
||||||
|
|
||||||
|
Variables are always declared before they are initialized. This means you must determine the type and the identifier before you store any values into it.
|
||||||
|
|
||||||
|
` int main() { int a; // Declaration a = 1; // Initialization int b = 2; // Declaration then initialization } `
|
||||||
|
|
||||||
|
Constant types are variables identified by `const` in their type definition. These are not allowed to be modified after they are declared and must be initialized at declaration.
|
||||||
|
|
||||||
|
### III. Operators
|
||||||
|
|
||||||
|
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(var)`. 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.
|
||||||
|
|
||||||
|
* \+ Addition
|
||||||
|
* \- Subtraction
|
||||||
|
* \* Multiplication
|
||||||
|
* / Division
|
||||||
|
* % Modulus
|
||||||
|
|
||||||
|
There are also ways to quickly perform arithmetic operations using "fast operators".
|
||||||
|
|
||||||
|
* Post increment/decrement:
|
||||||
|
This will return the current value and then perform the operation.
|
||||||
|
`x++`
|
||||||
|
`x--`
|
||||||
|
* Pre increment/decrement:
|
||||||
|
This will perform the operation, then return the current value.
|
||||||
|
`++x`
|
||||||
|
`--x`
|
||||||
|
|
||||||
|
The following is short for `x = x (operation) y`:* `x += y`
|
||||||
|
* `x -= y`
|
||||||
|
* `x *= y`
|
||||||
|
* `x /= y`
|
||||||
|
* `x %= y`
|
||||||
|
|
||||||
|
Next we'll approach the relational and logical operators. These allow you to evaluate comparisons between two or more variables.
|
||||||
|
|
||||||
|
Relational Operators
|
||||||
|
|
||||||
|
* Less than: <
|
||||||
|
* Greater than: >
|
||||||
|
* Less than or equal to: <=
|
||||||
|
* Greater than or equal to: >=
|
||||||
|
* Is equal to: ==
|
||||||
|
* Is not equal to: !=
|
||||||
|
|
||||||
|
Logical Operators
|
||||||
|
|
||||||
|
* AND: &&
|
||||||
|
* OR: ||
|
||||||
|
* NOT: !
|
||||||
|
|
||||||
|
When using multiple operators, it is important to know that operators have a precedence, just like in mathematics.
|
||||||
|
|
||||||
|
Operators with the same precedence are evaluated in the statement from left to right.
|
||||||
|
|
||||||
|
1st
|
||||||
|
|
||||||
|
Last
|
||||||
|
|
||||||
|
(a)
|
||||||
|
|
||||||
|
x++, x++
|
||||||
|
|
||||||
|
++x, --x
|
||||||
|
|
||||||
|
!
|
||||||
|
|
||||||
|
\* / %
|
||||||
|
|
||||||
|
\+ -
|
||||||
|
|
||||||
|
< > <= >=
|
||||||
|
|
||||||
|
\== !=
|
||||||
|
|
||||||
|
&&
|
||||||
|
|
||||||
|
||
|
||||||
|
|
||||||
|
\=
|
||||||
|
|
||||||
|
Control Flow
|
||||||
|
------------
|
||||||
|
|
||||||
|
### I. Decision Branching
|
||||||
|
|
||||||
|
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:
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
int x = 1;
|
||||||
|
if (x == 1) {
|
||||||
|
std::cout << x << " is equal to 1.\n";
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
will execute "1 is equal to 1".
|
||||||
|
|
||||||
|
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.
|
||||||
|
|
||||||
|
### II. Loops
|
||||||
|
|
||||||
|
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.
|
||||||
|
|
||||||
|
* While loop
|
||||||
|
* Do-While loop
|
||||||
|
* For loop
|
||||||
|
|
||||||
|
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.
|
||||||
|
|
||||||
|
` int main() { int x = true; while (x == true) { std::cout << "Hello!"; } } `
|
||||||
|
|
||||||
|
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.
|
||||||
|
|
||||||
|
The do-while loop will always evaluate once. This is because, unlike the while loop, the condition is checked after the main body executes.
|
||||||
|
|
||||||
|
` int main() { int x = true; do { std::cout << "Hello!"; } while (x == false); } `
|
||||||
|
|
||||||
|
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 `do { .. }` so the condition gets checked after it executes.
|
||||||
|
|
||||||
|
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.
|
||||||
|
|
||||||
|
` int main() { int x = true; for (int i = 0; i < 10; i++) { std::cout << "Hello!"; } } `
|
||||||
|
|
||||||
|
You can see here, the for loop starts at `i = 0`, checks if `i < 10`, then outputs `Hello!`. Then `i++` executes and i increases by 1. Then the loop repeats. The loop checks if `i < 10`. i is 1 now so this is true and the program outputs `Hello!`. This repeats until `i < 10` is false which is when `i = 10`. Pretty simple stuff.
|
||||||
|
|
||||||
|
Now suppose you want to leave a loop before the condition is false or skip an iteration quickly. Then you can use the `break` and `continue` statements. `break` will BREAK out of a loop causing is to cease executing. `continue` will SKIP everthing after the continue and start back at the top of the loop.
|
||||||
|
|
||||||
|
### III. Switch
|
||||||
|
|
||||||
|
Suppose you have a large array of if-else statements.
|
||||||
|
|
||||||
|
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 { ... }
|
||||||
|
|
||||||
|
Instead of writing that, you could've used a switch:
|
||||||
|
|
||||||
|
` 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; } `
|
||||||
|
|
||||||
|
One caveat is that the case must be a literal value. It cannot be another value. Another is that a `break;` 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.
|
||||||
|
|
||||||
|
### IV. Ternary Operator
|
||||||
|
|
||||||
|
The ternary operator lets you condense a simple if-else statement into one line.
|
||||||
|
|
||||||
|
x = (a > b) ? 1 : 0;
|
||||||
|
|
||||||
|
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.
|
||||||
|
|
||||||
|
Functions
|
||||||
|
---------
|
||||||
|
|
||||||
|
### I. Basics
|
||||||
|
|
||||||
|
A function is a wrapper to easily reuse code and improve readability of your program. The function signature contains the return type, the identifier of the function, and the parameters that the function requires. The function contains statements within the curly braces. Finally, the function returns a value which has a type indicated in the function signature. Proper functions also have one intended use. If a function contains more than one use, it is a good idea to split them up.
|
||||||
|
|
||||||
|
type name (parameters) {...}
|
||||||
|
|
||||||
|
Functions must be defined before they are called. That is, the definition must come before any reference to using the function. A function call is done by referencing the name of the function as a statement. For example `my_function(1,2,3);` is a call to `my_function(int a, int b, int c) {...}` with the parameter values `a = 1, b = 2, c = 3`.
|
||||||
|
|
||||||
|
A function which does not return anything has the type of `void`. When using a void type for a function, it is okay to omit the return statement at the end of the function. In other words, when using a void type, the return statement is optional.
|
||||||
|
|
||||||
|
Functions are also essential to organized code. It can also save you time because you no longer need to rewrite or copy large blocks of code that may need to be re-used. Instead, just call the function.
|
||||||
|
|
||||||
|
### II. Function Types
|
||||||
|
|
||||||
|
In c++, there are three different types of function parameters.
|
||||||
|
|
||||||
|
* Pass by value
|
||||||
|
|
||||||
|
* The data that is included in the function parameter is copied from the value of the variable that is in the argument. This creates an entirely new variable with the exact same data as the variable in the parameter. This new variable has only the scope of the function it is used in. When the function exists, this variable is destroyed.
|
||||||
|
|
||||||
|
* Pass by reference
|
||||||
|
|
||||||
|
* The address of the variable is directly associated with the variables for the local scope. That is, these local scope variables now hve access to the variables outside of the scope. This is done by using the ampersand character: `void my_func(int &var) {...}`. Essentially, these new variables contain no new data and do not take up any more memory that was already there. No matter is created, only another name for a variable which is removed after the function completes execution. The pass-by-reference parameters are called reference parameters and the arguments are called reference arguments.
|
||||||
|
|
||||||
|
* Constant parameters
|
||||||
|
|
||||||
|
* Constant types of parameters contain the `const` type for the parameters. The constant parameter can be either pass-by-value or a pass-by-reference type as well.
|
||||||
|
|
||||||
|
#### Pass by value:
|
||||||
|
|
||||||
|
Other Topics
|
||||||
|
------------
|
||||||
|
|
||||||
|
### String Manipulation
|
||||||
|
|
||||||
|
...
|
||||||
Reference in New Issue
Block a user