Section Two Flashcards
What are assignment operators?
=
+=
-=
and so on
What are arithmetic operators
*
etc
What are comparison operators
==
!=
>
<
>=
<=
What are logical Operators
&& and
|| or
! not
Reverse the result, returns false if the result is true !(x < 5 && x < 10)
Create a first and last name
append the last to the first
string firstName = “John “;
string lastName = “Doe”;
string fullName = firstName.append(lastName);
cout «_space;fullName;
Get the length of a string
string txt = “ABCDEFGHIJKLMNOPQRSTUVWXYZ”;
cout «_space;“The length of the txt string is: “ «_space;txt.length();
size() does the same thing
Get the H from a string variable with the value of Hello
string myString = “Hello”;
cout «_space;myString[0];
Using lenght(), get the last letter of a string variable
string myString = “Hello”;
cout «_space;myString[myString.length() - 1];
Change one of a string variable’s characters
string myString = “Hello”;
myString[0] = ‘J’;
cout «_space;myString;
What do you use to get a string character other than []
cout «_space;myString.at(0)
Show how to use quotes in a string with an escape character
string txt = “We are the so-called "Vikings" from the north.”;
If you don’t add the below, how do you access strings and cout?
using namespace std;
include <iostream></iostream>
#include <string>
// using namespace std; - Remove this line</string>
int main() {
std::string greeting = “Hello”;
std::cout «_space;greeting;
return 0;
}
How would you create a string in c-style
C doesn’t let you create strings, just characters. If you wanted to print a string though you could add all the letters into an array and print that
char greeting2[] = “Hello”; // C-Style String (an array of characters)
Find the highest value of 5 and 10
find the lowest value
cout «_space;max(5, 10)
cout «_space;min(5, 10)
If you want more math functions, what header library do you include?
find the square root of 64
round 2.6
natural logarithm of 2
include <cmath></cmath>
cout «_space;sqrt(64);
cout «_space;round(2.6);
cout «_space;log(2);
Use a boolean expression
int x = 10;
int y = 9;
cout «_space;(x > y);
Create an if then statement that takes your age and the voting age and let’s you know if you’re old enough to vote.
include <iostream></iostream>
using namespace std;
int main() {
int myAge = 25;
int votingAge = 18;
if (myAge >= votingAge) {
cout «_space;“Old enough to vote!”;
} else {
cout «_space;“Not old enough to vote.”;
}
return 0;
Perform a switch statement
include <iostream></iostream>
using namespace std;
int main() {
int num = 2;
switch (num) { case 1: cout << "One" << endl; break; case 2: cout << "Two" << endl; break; default: cout << "Other" << endl; } return 0; }
What is a ternary operator?
Short hand if-else
int time = 20;
string result = (time < 18) ? “Good day.” : “Good evening.”;
cout «_space;result;
If you go into a switch block, how do you get out?
break
Make a loop and a variable with the value of 0
The loop should continue if i is less than 5
increment i by 1 each iteration
int i = 0;
while (i < 5) {
cout «_space;i «_space;“\n”;
i++;
}
Create a do/while loop.
What makes it different?
It will always go through one iteration
int i = 0;
do {
cout «_space;i «_space;“\n”;
i++;
}
while (i < 5);
Show the structure of a for loop.
for (execute once, condition to keep running code block, what happens every iteration) {
// code block to be executed
}
Statement 1 is executed (one time) before the execution of the code block.
Statement 2 defines the condition for executing the code block.
Statement 3 is executed (every time) after the code block has been executed.
Using a for loop, give it an integer variable (i) that equals 0 to start, the code loop should continue as long as ‘i’ is less than five.
For each loop increase ‘i’ by 1
include <iostream></iostream>
using namespace std;
int main() {
for (int i = 0; i < 5; i++) {
cout «_space;i «_space;“\n”;
}
return 0;
}
Create a nested loop that increments by one and goes to 2
The nested loop should should iterate 3 times and increment a number by 1
include <iostream></iostream>
using namespace std;
int main() {
// Outer loop
for (int i = 1; i <= 2; ++i) {
cout «_space;“Outer: “ «_space;i «_space;“\n”; // Executes 2 times
// Inner loop for (int j = 1; j <= 3; ++j) { cout << " Inner: " << j << "\n"; // Executes 6 times (2 * 3) } } return 0; }
What is a for-each loop? How would you perform one?
This is basically a normal loop for us:
int myNumbers[5] = {10, 20, 30, 40, 50};
for (int i : myNumbers) {
cout «_space;i «_space;“\n”;
}
How would you create an array of 4 cars?
string cars[4] = {“Volvo”, “BMW”, “Ford”, “Mazda”};
Loop through an array using a:
for loop
for-each loop
// Create an array of strings
string cars[5] = {“Volvo”, “BMW”, “Ford”, “Mazda”, “Tesla”};
// Loop through strings
for (int i = 0; i < 5; i++) {
cout «_space;cars[i] «_space;“\n”;
}
or
// Create an array of integers
int myNumbers[5] = {10, 20, 30, 40, 50};
// Loop through integers
for (int i : myNumbers) {
cout «_space;i «_space;“\n”;
}
Do you have to specify an array size to make one?
No, you can omit it like below
string cars[] = {“Volvo”, “BMW”, “Ford”};