#include <iostream>
using namespace std;
int main() {
char c = "A";
cout << c << endl;
return 0;
}
#include <iostream>
using namespace std;
int main() {
char c = "A";
char c = 'A'; // has to have single quotations to initialize a char variable
cout << c << endl;
return 0;
}
#include <iostream>
using namespace std;
int main() {
bool itIsFriday = true;
cout << itIsFriday << endl;
return 0;
}
Output: 1
// remember that for boolean variables, the 1's and 0's represent true (1) and false (0) values. Therefore, printing 1 means yes! It is Friday :)
I am a data type that stores a value without decimal values. What am I ?
An integer variable! I can store values like "1", "250", or "231".
#include <iostream>
using namespace std;
int main() {
//this program is supposed to output (Sara said, "1 + 1 = 2")
cout << "Sara said, \"" << "1 + 1 = 2" << endl;
return 0;
}
#include <iostream>
using namespace std;
int main() {
//this program is supposed to output (Sara said, "1 + 1 = 2")
cout << "Sara said, \"" << "1 + 1 = 2" << endl;
cout << "Sara said, \"" << "1 + 1 = 2\"" << endl;
// remember that the (\) backslash character can be used to help print out quotation marks in your output
return 0;
}
#include <iostream>
using namespace std;
int main() {
char letter = 'a';
while (letter <= 'z') {
cout << letter << " ";
letter = letter + 1;
}
return 0;
}
Output:
a, b, c, d, e, f, g, ... x, y, z
// looping through while adding a plus one to a letter value will print out the following letter.
I am a data type that can store a single letter. What am I?
A char variable can only store one letter or symbol at a time! I can store values like 'a', '!', etc.
#include <iostream>
using namespace std;
//swap the variables
int main() {
int x = 1, y = 5;
x = y;
y = x;
cout << "x = " << x << endl;
cout << "y = " << y << endl;
return 0;
}
#include <iostream>
using namespace std;
int main() {
int x = 1;
int y = 5;
int temp = x;// create a temp variable to be able to properly swap the variables
x = y;
y = temp;
cout << "x: " << x << endl;
cout << "y: " << y << endl;
return 0;
}
#include <iostream>
using namespace std;
int main() {
int n = 1;
while (n <= 5) {
if (n == 5) {
cout << n;
} else {
cout << n << ", ";
}
n = n + 1;
}
return 0;
}
Output: 1, 2, 3, 4, 5
// remember you can use an if statement inside of a while loop to ensure that the last comma won't get printed
I am a data type that can store a number with decimal values. What am I?
A float or double! I can store values like 3.14.
#include <iostream>
using namespace std;
int main() {
int n = 0;
while (n >= 6) {
//this loop is meant to print the values 0 to 6.
cout << n << endl;
n++;
}
return 0;
}
#include <iostream>
using namespace std;
int main() {
int n = 0;
while (n <= 6) { //remember that for a loop to run, the parameters in parentheses has to be true and will exit the loop when false.
//this loop is meant to print the values 0 to 6.
cout << n << endl;
n = n + 1;
}
return 0;
}
#include <iostream>
using namespace std;
int main() {
char val = 'c';
val = val + 4;
cout << val << endl;
return 0;
}
#include <iostream>
using namespace std;
int main() {
char val = 'c';
val = val + 4;
cout << val << endl;
return 0;
}
Output: g
// the ASCII value of the letter adds to whatever value you added on
I am a data type that can tell you truths and falses. What am I?
A bool variable! I store values like true (1) or false (0).
#include <iostream>
#include <string>
using namespace std;
int main() {
//this program is meant to read in your whole name
string full_name;
cout << "Enter your full name: ";
cin >> full_name;
cout << "Hello " << full_name << "!";
return 0;
}
#include <iostream>
#include <string>
using namespace std;
int main() {
//this program is meant to read in your whole name
string full_name;
cout << "Enter your full name: ";
getline(cin, full_name);
//you can use getline to grab the whole line of input and therefore print your whole name.
cout << "Hello " << full_name << "!";
return 0;
}
#include <iostream>
using namespace std;
int main() {
int x = 5;
if (x > 4 || x < 7 && x != 5) {
cout << "Hurrah" << endl;
} else {
cout << "boo" << endl;
}
return 0;
}
Output: Hurrah
// remember the order of operations when it comes to if statements and the use of || and &&
I am a data type that can store sentences. What am I?
A string! I can store phrases such as, "CST 231" or "Cass Cabrera".