C++ (Lesson 1)

Hello World:

Typically, many programmers are shown a program that outputs “Hello, World!” when learning a new language. Try copying this code into your text editor, and then running the commands below:

#include <bits/stdc++.h>
using namespace std; // We are including the C++ toolkit.

// This is a comment, it will be ignored by the computer.

int main () {
cout << "Hello World" << endl;
return 0; // end the program
}

Run the program (assumed that the file is saved in file.cpp):

user:~$ make file
user:~$ ./file

The make command turns our code into something that is readable by the computer, and outputs a file known as a binary. Think of this as the .exe file format on Windows, where you can simply run the file.

Continuing on with C++ by example…

Variables:

Since you are in high school, it is assumed that you have somewhat learned the concept of a variable in math class. In programming, the premise is roughly the same. We use variables to assign a name to stored data, where the values of the data can be changed.

Declaring a variable:

type name = value; // general form
int number = 5; // example

Setting the value of a variable:

name = value; // general form
number = 10;

Using a variable:

#include <bits/stdc++.h>
using namespace std;

int main() {
int number = 5;
number = 10;
cout << number;
return 0;
}

C++ is statically typed, meaning that we need to manually tell the computer what type of variable we want to store. Here are some basic variable types:

int: Stores an integer (non-decimal)

double: Stores a floating point number (decimals)

char: Stores a single character.

string: Stores text (multiple characters).

bool: Stores either true or false.

I/O (console input and output):

There are multiple ways to print to the console, but we will be using cout for simplicity’s sake. If you’re curious, you can research printf and puts for alternative ways to print to console.

General Form:

cout << [data] << [data] ....; // print to the console
cin >> [variable]; // get input from the user and store it

Example:

#include <bits/stdc++.h>
using namespace std;

int main() {
cout << "this is being printed"; // print to console
cout << endl; // print a "line break" to go to the next line
int number;
cout << "please input a number:" << endl;
cin >> number; // takes the input from the user and stores it in number
cout << number << endl; // output the stored value
}

When doing input and output, we point the arrows (<< and >>) towards the direction that the data is going. So for cout, we point the << towards it, since we are directing data to the console output. For cin, we point the >> away from it, since we are directing data from the console.

By the way, cout stands for “console out”, while cin stands for “console in”.

If and Else Statements

Sometimes, we need to test a condition in our program. A condition is some statement where the answer will either be true or false.

General form:

if (condition) { // condition is true
// DO SOMETHING
} else { // condition is false
// DO SOMETHING
}

For example, you might want to see if an entered number is larger than 5.

#include <bits/stdc++.h>
using namespace std;

int main() {
cout << "Please enter a number: " << endl;
int number;
cin >> number;
if (number > 5) {
cout << "Your number is larger than 5!" << endl;
} else {
cout << "You number is smaller than 5!" << endl;
}
}

You can use operators to evaluate conditions:

> : Greater than

: Less than

>= : Greater than or equal to

<= : Less than or equal to

== : Is equal to

Loops (for) 

Sometimes, you need to repeat similar pieces of code. For example, you may want to take the input from the user several times to do something.

General form:

for (initialize; condition; increment) {
// DO SOMETHING
}

initialize: You need a variable to keep track of the number of times you have looped! You can create it here.

condition: This is the condition that is checked every time the loop occurs. If it is true, the loop will keep going.

increment: You probably want to change your counter variable every time a loop occurs (increase it). You can do this here.

Example (counting from zero to ten):

#include <bits/stdc++.h>
using namespace std;

int main() {
for (int i = 0; i <= 10; i++) {
cout << i << endl; // Print out counter variable on each loop iteration
}
}

Example (Taking the input of the user 5 times and outputting it plus one):

#include <bits/stdc++.h>
using namespace std;

int main() {
for (int i = 0; i < 5; i++) {
cout << "Input a number: " << endl;
int number;
cin >> number;
cout << (number + 1) << endl;
}
}

Arrays

Want to go the extra mile?

http://www.cplusplus.com/doc/tutorial/arrays/

Try these problems! 

Test: https://dmoj.ca/problem/helloworld

https://dmoj.ca/problem/aplusb

https://dmoj.ca/problem/ccc14j1

https://dmoj.ca/problem/ccc15j1

Leave a Reply

Your email address will not be published. Required fields are marked *