1
(true)
or 0
(false)if
if ... else ...
if ... else if ....
if ... else ...
if
Statementsif
Statements (1)true
or false
()
)
{}
)
which enclose the return statementif (variable == value) {
...
}
if
Statements (2)if
Statement iifExample1
to store
the integer value 1
iseXample1
equal to 1
?
true
: print the value
True, ifExample1 is 1
to the screenif
Statements (3)if
Statement iiifExample1
to store
the integer value 2
ifExample1
equal to 1
?
True
: print the value
True
to the screenif else
Statementsif else
Statements (1)else
is used as the ultimate result
for a test expression
false
if (variable == value) {
...
} else {
...
}
if else
Statements (2)if else
Statement iifExample1
to store
the integer value 1
ifExample1
equal to 1
?
True
: print the value
True, ifExample1 is 1
to the screenFalse, ifExample1 is not 1
if else
Statements (3)if else
Statement iiifExample1
to store
the integer value 2
ifExample1
equal to 1
?
True
: print the value
True, ifExample1 is 1
to the screenFalse, ifExample1 is not 1
#include <iostream>
int ifExample1 = 2;
int main() {
if (ifExample1 == 1) {
std::cout << "True, ifExample1 is 1" << std::endl;
} else {
std::cout << "False, ifExample1 is not 1" << std::endl;
}
return 0;
}
else if
Statementselse if
Statements (1)if (variable > value) {
...
} else if (variable < value) {
...
} else {
...
}
else if
Statements (2)else if
Statement iifExample1
to store
the integer value 1
ifExample2
to store
the integer value 3
ifExample1
equal to 1
?
True
: print the value
[True] ifExample1 is 1
to the screen#include <iostream>
int ifExample1 = 1;
int ifExample2 = 3;
int main() {
if (ifExample1 == 1) {
std::cout << "[True] ifExample1 is 1" << std::endl;
} else if (ifExample2 == 2) {
std::cout << "[True] ifExample2 is 2" << std::endl;
} else {
std::cout << "[False] ifExample1 is not 1, and ifExample2 is not 2" << std::endl;
}
return 0;
}
[True] ifExample1 is 1
else if
Statements (3)else if
Statement iiifExample1
to store
the integer value 3
ifExample2
to store
the integer value 2
ifExample1
equal to 1
?
True
: print the value
[True] ifExample1 is 1
to the screenifExample2
is equal to 2
?
True
: print the value
[True] ifExample2 is 2
to the screen#include <iostream>
int ifExample1 = 3;
int ifExample2 = 2;
int main() {
if (ifExample1 == 1) {
std::cout << "[True] ifExample1 is 1" << std::endl;
} else if (ifExample2 == 2) {
std::cout << "[True] ifExample2 is 2" << std::endl;
} else {
std::cout << "[False] ifExample1 is not 1, and ifExample2 is not 2" << std::endl;
}
return 0;
}
[True] ifExample2 is 2
else if
Statements (4)else if
Statement iiiifExample1
to store
the integer value 1
ifExample2
to store
the integer value 3
ifExample1
equal to 1
?
True
: print the value
[True] ifExample1 is 1
to the screenifExample2
is equal to 2
?
True
: print the value
[True] ifExample2 is 2
to the screen[False], ifExample1 is not 1, and ifExample2 is not 2
#include <iostream>
int ifExample1 = 1;
int ifExample2 = 3;
int main() {
if (ifExample1 == 1) {
std::cout << "[True] ifExample1 is 1" << std::endl;
} else if (ifExample2 == 2) {
std::cout << "[True] ifExample2 is 2" << std::endl;
} else {
std::cout << "[False] ifExample1 is not 1, and ifExample2 is not 2" << std::endl;
}
return 0;
}
[True] ifExample1 is 1
if
Statementsif
Statements (1)if
statements can be written inside
each other
if (variable == value) {
if (variable1 == value1) {
...
} else if (variable1 == value2) {
...
} else {
...
}
} else {
if (variable2 == value1) {
...
} else {
...
}
}
if
Statements (2)if
Statement iifExample1
to store
the integer value 1
ifExample2
to store
the integer value 2
ifExample1
equal to 1
?
True
: perform another comparison
check: ifExample2
equal to 2
?
True
: print
[True] ifExample1 is 1 and ifExample2 is 2
#include <iostream>
int ifExample1 = 1;
int ifExample2 = 2;
int main() {
if (ifExample1 == 1) {
if (ifExample2 == 2) {
std::cout << "[True], ifExample1 is 1, and ifExample2 is 2" << std::endl;
} else if (ifExample2 == 4) {
std::cout << "[True] ifExample1 is 1 and ifExample2 is 4" << std::endl;
} else {
std::cout << "[True] ifExample1 is 1 but, ifExample2 is not 2 or 4" << std::endl;
}
} else {
std::cout << "[False] ifExample1 is not 1" << std::endl;
}
return 0;
}
[True], ifExample1 is 1, and ifExample2 is 2
if
Statements (3)if
Statement iiifExample1
to store
the integer value 1
ifExample2
to store
the integer value 4
ifExample1
equal to 1
?
True
: perform another comparison
check: ifExample2
equal to 2
?
True
: print
[True] ifExample1 is 1 and ifExample2 is 2
ifExample2
equal to 4
?
True
: print
[True] ifExample1 is 1 and ifExample2 is 4
#include <iostream>
int ifExample1 = 1;
int ifExample2 = 4;
int main() {
if (ifExample1 == 1) {
if (ifExample2 == 2) {
std::cout << "[True], ifExample1 is 1, and ifExample2 is 2" << std::endl;
} else if (ifExample2 == 4) {
std::cout << "[True] ifExample1 is 1 and ifExample2 is 4" << std::endl;
} else {
std::cout << "[True] ifExample1 is 1 but, ifExample2 is not 2 or 4" << std::endl;
}
} else {
std::cout << "[False] ifExample1 is not 1" << std::endl;
}
return 0;
}
[True] ifExample1 is 1 and ifExample2 is 4
if
Statements (4)if
Statement iiiifExample1
to store
the integer value 2
ifExample2
to store
the integer value 5
ifExample1
equal to 2
?
True
: perform another comparison
check: ifExample2
equal to
2
?[False] ifExample1 is not 1
#include <iostream>
int ifExample1 = 2;
int ifExample2 = 5;
int main() {
if (ifExample1 == 1) {
if (ifExample2 == 2) {
std::cout << "[True], ifExample1 is 1, and ifExample2 is 2" << std::endl;
} else if (ifExample2 == 4) {
std::cout << "[True] ifExample1 is 1 and ifExample2 is 4" << std::endl;
} else {
std::cout << "[True] ifExample1 is 1 but, ifExample2 is not 2 or 4" << std::endl;
}
} else {
std::cout << "[False] ifExample1 is not 1" << std::endl;
}
return 0;
}
[False] ifExample1 is not 1
while
do ... while ...
for
while
Loopswhile
Loops (1)1
(true
) the code inside the loop will be
executed0
(false
) the loop will terminatewhile (variable < value) {
...
variable += 1;
}
while
Loops (2)whileExample1 = 0
whileExample1 <= 5
evaluates to
1
(true
)
whileExample1
whileExample1
by
1
0
(false
)while
Loops (3)while
Loopbreak
statements can be used to stop
the loop if a condition is evaluated to
true
whileExample1 = 0
whileExample1 <= 5
evaluates to
1
(true
)
whileExample1
whileExample1
by
1
1
(true)
whileExample1
is
2
while
Loops (4)continue
statements can stop the
current iteration and continue onto the nextwhileExample1 = 0
whileExample1 <= 5
evaluates to
1
(true
)
whileExample1
whileExample
by
1
1
(true)
whileExample1
is
2
#include <iostream>
int whileExample1 = 0;
int main() {
while (whileExample1 <= 5) {
whileExample1 += 1;
if (whileExample1 == 2) {
std::cout << "SKIPPED" << std::endl;
continue;
}
std::cout << "whileExample1 -> " << whileExample1 << std::endl;
}
return 0;
}
whileExample1 -> 1
SKIPPED
whileExample1 -> 3
whileExample1 -> 4
whileExample1 -> 5
whileExample1 -> 6
while
Loops (5)while
Loopstrue
value after the while
keyword
1
whileExample1
until it reaches a certain
value
whileExample1
must be
equal to 5
do ... while
Loopsdo ... while
Loops (1)while
loop
structuredo ... while
is
performed before the conditional check is evaluateddo {
...
}
while (condition);
do ... while
Loops (2)doWhileExample1 = 0
do
statement:
doWhileExample1
doWhileExample1
by
1
doWhileExample1 <= 5
evaluates to
1
(true
)0
(false
)#include <iostream>
int doWhileExample1 = 0;
int main() {
do {
std::cout << "doWhileExample1 -> " << doWhileExample1 << std::endl;
doWhileExample1 += 1;
}
while (doWhileExample1 <= 5);
return 0;
}
doWhileExample1 -> 0
doWhileExample1 -> 1
doWhileExample1 -> 2
doWhileExample1 -> 3
doWhileExample1 -> 4
doWhileExample1 -> 5
for
Loopsfor
Loops (1)for
loops are useful because…
for (initialisation; condition; update) {
...
}
initialisation
: initialises the
counter-variable
int i = 0;
condition
: if 1
(true) the
body of the loop is executed, if 0
(false)
the loop is terminatedupdate
: increments the counter-variable
and checks the condition
again
i++
for
Loops (2)int i = 0;
i < 5
- checks whether the
integer is less than 5
1
(true) then
execute the code within the for
body
i
i++
0
(false)for
Loops (3)int i = 5;
i > 0
- checks whether the
integer is less than 5
1
(true) then
execute the code within the for
body
i
i--
0
(false)for
Loops (4)for (variable : [array or vector]) {
...
}
for
Loops (5)int i
i
for
Loops (6)auto item
item
first
: returns the keysecond
: returns the value#include <iostream>
#include <map>
std::map<int, std::string> mapExample1 = {{0, "Ian Cornelius"}, {1, "Terry Richards"}, {2, "Daniel Goldsmith"}};
int main() {
for (auto &item : mapExample1) {
std::cout << "item.first -> " << item.first << std::endl;
std::cout << "item.second -> " << item.second << std::endl;
}
}
item.first -> 0
item.second -> Ian Cornelius
item.first -> 1
item.second -> Terry Richards
item.first -> 2
item.second -> Daniel Goldsmith
for
Loops (7)for
1
(otherwise known as
true
)for(int i = 1; i > 0; i++) {
...
}
i
will
always be greater than 0
i
is a particular
value