Upload
umair
View
212
Download
0
Embed Size (px)
DESCRIPTION
Introduction and information to starting C++ programming and debugging.
Citation preview
C++ Notes - MIT LecturesSource file (—Preprocessor—> Processed Code)(C++) —Compiler—> Object File -Libraries—Linker—>Executable—OS—> Program in Memory
Token type
KeywordsWords with special meaning to the compiler int, double, for, auto
IdentifiersNames of things that are not built into the language cout, std, x, myFunction
LiteralsBasic constant values whose value is specified directly in the source code
"Hello, world!", 24.3,0, ’c’
OperatorsMathematical or logical oper ations +, -, &&, %, <<
Punctuation/SeparatorsPunctuation defining the structure of a program {}(),;
WhitespaceSpaces of various sorts; ig nored by the compiler Spaces, tabs, newlines, com ments
cin >> - data being input into a value cout << - data being output into a value
Conditionals
use if statements and switch-case statements
Operator
> Greater than>= Greater than or equal to< Less than<= Less than or equal to== Equal to!= Not equal to
Operator
&& and|| or! not
Examples using logical operators (assume x = 6 and y = 2):
!(x > 2) → false(x > y) && (y > 0) → true (x < y) && (y > 0) → false (x < y) || (y > 0) → true
IF STATEMENTS
#include <iostream> using namespace std;
int main() {int x = 6;int y = 2;
if(x > y) cout << “x is greater than y\n”;
else if(y > x) cout << “y is greater than x\n”;
else cout << “x and y are equal\n”;
return 0;}
SWITCH-CASE STATEMENTS
#include <iostream>using namespace std;
int main() {int x = 6;
switch(x) { case 1: cout << “x is 1\n”; break; case 2: case 3: cout << "x is 2 or 3"; break; default: cout << "x is not 1, 2, or 3";
}
return 0;}
LOOPS
does certain actions while a condition is being met, infinite
>> Will say x is 10. Will keep adding 1 to x as long as x is < 10. once greater, will spit out the message
if: 1 or noneif-else statements: 1 has to be trueif-else-if: 1 or noneif-else-if-else: 1 has to be true
First gets an expression (in this case x, which is 6). Compares it to the first case, but x is not 1, so moves onSecond case, x is not 2 or 3, so moves onNo other options so goes to the default, which spits “x is not 1, 2, or 3”
#include <iostream>using namespace std;
int main() {int x = 0;
while(x < 10) x = x + 1;
cout << “x is “ << x << “\n”;
return 0;}
Variation of a loop. Will do statements 1 and 2 at least once as long as the condition is being met. Combined with above, will keep doing until x is not less than 10.
FOR
for loop is a loop that addresses change of a function of object.first part is the expression, second part is the while, third is positive or negative change
Here is the expression and variable, x =0. This for loop will document all instances x is less than 10 and spit it out with the formula x + 1. So users will see, 0-9
NESTED CONTROL STATEMENTS
do{statement1statement2... }while(condition);
for(initialization; condition; incrementation){ statement1 statement2 ...
1 #include <iostream>2 using namespace std;34 int main() {56 int x = 0;7 for(; x < 10; x = x + 1)8 cout << x << “\n”;9
#include <iostream> using namespace std;
int main() {
for(int x = 0; x < 10; x = x + 1) cout << x << “\n”;
return 0;}
#include <iostream>using namespace std;
int main() {
int x = 0;while(x < 10) {
cout << x << “\n”;x = x + 1;
}
#include <iostream> using namespace std;
int main() {int x = 6;int y = 0;if(x > y) {
cout << (“x is greater than y\n”;if (x == 6)
cout <<“x is equal to 6\n”;else
cout << “x is not equal to 6\n”;
} elsecout << “x is not greater than y\n”;
return 0;}
#include <iostream>using namespace std;
int main() {for(int x = 0; x < 4; x = x + 1) {
for(int y = 0; y < 4; y = y + 1) cout << y; cout << “\n”;
}return 0; }