Chapter 2.2
Chapter 2.2
First Program
C++ variables
(Containers for storing data)
int - stores integers (whole numbers), without decimals, such as 123 or -123
double - stores floating point numbers, with decimals, such as 19.99 or -19.99
char - stores single characters, such as 'a' or 'B'. Char values are surrounded by
single quotes
string - stores text, such as "Hello World". String values are surrounded by
double quotes
bool - stores values with two states: true or false
Variable declaration
int myNum = 15;
cout << myNum;
int myNum;
myNum = 15;
cout << myNum;
Variables
Note that if you assign a new value to an existing variable, it will overwrite
the previous value:
int myNum = 15; // Now myNum is 15
myNum = 10; // Now myNum is 10
cout << myNum;
Displaying variables
int x = 5;
int y = 6;
int sum = x + y;
cout << sum;
Multiple variables
int main() {
const int minutesPerHour = 60;
const float PI = 3.14;
cout << minutesPerHour << "\n";
cout << PI;
return 0;
}
User input
cin is a predefined variable that reads data from the keyboard with the extraction operator (>>)
#include <iostream>
using namespace std;
int main() {
int x;
cout << "Type a number: "; // Type a number and press
enter
cin >> x; // Get user input from the keyboard
cout << "Your number is: " << x;
return 0;
}
Creating a Simple Calculator
C++ Data Types
Scientific Numbers
A floating point number can also be a scientific number with an "e" to indicate
the power of 10:
Boolean Types
String Types
int main() {
if (20 > 18) {
cout << "20 is greater than 18";
}
return 0;
}
If
#include <iostream>
using namespace std;
int main() {
int x = 20;
int y = 18;
if (x > y) {
cout << "x is greater than y";
}
return 0;
}
If …..else
int main() {
int time = 20;
if (time < 18) {
cout << "Good day.";
} else {
cout << "Good evening.";
}
return 0;
}
If …..else
#include <iostream>
using namespace std;
int main() {
int time = 20;
if (time < 18) {
cout << "Good day.";
} else {
cout << "Good evening.";
}
return 0;
}
Else if
Use the else if statement to specify a new condition if the first condition is false.
#include <iostream>
using namespace std;
int main() {
int time = 22;
if (time < 10) {
cout << "Good morning.";
} else if (time < 20) {
cout << "Good day.";
} else {
cout << "Good evening.";
}
return 0;
}
Switch statements
Use the switch statement to select one of many code blocks to be executed.
The switch expression is evaluated once
The value of the expression is compared with the values of each case
If there is a match, the associated block of code is executed
#include <iostream> break;
using namespace std; case 'D' :
cout << "You passed";
int main () { break;
// local variable declaration: case 'F' :
char grade = 'D'; cout << "Better try again";
break;
switch(grade) { default :
case 'A' : cout << "Invalid grade";
cout << "Excellent!"; }
break; cout << "Your grade is " << grade;
case 'B' :
case 'C' : return 0;
cout << "Well done"; }
Break statement
The break in C++ is a loop control statement that is used to terminate the
loop