Вы находитесь на странице: 1из 2

Headers Logic & Math Operators Output Settings Loops

#include<iostream> += -= *= /= || && cin.ignore(1000,‘\n’) for (strt;tst;cnt){}


#include <string> Variable Types cout.precision(x) do {} while (test)
#include <cctype> double int char string cout.setf(ios::fixed) while (test) {}
using namespace std const type var_name Switch Chars
int main() {} I/O switch (var) {} s.size() s[k]
Comparative Operators cout << var cin >> var case x: action isdigit() isalpha()
== != > >= < <= getline (cin, var) break isupper() islower()

Header: Variable types Geometric patterns with nested loops


#include<iostream> short Integer -32,768 to 32767 *
#include<string> int Integer -2.14M to 2.14M **
#include<cctype> long Integer -2.14M to 2.14M ***
using namespace std; float Number -1038 to 1038 ****
int main(){} double Number -10308 to 10308
char int max = 4;
Single char in ‘’: ‘a’, ‘\n’, ‘&’
Variable initialization and input: bool for (int i=1; i<=max; i++)
true, false
int count = 0; {
string Sequence of char in “”: “ab cd”
cin >> count; for (int j=1; j<=i;
cin.ignore(10000,‘\n’); j++)
Headers {
string text = “”; #include<string> to use strings. cout << “*”;
getline(cin,text);` #include<cctype> to use isalpha, etc. }
cout << endl;
If, else statement: Declaring / initializing a variable }
int x = 0; Var names can only contain letters,
if (x == 0) numbers and underscores. ****
cout << “x is 0”; Var names cannot begin with numbers. ***
else Example: a, text, count, _i, count_i **
cout << “x is not 0”; *
If var is declared within a branch of an if
Switch statement: statement or within a loop, var can only be int max = 4;
switch (x) used within the statement. for (int i=max; i>=1; i--)
{ {
case 1: cout << “A”; Integer overflow is when value is too large for (int j=1; j<=i;
break; to be represented by int, etc. j++)
case 2: cout << “B”; {
break; A char value cannot be empty. cout << “*”;
case 3: int m = 5.6 will store 5.6 in m. }
case 4: cout << “C”; int m = 11/5 will store 2 in m. cout << endl;
break; int m = 2.6/0.5 will store 5 in m. }
default: cout << “D”;
} Operators Common Errors
&& takes priority over ||. • **MISSING SEMICOLON**
For loop: ! also applies to true/false functions. • Unmatched semicolons
for (int i = 0, i < 5, i++) Example: !isalpha • Unmatched quotes
{} • = instead of ==
Flow Control • Division of integers (e.g. 5/2 = 2)
Nested for loops: Parameter can only be int or char. • Declaring var inside loop
for (int i = 0, i < 5, i++) Common loop numbers: • Using undeclared variable
{
Loop Parameters Reps • , instead of ; in for parameters
for (int j = 0, j <= i,
int i = 0; i <= 49; i++ 50 • Forgetting to break in switch
j++)
int i = 0; i < 49, i++ 49 • Infinite loop (runtime error)
{}
int i = 0; i >= 0, i-- 50 • Empty char (compilation error)
}
The program will still compile with an • Type mismatch (int x = 2.99)
While loop: infinite loop, however it will give a
int i = 0 nonsensical answer. String / Char Sequences
while (i < 5) \n New line
{ Chars \t Tab
i++; For string “abcdefg hijk”: \\ Backslash
} • s.size() is 12 \’ Single quote
• s[0] is ‘a’ \” Double quote
Do-while loop: • s[1] is ‘b’
int i = 0; • s[7] is ‘ ’ Boolean Operators
do In a loop, using (int i = 0; i != if ( (score >= 0) && (score
{ <=10)
s.size(); i++) will end loop with
i++; Is true if score is larger or equal to 0 and
string length number of repetitions.
} while (i < 5); small or equal to 1
More cctype functions: myword[] = "Bye";
toUpper() to make character uppercase
toLower() to make character lowercase User input:
+ operator can append two strings. cin.getline(s,50)

Reference Functions: CString functions:


void addOne(int &y) Using #include <cstring>:
{ strcat(dest, source)
y = y + 1; Appends source to dest.
}
strcpy(dest, source)
Only used to pass integer / double arguments, not needed for Copies source to dest (replaces).
strings / arrays.
strcmp(str1, str2)
Swap: Compares str1 to str2.
void swap(int& x, int& y) If output is 0, str1 == str2.
{ If output is >0, str1 > str2.
int t = x; If output is <0, str1 < str2.
x = y;
y = t; strlen(str)
} Outputs number of characters between beginning and null
terminator.
Arrays
Parameter in function is defined by var[]: An array of cstrings is declared by:
int function(string a[]) char wordArray[100][10]
Calling a function with array parameter:
function(a) Declaring array of cstrings as a parameter:
int function(char words[][MAXWORDLEN+1])
Arrays can be declared like this:
int foo[] = { 10, 20, 30 }; strcpy:
Assumes size of 3. char s1[20];
int bar [5] = { 10, 20, 30 }; char s2[20] = "Another new string";
Empty elements are set to default values (normally 0). strcpy(s1, ""); // Contents of s1 changed to
Position values start from 0, for example: null string
cout << bar[1] prints out 20. strcpy(s1, "new string"); // Contents of s1
changed to "new string"
2D arrays: foo[row][column] strcpy(s1, s2); // Contents of s1 changed to
When passing a 2D array to a function, when declaring the array "Another new string"
parameter
• Leave the first pair of square brackets empty strcat:
char s1[20] = "Hello";
• Supply the actual declared size for the remaining
char s2[20] = "friend";
dimensions
strcat(s1, ", my "); // s1 now contains
"Hello, my "
Bubble sort:
strcat(s1, s2); // s1 now contains
for (int i = 0; i < (n-1); i++) // Bubble
"Hello, my friend"
sort to sort in ascending order.
{
string store = "";
for (int k = 0; k < (n-i-1); k++)
if (a[k] > a[k+1]) // Switch
neighbours if current is larger then next.
{
store = a[k];
a[k] = a[k+1];
a[k+1] = store;
}
}

CStrings
Declaring strings as a sequence of characters.
“Hello” is a string literal, but can also be expressed by a sequence
of 5 characters + the null terminator (\0) (6 elements for 5 letter
word).

Declarations:
char myword[] = { 'H', 'e', 'l', 'l', 'o',
'\0' };
char myword[] = "Hello";

Arrays cannot be assigned values, following is invalid:

Вам также может понравиться