C++ Starting
#include <iostream> using namespace std; int main() { cout << "Hello World!"; } return 0
"Hello World"
if
specify a block of code to be executed, if a specified condition is true
else
specify a block of code to be executed, if the same condition is false
else if
specify a new condition to test, if the first condition is false
switch
specify many alternative blocks of code to be executed
continue;
statement breaks one iteration (in the loop), if a specified condition occurs, and continues with the next iteration in the loop.
switch
statement to select one of many code blocks to be executed.
\\
used to comment
x *= 3
x = x * 3 in code
x += 3
x = x + 3 in code
x -= 3
x = x - 3 in code
x /= 3
x = x / 3 in code
x >>= 3
x = x >> 3 in code
x ^= 3
x = x ^ 3 in code
Abstract Data Type (ADT)
A container whose properties (data and operations) are specified independently of any particular implementation... Created to hide internal details
while loop
A control flow statement that allows code to be executed repeatedly.
"for" loops
A for loop lets us repeat code a fixed number of times. It is used to DETERMINE.
do { // code block to be executed } while (condition);
A vague example of a do/while loop
--
Decrements operator; decreases the value of a variable by 1
struct<structName> { type1 field1; type2 field2; }
How do you write a structure?
{
Marked after int main()
!=
Not equal
#include <iostream>
Starts code
float
Stores fractional numbers, containing one or more decimals. Sufficient for storing 7 decimal digits
/* */
Used for multi-lined comments
break;
Used in a switch statement to jump out of a loop.
struct<structName>
What hides data more secure than strings?
=
assignment operator
using namespace std;
follows #include <iostream>
\n
new line character
''
Character variables are quoted in
#include <iostream> using namespace std; int main() { for (int i = 0; i <= 10; i = i + 2) { cout << i << "\n"; } } return 0
Code that will print *only even* values between 0 to 10
#include <iostream> using namespace std; int main() { for (int i = 0; i < 5; i++) { cout << i << "\n"; } } return 0
Code that will print numbers 0 to 4 Hint: Statement 1 sets a variable before the loop starts (int i = 0). Statement 2 defines the condition for the loop to run (i must be less than 5). If the condition is true, the loop will start over again, if it is false, the loop will end. Statement 3 increases a value (i++) each time the code block in the loop has been executed.
#include <iostream> using namespace std; int main() { int myNum = 15; cout << myNum; } return 0
Create a variable called myNum of type int and assign it the value 15.
cout << "Hello World!" << endl;
One line example of how to a cout line without using \n Hint: use "Hello World!"
#include <iostream> using namespace std; int main() { int time = 20; if (time < 18) { cout << "Good day."; } else { cout << "Good evening."; } } return 0
Write a code that time (20) is greater than 18, so the condition is false. Because of this, we move on to the else condition and print to the screen "Good evening". If the time was less than 18, the program would print "Good day".
#include <iostream> using namespace std; int main() { int i = 0; do { cout << i << "\n"; i++; } while (i < 5); return 0; }
Write a code that will print out the numbers 0 to 4 Hint: Do/While Loop
#include <iostream> #include <string> using namespace std; int main () { // Creating variables int myNum = 5; // Integer (whole number) float myFloatNum = 5.99; // Floating point number double myDoubleNum = 9.98; // Floating point number char myLetter = 'D'; // Character bool myBoolean = true; // Boolean string myString = "Hello"; // String // Print variable values cout << "int: " << myNum << "\n"; cout << "float: " << myFloatNum << "\n"; cout << "double: " << myDoubleNum << "\n"; cout << "char: " << myLetter << "\n"; cout << "bool: " << myBoolean << "\n"; cout << "string: " << myString << "\n"; } return 0
Write a code the will print out: *Hint include string* int: 5 float: 5.99 double: 9.98 char: D bool: 1 string: Hello
int main() { student info studentOne; studentOne.name = "John"; studentOne.id = "10"; studentOne.status = "Freshman"; } return 0
Write a code to store a students Name, id, and status as a structure. (Name John, id 10, status Freshman)
#include <iostream> using namespace std; int main() { int i = 0; while (i < 5) { cout << i << "\n"; i++; } } return 0
code in the loop will run, over and over again, as long as a variable (i) is less than 5: Hint: while loop
const
keyword if you don't want others (or yourself) to override existing values (this will declare the variable as "constant", which means unchangeable and read-only):
int main()
marks the beginning of a function, auto puts info into RAM, says 'I'll return an integer value'
double
stores floating point numbers, with decimals, such as 19.99 or -19.99 Sufficient for storing 15 decimal digits
int
stores integers (whole numbers), without decimals, such as 123 or -123
char
stores single characters, such as 'a' or 'B'. Char values are surrounded by *single quotes... Stores a single character/letter/number, or ASCII values
string
stores text, such as "Hello World". String values are surrounded by double quotes
bool
stores values with two states: true or false
Puts data into arrays
string name [10]; string id [10]; string status;
"for" loops
(initialization; condition; update)
==
Equal to
int myNum = 5; myFloatNum = 5.99; myLetter = 'D'; myText = "Hello"; myBoolean = true;
Examples of myNum as interger 5, FloatNum 5.99, Letter D, Text Hello, and Boolean true.
1 byte
How many bytes is a boolean?
1 byte
How many bytes is a char?
8 bytes
How many bytes is a double?
4 bytes
How many bytes is a float?
4 bytes
How many bytes is an int?
#include<string>
If you are using strings, you must include this..
++
Increment operator; increases value by 1
std::cout << "Hello World!";
One line example of how to 'using namespace line std' line can be omitted.. Hint: use "Hello World!";
Abstraction
Reducing information and detail to focus on essential characteristics. Ex: A three-sided polygon is a triangle but triangles can be scalene, isosceles, or equilateral.
&&
Returns true if both statements are true; logical and
||
Returns true if one of the statements is true; logical or
!
Reverse the result, returns false if the result is true; Logical not
string cars[4];
Short array for 4 cars.
string cars[4] = {"Volvo", "BMW", "Ford", "Mazda"};
Short array for 4 cars: Volva, BMW, Fora, and Mazda (You don't always have to put a number into the brackets)
""
String's variables are quoted in
do/while loop
This loop will execute the code block once, before checking if the condition is true, then it will repeat the loop as long as the condition is true. (So it runs even if the condition is false.)
<<
To combine both text and a variable, separate them with the ____ operator
square brackets
To declare an array, define the variable type, specify the name of the array follow by the specific number of elements in....
#include <iostream> using namespace std; int main() { int day = 4; switch (day) { case 1: cout << "Monday"; break; case 2: cout << "Tuesday"; break; case 3: cout << "Wednesday"; break; case 4: cout << "Thursday"; break; case 5: cout << "Friday"; break; case 6: cout << "Saturday"; break; case 7: cout << "Sunday"; break; } } return 0
Use cases as days.. write a code that will output day 4 as Thursday.
Array
Used to store multiple values in a single variable, instead of declaring separate variables for each value
switch(expression) { case x: // code block break; case y: // code block break; default: // code block }
Vague example of switch
#include <iostream> #include <string> using namespace std; int main() { int time = 20; string result = (time < 18) ? "Good day." : "Good evening."; cout << result; return 0; }
Write a short-handed version of this using ternary operators: #include <iostream> using namespace std; int main() { int time = 20; if (time < 18) { cout << "Good day."; } else { cout << "Good evening."; } } return 0
int i = 0; while (i < 5) { cout << i << "\n"; i++; }
Write body of a code that will run, over and over again, as long as a variable (i) is less than 5:
#include <iostream> using namespace std; int main() { int x, y; int sum; cout << "Type a number: "; cin >> x; cout << "Type another number: "; cin >> y; sum = x + y; cout << "Sum is: " << sum; } return 0
Write code for a user that needs to input two numbers, and then we print the sum..
while (condition) { // code block to be executed }
Write vague example for a while loop
#include <iostream> using namespace std; int main() { (int i = 0; i < 5; ) { cout << << "\n"; } } return 0
for loop that will print "Yes" 5 times
%
modulus (remainder) operator; divides two integers and returns the remainder as an integer
cout ("see-out")
object used to output/print text
+
operator can be used between strings to add them together to make a new string. This is called concatenation:
cin ("see-in")
predefined variable that reads data from the keyboard with the extraction operator (>>); used to get user input