- Code: Select all
if(true) // if the statement is true do the following. This
//could be a simple math equation like if(2=1+1)
{ //this is a scope bracket, variables defined here only work here.
int a; // always add a semi colon after almost everything. This is an integer
//variable, also known simply as a whole number variable.
float b; // this is a decimal number, it holds decimals with whole numbers,
//look up double on google
// This is white space, it doesn't mean anything to the
//compiler, you can have everything written on one line and
//it would mean the same
// thing to the compiler. Those // are comments, anything
//in front of them will make a comment also /* */ makes
//comments.
/* written like this*/
a = 1; // Assign an integer variable the value of 1
b = .5; // Assign a decimal value to a float variable.
while(1) { break;//do this} // This is a while loop, you will almost
// never need to use these. They create a loop
// until the value is false. This one would
// be infinite but I put a break statement to make
// sure the loop doesn't continue forever. But this
// loop will always be true
if(a) { break;} // another if statement with a variable, the value is
// 1 = TRUE. If a = 0 then the if wouldn't do anything
// because its false. break; means
// break out of the loop. Good for while statements.
for(a = 0; a < 10; ++a) { /* do this stuff 10 times*/} // for loop, this
// is you most useful loop. It allows for iterations
// and making redundant tasks automated.
} // end scope
Thats pretty simple stuff to understand, but also thats nearly everything you need to make good code. Almost everything in programming is some sort of loop and mathmatical equations that use data structures.
There are other useful functions available but to make good code first comes making a good design of what needs to be done.
You ask yourself, "What do I need to do to make this happen?" then you write out psuedo code that helps you figure it out, then you write the actual code, then debug the code.
70% design, 15% coding, 120% debugging... thats programming.