![]() |
|
#1
|
||||
|
||||
|
Variables Loops Formatted Text Sample code: Spoiler for Simple PC Calculator: Spoiler for Guess the number game: Spoiler for Encryption/Decryption: Hope these have helped people
__________________
Last edited by Furypaw; 03-29-2009 at 01:31 AM. |
|
#2
|
||||
|
||||
|
Variables
There are a few different kinds of variables. The ones I will cover here are 'int', 'float', 'double' and 'char'. I will cover more advanced ones like arrays (which char variables actually are but I won't go there now) or structs if enough people ask. int int is your average integer variable. It can contain numbers between -2147483648 to 2147483647 (the same as long, although you don't have to specify long like unsigned or short), 0 to 4294967295 when you put 'unsigned' before int and -32768 to 32767 with short or 0 to 65535 unsigned short. None of these can use decimals, although float can. It can be used like this: Code:
int variable; Code:
int variable = 9812; float float is very much like int, although it can use decimals. It is precise to 7 digits. It looks like this: Code:
float variable; Code:
float variable = 238.2717 double There isn't much to say for double, except that is is the same as float, apart from the 15 digit precision. It is used like this: Code:
double variable; Code:
double variable = 238.271792752545 char The char variable can hold letters, or strings. It's range is about -128 to 127, about the amount of letters on your keyboard. It is used like this: Code:
char variable[8]; )it can also be initialised: Code:
char variable[8] = "JimBob";
__________________
Last edited by Furypaw; 03-12-2009 at 12:42 AM. |
|
#3
|
||||
|
||||
|
Loops
There are two different loops that I am currently aware of. The first is the 'for' loop, the second is the 'while' loop. for: The for loop is simple although it is slightly more powerful in my opinion then the while loop. it's basic structure is this: Code:
for(1;2;3){
//write your awesome stuff here. Nao.
}
2:This is where you set the condition for the loop e.g count < 0;. leave blank for a never ending loop (or until you use break) 3:What happens every time the user goes through the loop e.g count ++; While: The while loop is simple, and can be used if you don't need anything to happen every time it loops etc. It looks like this: Code:
while(1){
//I said write your epic stuff here!
}
do-while loops These loops are simply backwards while loops, and are always executed at least once. it looks like this: Code:
do{
//your stuff here
}
while(thisthingy == 1);
break break is a simple function that simply exits the loop you are currently in, it makes it easier to get out of infinite loops without having defined a way of exiting normally. e.g. Code:
int count = 0;
while(1){
printf("infinite loop :D");
count ++;
if(count == 15)break;
}
//This is where break leaves you
continue continue is very similar to break, it simply returns you to the start of the loop without executing anything after it, e.g. Code:
while(1){
//your code here, continue; returns you to here
continue;
//this won't be executed.
}
Thanks to lokiare1 for reminding me of break; and continue;, and Xitherun/TacticalBread for reminding me of do-while loops.
__________________
Last edited by Furypaw; 03-14-2009 at 02:59 PM. |
|
#4
|
||||
|
||||
|
Formatted text
There are several thing you can do with formatted text inside printf (or sprintf,as printf isn't that great on PSP). %.2f This is the same as %f (to show float variables) although it rounds it to 2 d.p (decimal points for all you math dropouts )%9s This can be used in displaying a variety of variables, here strings (or chars). What is does is it right justifies it to 9 characters along. Escape characters Escape characters are used when a certain character is used for something else, like \, which is an escape character in itself, so you go \\ to show it in printf etc. \n, is a newline, \t, is a tab, and there are many others that I simply can't remember at the moment.
__________________
Last edited by Furypaw; 03-11-2009 at 11:33 PM. |
|
#5
|
||||
|
||||
|
what about do...while loops?
lol, if those are even worth talking about. if you need moar room for reserved posts, let me know.
__________________
|
|
#6
|
|||
|
|||
|
Quote:
I wasnt gonna post for atleast 30 minutes but then i saw u come in. Anyways i jus wanted to say "Can I Haz C++ Now :-p" |
|
#7
|
||||
|
||||
|
Ah... I forgot about that, it's pretty much a backwards while loop hold up
__________________
|
|
#8
|
||||
|
||||
|
Quote:
lol. anyway, nice guide.
__________________
|
|
#9
|
|||
|
|||
|
TacticalBread you freaking Javatard..... Javatards fucking up C++ Tutorials since 1995.
No but seriously, Id like to learn C++ and ive had trouble in the past soo. This is fucking awesome. |
|
#10
|
||||
|
||||
|
Quote:
![]() Code:
variable[0] variable[1] variable[2] variable[3] variable[4] variable[5] variable[6] variable[7]
__________________
|
![]() |
|
|
|||
|
|||
|
|
| Thread Tools | |
| Display Modes | |
|
|