[Guide] Basic C++ Statements etc.
Info:
I'm going to show and explain some of the basics of C++ for all the new people that want to learn something. Should my information be incorrect, feel free to correct me.
1. The Main() Function
Info:
This is the function that happens the second your program starts and is USUALLY the last piece of code that runs, also.
Example: (int comes before main() because it returns an integer value)
Code:
int main()
{
cout << "Hello World!";
system("pause >nul");
return 0;
}
2. Variables
Info:
Variables store information for use during your program.
Types of Variables:
Integers - Store numbers ONLY.
Boolean - True or False.
Char - Stores ONE character
Float - Integers are great for counting whole numbers, but sometimes we need to store very large numbers, or numbers with a fractional component. A floating point type variable is a variable that can hold a real number, such as 4.0, 2.5, 3.33, or 0.1226. (Credits to
Learn C++ - 2.5 — Floating point numbers since I couldn't think of a way to explain it.)
Strings - Store multiple characters. (Strings require an additional header to be defined before use.)
3. Basic Output/Input
Info:
Basic statements that allow you to write something to the console window or recieve user input for the value of a variable.
Examples:
This writes Hello World! to the console window:
Code:
cout << "Hello World!";
This receives user input for the variable x:
Code:
int x;
cout << "What is x?";
cin << x;
4. If Statement
Info:
Allows you to check if a boolean is true (or even false, in some cases) and if so, do a piece of code.
Example:
Code:
int a = 1;
if(a = 1)
{
cout << "Integer A is equal to one!";
}
5. Loop Statements
Info:
Loops allow you to continuously do something until you get the wanted result. (Like +1 until the variable is 10)
Examples:
Code:
while(a=1)
{
a = a+1;
}
for(int i = 0; i <= 10; i++)
{
cout << i << endl;
}
If you just try to pop these codes into your compiler, they won't do anything for you without proper use and proper headers being defined. This is just meant to show newer C++ coders some of the fundamentals of C++ to get started with. If you have any suggestions, please post them