We are still actively working on the spam issue.
Difference between revisions of "C++ for new friends"
(needs moar tabs, stylistic fixups (CONSISTENCY!), et al) |
|||
Line 10: | Line 10: | ||
#include <iostream> | #include <iostream> | ||
− | //int main is where you will be writing your code example | + | // int main is where you will be writing your code example |
− | //int main(){code goes here} | + | // int main(){code goes here} |
int main() | int main() | ||
{ | { | ||
− | //std::cout is sending the text to your screen | + | // std::cout is sending the text to your screen |
− | //std::endl creates a new line | + | // std::endl creates a new line |
− | std::cout << "This text will appear on your sceen" << std::endl; | + | // note: 'std::' means you are using a member of the namespace 'std' |
− | std::cout << "This text will appear on your sceen" << std::endl; | + | // 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; | + | return 0; |
} | } | ||
Line 38: | Line 40: | ||
int main() | int main() | ||
{ | { | ||
− | //Numbers in 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 | |
− | + | bill = 5; | |
− | + | tom = true; | |
− | + | jim = 1.5; | |
+ | tommy = 'b'; | ||
− | //note tom will appear as 1 or 0 depending on the true/false state | + | // 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; | |
− | return 0; | ||
} | } | ||
Line 76: | Line 77: | ||
{ | { | ||
− | 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 | |
+ | // != 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; | + | return 0; |
} | } | ||
Line 122: | Line 119: | ||
int main() | 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) | ||
+ | { | ||
+ | std::cout<< "TOMMY "<<tommy<<std::endl; | ||
+ | //while bill is not equal to 0 the loop will run and print out tommy | ||
+ | bill--; | ||
+ | //the loop will also reduce bill by 1 each time it runs | ||
+ | //thanks to the decrement operator "bill--" bill could also be Incremented with "bill++" | ||
+ | } | ||
− | + | return 0; | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | return 0; | ||
} | } | ||
Revision as of 22:05, 4 February 2014
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) { std::cout<< "TOMMY "<<tommy<<std::endl; //while bill is not equal to 0 the loop will run and print out tommy bill--; //the loop will also reduce bill by 1 each time it runs //thanks to the decrement operator "bill--" bill could also be Incremented with "bill++" } return 0; }