Use LEFT and RIGHT arrow keys to navigate between flashcards;
Use UP and DOWN arrow keys to flip the card;
H to show hint;
A reads text to speech;
61 Cards in this Set
- Front
- Back
modulus
|
-C++ language for the remainder
|
|
#include <iostream>
using namespace std; int main() { int start; int time; start = 37; time = 22; cout << start + time << endl; return 0; } |
identifies i/o stream and uses the namespace of standard library
-declares two variables -assigns values (intializes variables in two steps) displays the total of 37 and 22 by adding their variables and sending them to the console |
|
#include <iostream>
using namespace std; int main() { int start; int time; int total; start = 37; time = 22; total = start + time; cout << total << endl; return 0; } |
prints the variable total which is set to the sum of start and time
|
|
total = start + 5;
cout << total << endl; What can you assume of the variable total... |
total is an int
total is the sum of the integer contained in the variable start and the 5 |
|
total = total + 5;
|
This statement really just means we’re going to add 5 to the value stored in
total, and we’ll take the value we get back and store it back in total. In other words, total will now be 5 greater than it was to begin with. The following code shows this in action: #include <iostream> using namespace std; int main() { int total; total = 12; cout << total << endl; total = total + 5; cout << total << endl; return |
|
Computer Variables vs Math Variables
|
Computer variables serve as abstract representations for other values HOWEVER unless specified variables are not constant and in actuality serve more as containers that hold value vs naming devices that represent a specific value
|
|
total = total + 5; re write in short hand
|
total +=5
|
|
total += time; re write in longhand
|
total = total + time
|
|
total = total + 1
|
total++
|
|
total++
|
adds the value of one to the variable
|
|
Add two variables
|
example
cout << start + time << endl; |
|
Add a variable and a number
|
example
cout << start + 5 << endl; |
|
Add two variables and save the result in a
variable |
example
total = start + finish |
|
Add a variable and a number and save the
result in a variable |
example
total = start + 8 |
|
Add a number to what’s already in a variable
|
example
total = total + 2 || total +=2 |
|
Add a number to what’s already in a variable
by using a shortcut |
example
total +=2 |
|
Add a variable to what’s already in a variable
|
total = total + time
|
|
Add a variable to what’s already in a
variable by using a shortcut |
totale += time
|
|
Add 1 to a variable
|
total++
|
|
why C++?
|
- adding one thing to the C language OBJECT_ORIENTED-PROGRAMMING!!!!
|
|
final = final - time
Shorthand and what is happening |
final -= time
final is being reassigned the a value of its starting value minus the value contained in the variable time |
|
final = final - 1
|
final--
|
|
total = total * multiplier;
shorthand and explain |
total *= multiplier;
the new total is equal to the previous value of total times multiplier |
|
C++ and division
|
So in terms of strictly whole numbers, the answer to 21 divided by 5 is 4
remainder 1. And that’s how the computer does arithmetic with integers: It gets two different answers: The quotient and the remainder. In math terms, the main answer (in our example, 4) is called the quotient. And what’s left over is the remainder. |
|
the two components to the a division problem in C++
|
quotient and remainder
|
|
to find quotient use what operator
|
(/) foward slash division operator
|
|
to find the remainder use
|
To find the remainder, use the percent sign (%). This is often called the
modulus operator. used to find remainder using ints not neccesary for floating point variables |
|
int / float = what will the answer be?
|
the answer will be a float
|
|
int first = 10 ;
first = first / 5; shorthand and explain |
first /= 5;
the the integer value held in the variable first is divided by 5 and reassigned with the quotient value |
|
int first = 33;
first %= 5; cout << first << endl; |
the variable first is reassigned the remainder value of 33 % 5
|
|
char variables
|
- hold just ONE character
- use single quotes to identify character |
|
Null character
|
- \0
-But the null character is important because it is often used to signify the end of something -nonprintable character |
|
NAMES OF SYMBOLS
|
. dot (but not period or decimal point)
@ at & ampersand (but not and) # pound (but not number sign) ! bang, but most people still say exclamation point ~ tilde % percent * star (not asterisk) ( left paren or left parenthesis ) right paren or right parenthesis [ left square bracket or left bracket ] right square bracket or right bracket == equal-equal (not double equal) ++ plus-plus (not double plus) – – minus-minus (not double minus) / forward slash \ backslash { left brace or left curly brace or open brace } right brace or right curly brace or close brace ^ caret, but a few people say hat (for real — no joke here!) “ double quote |
|
char ch = ' " '
|
A way o placing quotes without having to use the convention of \"
|
|
Backslashes
|
When the compiler sees a backslash inside a string or a character, it treats
the backslash as special and looks at whatever follows it. If you have something like ‘\’ with no other character inside the single quotes following it, the compiler thinks the final quote is to be combined with the backslash. And then it moves forward, expecting a single quote to follow, representing the end. Because a single quote doesn’t appear, the compiler gets confused and issues an error. |
|
string (a simplified definition)
|
short, a string is simply a set of characters strung
together. The compiler knows the start and end of a string in your code based on the location of the double quotes |
|
mystring = “Hello there”;
use encapsulation |
mystring(“Hello there”);
|
|
Using Brackets to Access Individual Characters in a String
|
mystring[3] is equal to the fourth character in mystring
|
|
#include <string>
|
This line means that your program is making
use of another file somewhere, and that file has a filename of string. Inside that other file is a bunch of C++ code that essentially gives your program the ability to understand strings. |
|
#include <iostream>
|
gives your program the ability to write to the
console, among other things. |
|
mystring[3]
what does the 3 represent and what is it called? |
-fourth position in mystring
- (The number inside brackets is called an index.) |
|
string mystring;
mystring = “abcdef”; char mychar = mystring[2]; cout << mychar << endl; what character is contained by mychar? |
'c'
|
|
string x = “abcdef”;
x[1] = ‘q’; cout << x << endl; |
index 1 is equal to 'q' changing string x to "aqcdef"
|
|
string mystring;
mystring = “Hi “; mystring += “there”; cout << mystring << endl; |
console "Hi there"
The first line declares the string mystring. The second line initializes it to “Hi ”. But what does the third line do? The third line uses the += operator, which appends something to the string, in this case “there”. Thus, after this line runs, the string called mystring contains the string “Hi there”, and that’s what appears on the console when the cout line runs. The fancy programmer term for adding something to a string is concatenation. |
|
concatenation
|
adding something to a string
|
|
string first = “hello “;
string second = “there”; string third = first + second; cout << third << endl; |
This code prints the value of third, which is simply the two strings pushed
together, in other words, “hello there”. (Notice the string called first has a space at its end, which is inside quotes and, therefore, part of the string.) |
|
string constant
|
an actual string in your program
surrounded by quotes |
|
add a string constant
|
mystring += "string constant";
mystring = yourString + "string constant"; |
|
how do you index into a string
|
[position number] by placing the position number you wish to access between square brackets
|
|
result = (first == second) ? “equal” : “not equal”;
what does it mean? what does result equal? |
if int first is equal to int second than string result contains "equal" otherwise "not equal"
book: And once again, consider what it means: If first is equal to second, result gets “equal”; otherwise, it gets “not equal”. |
|
Using the Conditional Operator to Do Comparisons example
|
#include <iostream>
using namespace std; int main() { int first = 10; int second = 20; string result; result = first == second ? “equal” : “not equal”; cout << result << endl; return 0; } |
|
result = (first == second) ?
“equal” : “not equal”; using Boolean |
bool isequal;
isequal = (first == second); result = isequal ? “equal” : “not equal”; |
|
bool isequal;
isequal = (first == second); result = isequal ? “equal” : “not equal”; explain. |
1) isqual declared as a boolean variable
bool isequal; 2) The second line sets this to the value first == second. In other words, if first is equal to second, then isequal gets the value true. Otherwise, isequal gets the value false. isequal = (first == second); 3) In the third line, result gets the value “equal” if isequal is true; or result gets the value “not equal” if isequal is false. result = isequal ? “equal” : “not equal”; |
|
Boolean expression
|
that the code requires a Boolean
value. Therefore, you can throw in a Boolean variable if you prefer, because a Boolean variable holds a Boolean value. |
|
bool finished;
|
This declares a Boolean variable called finished. Then, you can either put
a true or a false in this variable, as in the following: finished = true; or finished = false; When you print the value of a Boolean variable by using code like the following cout << finished << endl; you see either a 1 for true or a 0 for false. The reason is that, deep down inside, the computer stores a 1 to represent true and a 0 to represent false. |
|
1
|
true
|
|
0
|
false
|
|
cin
|
- reading from the console
- uses extraction operator (>>) extractinginto the stream |
|
cout uses what operator
|
-- insertion operator (<<)
|
|
#include <iostream>
using namespace std; int main() { string name; cout << “Type your name: “; cin >> name; cout << “Your name is “ << name << endl; return 0; } |
1) string name is declared
2) user input is stored in string name 3) string name is displayed |
|
#include <iostream>
using namespace std; int main() { string name; cout << “Type your name: “; cin >> name; cout << “Your name is “ << name << endl; return 0; } |
Type your name: Fred
Your name is Fred |