You are on page 1of 5

This one is long lol

Operator precedence (Operator Hierarchy)

!, +, -, & (Unary Operators)


*, /, %, +, - (Arithmetic Operators)
<, <=, >-, >, ==, != (Relational Operators)
&&, || (Logical Operators)
= (Equal Sign/Assignment Statement)

(RELATIONAL OPERATORS)
[this is just a recap we already discussed this last g9 feel free to skip]
Boolean values are typically obtained as the result of some test involving relational
operators
> (Greater than)
>= (Greater than or equal to)
<= (Less than or equal to)
< (Less than)
== (Equal to)
!= (Not equal to, unequal)

(LOGICAL OPERATORS)
More complicated tests can be performed by connecting Boolean expressions together
And (&&) returns true only if BOTH operands are true
True && True => True
True && False => False
False && True => False
False && False => False
Or (||) returns true if EITHER one (or both) operands is true
True || True => True
True || False => True
False || True => True
False || False => False

Negation (!) Negates the expressions/ Returns the opposite


!a
True => False
False => True

==================================================================
Control Structures
Current program flow: Sequential, one statement at a time, from first to last
Computer programs won't be so useful if they ran the same sequence of statements
every time
Need to be able to change which statements to run and when, the order of statements,
or control flow.

Example:
Words Count Program - Must count the correct number of words no matter how large or
small the file is
Computer game = Character must move based on which key the user pressed

Portions of code that contain groups of statements called blocks, and depending on
circumstances, execute these statements in a particular way
Two main kinds: Conditionals and Loops
Conditions
often expressed in terms of Boolean expressions
Expressions that can either be true or false
Variables can be declared of type bool for Boolean
Reserved words true and false can be used as values equivalent to 1 and 0,
respectively. You can also see this when you print this out. But useful to think about
them as non-numeric values
bool x = true;
cout << x << endl;

Conditionals use Relational operators


Example: Assume x = 6 and y = 2
x > y // true (1)
x < y // false (0)
x == y // true (1)
x != y // false (0)

More complex conditions can be made using logical operands (&&, ||, !)
Example:
!(x > 2) // False
(x > y) && (y > 0) // True
(x < y) && (y > 0) // False
(x < y) || (y > 0) // True
IF CONDITIONALS
If conditions evaluate true statements, the program is executed. If it's false, it would be
ignored
Example:
if (condition)
statement;
// if the condition is true then it would proceed to execute the statement below

IF ELSE CONDITIONALS
Used to decide between two alternatives
If the condition evaluates to true, the first block is executed, otherwise, the second block
is executed:
Example:
if (condition)
{
Statement 1;
}
else
{
Statement 2;
}
// If the condition is true, statement one will be executed, if it's false, statement two will
be executed //

Note for using these statements: there’s no need to include brackets {} if it’s only one
statement will be executed
IF ELSE CHAINS (LOOPING)
If (condition)
Statement 1;
Else if (condition 2)
Statement 2;
Else if (condition 3)
Statement 3
.
.
.
Else
Statement n;

The code checks from top to bottom and will execute the statement under the first condition met.

You might also like