C++ Cheat Sheet
C++ Cheat Sheet
C++ Cheat Sheet
/programname Beggin with #include <iostream> using namespace std; int main()
Basic Commands:
cout<< Text Here \n; cin>> Variable that you are assigning user input to; cin.ignore();
Loops:
For Loops: for ( variable initialization; condition; variable update ) { Code to execute while the condition is true; } example: for ( int x = 0; x < 10; x++ ) { cout<< x <<endl; } While Loops: declare variable while ( condition ) { code to execute while condition is true; } example: int x=0; while ( x < 10 ) { cout<< x <<endl; x++; } Do..While Loops: declare variable;
do {
Switch Case: switch ( <variable> ) { case this-value: Code to execute if <variable> == this-value break; case that-value: Code to execute if <variable> == that-value break; ... default: Code to execute if <variable> does not equal the value following any of the cases break; } example: #include <iostream> using namespace std; int main() { int day; day = 2 switch (day) { case 1 : cout<< "Sunday \n"; break; case 2 : cout<< "Tuesday \n"; break; case 3 : cout<< "Tuesday \n"; break; default : cout<< "Not an allowable day \n"; break; }
Short Cuts
typedef variable type shortcut; example typedef unsigned short int USHORT; USHORT age; USHORT weight;
Thing to understand:
Prefix and Postfix increments: 5: int main() 6: { 7: int myAge = 39; // initialize two integers 8: int yourAge = 39; 9: cout << "I am: " << myAge << " years old.\n"; 10: cout << "You are: " << yourAge << " years old\n"; 11: myAge++; // postfix increment 12: ++yourAge; // prefix increment 13: cout << "One year passes...\n"; 14: cout << "I am: " << myAge << " years old.\n"; 15: cout << "You are: " << yourAge << " years old\n"; 16: cout << "Another year passes\n"; 17: cout << "I am: " << myAge++ << " years old.\n"; 18: cout << "You are: " << ++yourAge << " years old\n"; 19: cout << "Let's print it again.\n"; 20: cout << "I am: " << myAge << " years old.\n"; 21: cout << "You are: " << yourAge << " years old\n"; 22: return 0; 23: } Output: I am 39 years old You are 39 years old One year passes I am 40 years old You are 40 years old Another year passes I am 40 years old You are 41 years old Let's print it again I am 41 years old You are 41 years old