This repository is the basic things for C++
// iostream stands for input output stream
#include <iostream>
main ()
{
//cout stands for Console Output, "this is test" will be sent to the Console Output
cout << "this is test";
}
This won't be able to run cause "cout" is inside a namespace, you need to add "using namespace" or "std::", see the following code:
// iostream stands for input output stream
#include <iostream>
using namespace std;
main ()
{
//cout stands for Console Output, "this is test" will be sent to the Console Output
cout << "this is test";
}
// iostream stands for input output stream
#include <iostream>
main ()
{
//cout stands for Console Output, "this is test" will be sent to the Console Output
std:: cout << "this is test";
}
Now you can run your code :)
- Variables can't have the same name
- Variables can't start from the number
- We can't use spaces
- Our variables should be self-descriptive
- Variables can't be constructed of special characters, keywords
- Variables should be nouns
//short means short integer
short
//float allocates 4 bytes of memory
float
//double allocates a bytes of memory
//double can save more digits after the point
double
//char is the abbreviation of the character
char
//boolean | true or false, so false is always 0, every other number is true, 1 represents true
bool
- Addition operation
- Substraction operation
- Multiplication
- Division
- Incrementation
- Decrementation
- POST Incrementation
- PRE Incrementation
- POST Decrementation
- PRE Decrementation
1. The result of relation operation is always 0 or 1. 1 represents true, just like in bool variable. 0 represents false
2. We can also use exclamation marks to change value from true to false
- AND conjunction: &&
- OR disjunction: ||
- Bitwise AND --> &
- Bitwise OR --> |
- Bitwise NOT --> ~ (tilde)
- Bitwise XOR --> ^ (caret) exclusive OR
- Bitwise left shift <<
- Bitwise right shift >>
- Variable types you can use:
- int
- char
- Variable types you can't use:
- double
- string
You can't use "double" cause it has some precision number after the number, and it's hard to compare
- If a is greater than b, then assign the message that is in " ". Else, a is less or equal to b
(a > b) ? "a > b" : "a <= b"
- If e is greater than f, then chose e, else chose f. After chosing the largest one, then add 10 to it.
((e > f ? e : f) + 10 )
- Add: +
- Substract: -
- Multiply: *
- Divide: /
The way of naming an array: TYPE NAME[SIZE_NUMBER_OF_ELEMENTS]
- int array[4]
int biArrays[3][4] = {0};
//checking the address of the array
//The address of biArrays[0] and biArrays[0][0] are the same
cout << &biArrays[0] << endl;
cout << &biArrays[0][0] << endl;
//infinite loop
for (;;)
cout << "lala" << endl;
const int SIZEOFARRAY = 10;
int q = 0;
int array[SIZEOFARRAY];
//We can get the same result using the methods below
//1.
while (q < SIZEOFARRAY)
{
array[q] = 10 * q;
cout << array[q] << endl;
q++;
}
//2.
while (q < SIZEOFARRAY)
{
array[q] = 10 * q;
cout << array[q++] << endl;
}
This won't be executed at all
while (q)
cout << "lalala";
do
{
cout << "lala";
}
while(q);
//15 rows and 12 columns
for (int i = 1; i <= 15; i++)
{
for (int j = 1; j <= 12; j++)
{
cout.width(4);
cout << i * j;
}
cout << endl;
}
Everything after BREAK WON'T be executed and we are leaving the actual loop
for (int i = 1; i <= 10; i++)
{
if(i == 5)
break;
for (int j = 1; j <= 10; j++)
{
cout.width(4);
cout << i*j;
}
cout << enddl;
}
for (int i = 1; i <= 10; i++)
{
for (int j = 1; j <= 10; j++)
{
if (j == 5)
break;
cout.width(4)
cout << i*j;
}
cout << endl;
}
Everything after CONTINUE instruction WON'T be executed but loop won't end because of it (we will skip the codes after CONTINUE)
for (int i = 1; i <= 10; i++)
{
if (i == 5)
continue;
for (int j = 1; j <= 10; j++)
{
cout.width(4)
cout << i * j;
}
cout << endl;
}
for (int i = 1; i <= 10; i++)
{
for (int j = 1; j <= 10; j++)
{
if (j ==5)
continue;
cout.width (4);
cout << i * j;
}
cout << endl;
}
Global variables can be used everywhere, but local variables only can be used in the bracket
double add (double a, double b) {return a + b;}
What return does? What does it mean to return value?
You return values FROM function, not TO the function.
When computer invokes a function so when it meets a function name with added parentheses so something like that:
functionName();
computer JUMPS to the declaration of the function.
So invoking means jumping and executing code from body (between curly brackets) of function. Computer invokes every instruction inside its body and then it comes back to the place of invocation (name of the function). The process of coming back is called returning. return instruction allows you to return with a value and relpace the invoked function with the returned value.
So when you have a function for example:
int addition (int a, int b)
{
int result;
result = a + b;
return result;
}
and you invoke it like that:
addition (4, 5);
Computer jumps to the body of the function. Executes all instructions from line 3-4. And when it "meets" return keyword it returns to the place where the function was invoked. When computer returns to that place it replaces name of the function including everything that was inside parentheses by the value that was returned.
In our case, addition(4, 5) is gonna be replaced by 9, because 4 + 5 is equal to 9.
So when you write:
addition (4, 5);
You are really writing as a result:
9;
That's why when you write:
cout << addition (4, 5);
It's the same as if you had written:
cout << 9;
To sum up:
return keyword allows you to return to the place where function was invoked and return the value that is placed after return keyword.