[Guide] Basic C++ Statements etc.

Posts 113 of 13 · Page 1 of 1
[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
Quote Originally Posted by That0n3Guy View Post
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
Ah, this is good, but I noticed cin and cout.
use cout like this: cout << (variables, expressions, etc. )
use cin like this: cin >> ( same as above. )
you had the << and >> mixed up.
also, at the end of a line of input or output, you should get into the habit of using <<endl or \n
and can you explain what a double is? They confuse me.
The character or char data type holds a numerical value from 0 to 256, and each number stands for a character on a keyboard. Let me sift through my source code collection and see if I can find something showing this.
Quote Originally Posted by t7ancients View Post
Ah, this is good, but I noticed cin and cout.
use cout like this: cout << (variables, expressions, etc. )
use cin like this: cin >> ( same as above. )
you had the << and >> mixed up.
also, at the end of a line of input or output, you should get into the habit of using <<endl or \n
and can you explain what a double is? They confuse me.
The character or char data type holds a numerical value from 0 to 256, and each number stands for a character on a keyboard. Let me sift through my source code collection and see if I can find something showing this.
Character holds value from 0 to 255 (0x00 - 0xFF) . They are basically bytes. Actually 'tis quite surprising that C++ didn't include a byte variable type
Yay! I found it in this massive folder with like 700 different source files in it...
Code:
#include <iostream.h>
int main()
{
using namespace std;
for (int i = 0; i<256; i++)
cout << (char) i; 
getchar(); // waits for a key to be pressed to continue execution of code.
return 0;
}
a double is named for its size in memory which is a DWORD or double word. An int (integer) is a word) so a double is twice the size and allows for floating point operations. I forget how large it can be, but its a pretty large value.
Quote Originally Posted by That0n3Guy View Post
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

These codes are wrong, you have to put "using namespace std;" or "std::cout".
When you give examples you should include all the header files and enclose in a function, as well as give an example of all the variables. Not to be harsh but this would be hard to understand for a beginner.
pixi, you don't really need to put using napespace for anything, when I leave it out it all works fine. I'm using borland 5.5 compiler so maybe it's just that. I haven't seen anyone else on here that uses borland, it's either dev c++ or msvs.
Code:
while(a=1) 
{
a = a+1;
}

for(int i = 0; i <= 10; i++)
{
cout << i << endl;
}
shouldn't while(a=1) be while(a==1) because it is checking a value and not changing one.

and replace a = a+1; by a++; does the same but looks pro...

a +=1 is possible too
Quote Originally Posted by lalakijilp View Post
Code:
while(a=1) 
{
a = a+1;
}

for(int i = 0; i <= 10; i++)
{
cout << i << endl;
}
shouldn't while(a=1) be while(a==1) because it is checking a value and not changing one.

and replace a = a+1; by a++; does the same but looks pro...

a +=1 is possible too
an incrementing value has less margin for error, so a++ would be more efficient. looking good is nice and all, but shouldn't practicality hold more priority? and a = 1 as a parameter in a while loop doesn't change the value of a to 1. a = 1 does the EXACT same thing as a == 1 in a loop parameter and takes less to type. you seem like you've confused the syntax and rules of c++ with c#.
Quote Originally Posted by t7ancients View Post
and a = 1 as a parameter in a while loop doesn't change the value of a to 1. a = 1 does the EXACT same thing as a == 1 in a loop parameter and takes less to type.
lalakijilp is right. It changes the value of a to 1, and if that succeeded it continues with the loop.

Code:
int _tmain(int argc, _TCHAR* argv[])
{
	int a=1337;
	bool bdone=false;
	while(a=2)
	{
		cout<<a<<endl;
		if(bdone==true)
		{
			system("pause");
			return 1;
		}
		bdone=true;
		Sleep(1);
	}
	return 0;
}
I say this is absolutely easy. Therefore, if people don't understand this then I don't know what they will.

I suggest you continue this, and it should be added to the C++ Tut thread under: for beginners as a reference for other people that are just starting C++. It's simple, and to the point.
Perfect for a noob like me.
Posts 113 of 13 · Page 1 of 1
This thread is closed for replies.

Similar Threads

Tags for this Thread

None

Need help?