We are still actively working on the spam issue.

Difference between revisions of "C++ for new friends"

From InstallGentoo Wiki
Jump to: navigation, search
(fake & gay)
(Tag: Blanking)
(Undo revision 49951 by Mootroot (talk))
(Tag: Undo)
Line 1: Line 1:
 +
'''It is recommended to use an IDE, for a tutorial on how to set one up click''' [http://www.cprogramming.com/code_blocks/ HERE] <br />
 +
Other great resource for learning [http://www.learncpp.com/ C++] from the ground up.
  
 +
 +
== Printing text to the screen ==
 +
 +
<nowiki>
 +
//Text that starts with "//" is ignored by the compiler
 +
//This is the library that allows you to use the "cout" and "endl" objects
 +
#include <iostream>
 +
 +
// int main is where you will be writing your code example
 +
// int main(){code goes here}
 +
int main()
 +
{
 +
// std::cout is sending the text to your screen
 +
// std::endl creates a new line
 +
// note: 'std::' means you are using a member of the namespace 'std'
 +
// more on that later.
 +
std::cout << "This text will appear on your sceen" << std::endl;
 +
std::cout << "This text will appear on your sceen" << std::endl;
 +
// note: all statements must end with a ; such as above
 +
 +
 +
return 0;
 +
}
 +
</nowiki>
 +
 +
----
 +
 +
 +
== Using Data ==
 +
 +
<nowiki>
 +
#include <iostream>
 +
 +
int main()
 +
{
 +
// Numbers in C (or C++) can be stored in variables
 +
// Each type can store a different type of data
 +
// For example an int is created by typing keyword int followed by a name, for this example bill
 +
int bill; //this int can store Integers -3 -2 -1 0 1 2 3 etc
 +
bool tom; //can store true or false
 +
float jim; //can store a decimal
 +
char tommy; //this takes characters such as 'x' or 'b'
 +
 +
// variables can be created with no data and set later in the program such as here
 +
bill = 5;
 +
tom = true;
 +
jim = 1.5;
 +
tommy = 'b';
 +
 +
// just to prove everything works we will print these to the screen
 +
std::cout<< "BIL "<<bill<<std::endl;
 +
std::cout<< "TOM "<<tom<<std::endl;
 +
std::cout<< "JIM "<<jim<<std::endl;
 +
std::cout<< "TOMMY "<<tommy<<std::endl;
 +
// note: tom will appear as 1 or 0 depending on the true/false state
 +
 +
return 0;
 +
}
 +
</nowiki>
 +
 +
----
 +
 +
== Conditions ==
 +
 +
<nowiki>
 +
#include <iostream>
 +
 +
int main()
 +
{
 +
 +
int bill = 5;
 +
char tommy = 'b';
 +
// the if statement will look if the condition inside its brackets is true,
 +
// if it is it will run the block of code below
 +
// we are using the Equal to operator "==" to check if bill is equal to 5
 +
 +
// other operators include
 +
// != Not equal to
 +
// > Greater than
 +
// <= Less than or equal to
 +
// >= Greater than or equal to
 +
 +
if(bill == 5)
 +
{
 +
tommy = 'X';
 +
// because bill is equal to 5 tommy will become X
 +
}
 +
 +
if(bill == 6)
 +
{
 +
tommy = 'Y';
 +
//because bill is not equal to 6 this code is ignored
 +
}
 +
 +
std::cout<< "TOMMY "<<tommy<<std::endl;
 +
 +
return 0;
 +
}
 +
</nowiki>
 +
 +
----
 +
 +
== Loops ==
 +
 +
<nowiki>
 +
#include <iostream>
 +
 +
int main()
 +
{
 +
int bill = 5;
 +
char tommy = 'b';
 +
 +
// like if, the while loop will run code if the condition is true
 +
// unlike if, while loops do not stop until the condition becomes false
 +
// below we used the Not equal to operator to check if bill is not equal to 0
 +
while (bill != 0)
 +
{
 +
//while bill is not equal to 0 the loop will run and print out tommy
 +
std::cout<< "TOMMY "<<tommy<<std::endl;
 +
 +
//the loop will also reduce bill by 1  each time it runs:
 +
bill--;
 +
//thanks to the decrement operator "bill--" bill could also be Incremented with "bill++"
 +
}
 +
 +
return 0;
 +
}
 +
</nowiki>
 +
 +
[[Category:Programming]]
 +
[[Category:Tutorials]]
 +
[[Category:HowTo]]
 +
[[Category:Programming languages]]

Revision as of 00:53, 24 November 2020

It is recommended to use an IDE, for a tutorial on how to set one up click HERE
Other great resource for learning C++ from the ground up.


Printing text to the screen

//Text that starts with "//" is ignored by the compiler 
//This is the library that allows you to use the "cout" and "endl" objects
#include <iostream>

// int main is where you will be writing your code example
// int main(){code goes here}
int main()
{
	// std::cout is sending the text to your screen
	// std::endl creates a new line
	// note: 'std::' means you are using a member of the namespace 'std'
	// more on that later.
	std::cout << "This text will appear on your sceen" << std::endl;
	std::cout << "This text will appear on your sceen" << std::endl;
	// note: all statements must end with a ; such as above


	return 0;
}



Using Data

#include <iostream>

int main()
{
	// Numbers in C (or C++) can be stored in variables
	// Each type can store a different type of data
	// For example an int is created by typing keyword int followed by a name, for this example bill
	int bill; //this int can store Integers -3 -2 -1 0 1 2 3 etc
	bool tom; //can store true or false
	float jim; //can store a decimal
	char tommy; //this takes characters such as 'x' or 'b'

	// variables can be created with no data and set later in the program such as here
	bill = 5;
	tom = true;
	jim = 1.5;
	tommy = 'b';

	// just to prove everything works we will print these to the screen
	std::cout<< "BIL "<<bill<<std::endl;
	std::cout<< "TOM "<<tom<<std::endl;
	std::cout<< "JIM "<<jim<<std::endl;
	std::cout<< "TOMMY "<<tommy<<std::endl;
	// note: tom will appear as 1 or 0 depending on the true/false state 

	return 0;
}


Conditions

#include <iostream>

int main()
{

	int bill = 5;
	char tommy = 'b';
	// the if statement will look if the condition inside its brackets is true,
	// if it is it will run the block of code below
	// we are using the Equal to operator "==" to check if bill is equal to 5

	// other operators include 
	// !=	Not equal to
	// >	Greater than
	// <=	Less than or equal to
	// >=	Greater than or equal to

	if(bill == 5)
	{
		tommy = 'X';
		// because bill is equal to 5 tommy will become X
		}

	if(bill == 6)
	{
		tommy = 'Y';
		//because bill is not equal to 6 this code is ignored
	}

	std::cout<< "TOMMY "<<tommy<<std::endl;

	return 0;
}


Loops

#include <iostream>

int main()
{
	int bill = 5;
	char tommy = 'b';

	// like if, the while loop will run code if the condition is true
	// unlike if, while loops do not stop until the condition becomes false
	// below we used the Not equal to operator to check if bill is not equal to 0
	while (bill != 0)
	{
		//while bill is not equal to 0 the loop will run and print out tommy
		std::cout<< "TOMMY "<<tommy<<std::endl;

		//the loop will also reduce bill by 1  each time it runs:
		bill--;
		//thanks to the decrement operator "bill--" bill could also be Incremented with "bill++"
	}

	return 0;
}