CPP Shorthand
C code can be very confusing, and anoying having to put semi-colons to end each line, however it can also be very flexable and fast to program, now im going to show you some short ways to do code which are commonly used later in loops.
It is very common for your to have a variable with a value and you want to add something to do that variable, in basic this means always putting:
myvarname = myvarname + 1
myvarname = myvarname - 1
myvarname = myvarname & "test" '[wont work basic only]
This can get anoying for the lazy typeests like myself
The lazy mans ways
Code:
myvarname += 1; // is the same as myvarname=myvarname+1
myvarname -= 1; // is the same as myvarname=myvarname-1
myvarname *= 1; // is the same as myvarname=myvarname*1
myvarname /= 1; // is the same as myvarname=myvarname/1
But wait theirs an even shorter way!
Incrementation adding and subtracting 1 from a variable is comon in loops, it tells us how many times the loop has been run, which might just be useful...
Code:
myvarname++; // is the same as myvarname=myvarname+1
myvarname--; // is the same as myvarname=myvarname-1
While im on the subject...
The if statement, I said before runs code IF something = true, but wait in C it is infact as simple as that:
if(myname=="martin"){ /* runcode */ }
All 'if' reads is: if(true) or if(false), by that I meen the brackets do the statement and return true or false, you may not even need if at all! for example:
Code:
bool answer = (myname=="martin")
if(answer){ /* run code */
}
You may now congratulate me on doubling the code and calling it a shortcut...
Imagine you have a button and you wanted it to be visible if myname=="martin";? ok so we are doing console and their are no buttons but this is just an example
Old way:
Code:
if(myname=="martin"){
button.visible=true;
} else{
button.visible=false;
}
New way:
Code:
button.visible = (myname=="martin");
Hopefully now you see what I am getting at.... welcome to 'c' code, you may throw away Visual Basic.
Copyright (c) 2008, Martin Sykes.
Learning Center is a branch of Martrinex Systems, Martrinex.net
Do not copy any materials from this site without permission from the auther.
Add Comment
Comments