You are on page 1of 13

Beginners course programming

using C++

Sybrand

Language Sloth

. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .

Sybrand (Language Sloth) Lesson 2 1 / 13


Editor and compiler

Editor:
You can use any editor you want!
Good option: VSCode
Something like notepad also works
Compiler:
Windows: install MinGW
MacOS: install clang via Xcode
Linux: install gcc via your package manager
I might do a tutorial on installing later, for now either use
cplayground.com or lookup installation instructions.

. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .

Sybrand (Language Sloth) Lesson 2 2 / 13


Today

Write a quiz!
if and if-else statements
New variable type: bool
Boolean logic (conditions)
for loops
while loops

. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .

Sybrand (Language Sloth) Lesson 2 3 / 13


Example program: Quiz!
#include <iostream>
using namespace std;

int main() {
int correct = 0;
string answer;

cout << ”What type stores whole numbers?” << endl;


cin >> answer;
bool isCorrect = answer == ”int”;
if (isCorrect) {
correct++;
}

cout << ”What type stores text?” << endl;


cin >> answer;
if (answer == ”string”) {
correct++;
}

if (correct == 0) {
cout << ”Next time better luck, you didn't answer any question correctly.” << endl;
} else {
cout << ”Good job, you correctly answered ” << correct << ” questions.” << endl;
}

return 0;
}

. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .

Sybrand (Language Sloth) Lesson 2 4 / 13


if and else
Template:
if (<condition>) {
// Do something
} else if (<condition>) {
// Do something else
} else if (<condition>) {
// Do yet something else
} else {
// Do yet another thing
}

Condition:
// These are the variables the condition will use.
bool a, b;
int c, d;

// All kinds of possibilities


bool a_and_b = a && b;
bool a_or_b = a || b;
bool not_a = !a;

bool is_equal = c == d;
bool is_not_equal = !(c == d);
bool is_not_equal_2 = c != d;

bool is_bigger = c > d;


bool is_bigger_or_equal = (c > d) || (c == d);
bool is_bigger_or_equal_2 = c >= d;

bool is_smaller_or_equal = c <= d; . . . . . . . . . . . . . . . . . . . .


. . . . . . . . . . . . . . . . . . . .

Sybrand (Language Sloth) Lesson 2 5 / 13


Mindgames/exercises

Find another way to write the following:


(c == d) || (c < d)
!(c < d)
(c <= d) && (c >= d)
(c < d) && (c >= d)
!(d <= c)
!(c > d) && ((d < c) || (d == c))
!((d < c) || (d == c))
!(a || b)

. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .

Sybrand (Language Sloth) Lesson 2 6 / 13


Back to example program
#include <iostream>
using namespace std;

int main() {
int correct = 0;
string answer;

cout << ”What type stores whole numbers?” << endl;


cin >> answer;
bool isCorrect = answer == ”int”;
if (isCorrect) {
correct++;
}

cout << ”What type stores text?” << endl;


cin >> answer;
if (answer == ”string”) {
correct++;
}

if (correct == 0) {
cout << ”Next time better luck, you didn't answer any question correctly.” << endl;
} else {
cout << ”Good job, you correctly answered ” << correct << ” questions.” << endl;
}

return 0;
}

. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .

Sybrand (Language Sloth) Lesson 2 7 / 13


Loops!

#include <iostream>
using namespace std;

int main() {
int amount;

cout << ”How many *'s do you want?” << endl;


cin >> amount;

for (int i = 0; i < amount; i = i+1) {


cout << ”*”;
}
cout << endl;

return 0;
}

. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .

Sybrand (Language Sloth) Lesson 2 8 / 13


Gauss and the sum from 1 to 100

Gauss was a very succesful mathematician and a child prodigy,


there’s a famous story about his childhood:
One day Gauss’ teacher asked his class to add together all
the numbers from 1 to 100, assuming that this task would oc-
cupy them for quite a while. He was shocked when young
Gauss, a ter a few seconds thought, wrote down the answer
5050. The teacher couldn’t understand how his pupil had cal-
culated the sum so quickly in his head, but the eight year old
Gauss pointed out that the problem was actually quite simple.
He had added the numbers in pairs - the first and the last,
the second and the second to last and so on, observing that
1 + 100 = 101, 2 + 99 = 101, 3 + 98 = 101, ...so the total would
be 50 lots of 101, which is 5050.

. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .

Sybrand (Language Sloth) Lesson 2 9 / 13


We don’t need no bloody maths, we have computers!
#include <iostream>
using namespace std;

int main() {
int amount;

cout << ”How many numbers do you want to sum?” << endl;
cin >> amount;

int sum = 0;
for (int i = 1; i <= amount; i = i+1) {
// Does the same as: sum = sum + i;
sum += i;

// In fact, these all do the same:


// i = i+1
// i += 1
// i++
}
cout << ”The answer is ” << sum << endl;

return 0;
}
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .

Sybrand (Language Sloth) Lesson 2 10 / 13


One more: find the biggest number
#include <iostream>
using namespace std;

int main() {
int amount;

cout << ”How many numbers do you want to input?” << endl;
cin >> amount;

int biggest = 0;
for (int i = 0; i < amount; i++) {
int number;
cin >> number;

if (number > biggest) {


// There's a new biggest number
biggest = number;
}
}
cout << ”Of the ” << amount << ” numbers the biggest is ” << biggest <<
endl;

return 0;
} . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .

Sybrand (Language Sloth) Lesson 2 11 / 13


while loops

Continuously loops while the condition is met:


while (<condition>) {
// Do something
}

Let’s make our own for loop:


int i = 0;
while (i < 100) {
// Do something

// Increment i
i++;
}

. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .

Sybrand (Language Sloth) Lesson 2 12 / 13


Homework
Choose an exercise that interests you, you can do multiple if you
want. DM me your homework or questions when you get stuck, feel
free to pair up!
Write your own quiz, use the following questions and/or write
your own:
▶ Name a type in C++ you know (Accept int, string, bool, etc.)
▶ Rate this quiz with a number from 1 to 10 (Keep asking until you
get a number in 1-10)
Write a program that asks for a number a and b and calculates
ab = a ∗ a ∗ ... ∗ a (b times), return an error message if b is
negative.
Write a program that lets you guess a number, tell the user
“higher” or “lower”, congratulate and exit if they got it right.
Write a program that gives all divisors of a number up to 10000.
Hint: % gives the remainder, so 100 % 3 == 1 and 77 % 11 == 0.
Example: Divisors of 100 are 5 and 2 so 100 % 2 == 0 && 100 %
5 == 0 . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .

Sybrand (Language Sloth) Lesson 2 13 / 13

You might also like