150 lines
5.4 KiB
Markdown
150 lines
5.4 KiB
Markdown
+++
|
|
date = '2026-02-27T23:57:42-06:00'
|
|
draft = true
|
|
title = 'Control Flow'
|
|
layout = 'chapter'
|
|
type = 'book'
|
|
tags = 'cpp'
|
|
chapterno = 2
|
|
+++
|
|
|
|
## 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 |= |
|
|
|