hello friends! new(ish)!

C++ for new friends: Difference between revisions

From InstallGentoo Wiki v2
Jump to navigation Jump to search
>Mrsnooze
m (category:programming languages)
(I fixed the formatting to be consistent.)
 
(2 intermediate revisions by 2 users not shown)
Line 32: Line 32:
== Using Data ==
== Using Data ==


  <nowiki>
   
#include <iostream>
#include <iostream>
 
int main()
int main()
{
{
// Numbers in C (or C++) can be stored in variables
// Numbers in C (or C++) can be stored in variables
// Each type can store a different type of data
// 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
// 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
int bill; //this int can store Integers -3 -2 -1 0 1 2 3 etc
bool tom; //can store true or false
bool tom; //can store true or false
float jim; //can store a decimal
float jim; //can store a decimal
char tommy; //this takes characters such as 'x' or 'b'
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
// variables can be created with no data and set later in the program such as here
bill = 5;
bill = 5;
tom = true;
tom = true;
jim = 1.5;
jim = 1.5;
tommy = 'b';
tommy = 'b';
 
// just to prove everything works we will print these to the screen
// just to prove everything works we will print these to the screen
std::cout<< "BIL "<<bill<<std::endl;
std::cout << "BIL " << bill <<std::endl;
std::cout<< "TOM "<<tom<<std::endl;
std::cout << "TOM " << tom <<std::endl;
std::cout<< "JIM "<<jim<<std::endl;
std::cout << "JIM " << jim <<std::endl;
std::cout<< "TOMMY "<<tommy<<std::endl;
std::cout << "TOMMY " << tommy <<std::endl;
// note: tom will appear as 1 or 0 depending on the true/false state  
// note: tom will appear as 1 or 0 depending on the true/false state  
 
return 0;
return 0;
}
}
</nowiki>


----
----
Line 66: Line 65:
== Conditions ==
== Conditions ==


  <nowiki>
   
#include <iostream>
#include <iostream>
 
int main()
int main()
{
{
 
int bill = 5;
int bill = 5;
char tommy = 'b';
char tommy = 'b';
// the if statement will look if the condition inside its brackets is true,
// the if statement will look if the condition inside its brackets is true,
// if it is it will run the block of code below
// 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
// we are using the Equal to operator "==" to check if bill is equal to 5
 
// other operators include  
// other operators include  
// != Not equal to
// != Not equal to
// > Greater than
// > Greater than
// <= Less than or equal to
// <= Less than or equal to
// >= Greater than or equal to
// >= Greater than or equal to
 
if(bill == 5)
if(bill == 5)
{
{
tommy = 'X';
tommy = 'X';
// because bill is equal to 5 tommy will become X
// because bill is equal to 5 tommy will become X
}
}
 
if(bill == 6)
if(bill == 6)
{
{
tommy = 'Y';
tommy = 'Y';
//because bill is not equal to 6 this code is ignored
//because bill is not equal to 6 this code is ignored
}
}
 
std::cout<< "TOMMY "<<tommy<<std::endl;
std::cout << "TOMMY " << tommy <<std::endl;
 
return 0;
return 0;
}
}
</nowiki>


----
----
Line 106: Line 104:
== Loops ==
== Loops ==


  <nowiki>
   
#include <iostream>
#include <iostream>
 
int main()
int main()
{
{
int bill = 5;
int bill = 5;
char tommy = 'b';
char tommy = 'b';
 
// like if, the while loop will run code if the condition is true
// 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
// 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
// below we used the Not equal to operator to check if bill is not equal to 0
while (bill != 0)
while (bill != 0)
{
{
//while bill is not equal to 0 the loop will run and print out tommy
//while bill is not equal to 0 the loop will run and print out tommy
std::cout<< "TOMMY "<<tommy<<std::endl;
std::cout << "TOMMY " <<tommy <<std::endl;
 
//the loop will also reduce bill by 1  each time it runs:
//the loop will also reduce bill by 1  each time it runs:
bill--;
bill--;
//thanks to the decrement operator "bill--" bill could also be Incremented with "bill++"
//thanks to the decrement operator "bill--" bill could also be Incremented with "bill++"
}
}
return 0;
}


return 0;
}
</nowiki>


[[Category:Programming]]
[[Category:Programming]]

Latest revision as of 04:14, 23 April 2025

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;
}