Completion of the download , we will now start to code , grab a copy of codeblocks-mingw32.exe from http://sourceforge.net/projects/codeblocks/files/Binaries/13.12/Windows/codeblocks-13.12mingw-setup.exe. First of all let's get to know the syntax of C++
First see the code below , this is the simplest code , which does nothing.
#include <iostream>
int main(){
int gordon = 2;
std::string gordonfreeman = "Print";
double powerfulGordon = 4;
if(gordon * gordon == powerfulGordon){
//Do Nothing
}
while(gordon * gordon == gordon){
//Do Everything
}
}
Are you thinking to leave? no don't. Let's simplify the code into simple english language.
include standard library
function main starts
Number gordon is 2
String gordon freeman is print
Decimal Number powerfulgordon is 4
if gordon multiplied by gordon is powerfulgordon , then
"do nothing" <- the text on the right is a comment and will not be executed
then ends
till , gordon multiplied by gordon is gordon till then
Do Everything <- Another Comment
till then ends
ends
Wasn't that simple though. let's make it into points
First see the code below , this is the simplest code , which does nothing.
#include <iostream>
int main(){
int gordon = 2;
std::string gordonfreeman = "Print";
double powerfulGordon = 4;
if(gordon * gordon == powerfulGordon){
//Do Nothing
}
while(gordon * gordon == gordon){
//Do Everything
}
}
Are you thinking to leave? no don't. Let's simplify the code into simple english language.
include standard library
function main starts
Number gordon is 2
String gordon freeman is print
Decimal Number powerfulgordon is 4
if gordon multiplied by gordon is powerfulgordon , then
"do nothing" <- the text on the right is a comment and will not be executed
then ends
till , gordon multiplied by gordon is gordon till then
Do Everything <- Another Comment
till then ends
ends
Wasn't that simple though. let's make it into points
- #include <iostream> is including the main library which has got basic Input Output commands we need
- int main() is a function which is executed when the code is executed and the bracket { means start and the bracket } means ends
- int means integer (or number) , It does not support numbers with decimal like 2.23 , It will read 2.23 as 2 use double instead. the gordon written after int is the name you assign to the integer , now the integer that we declared has a name "gordon". then we set it's value by putting an "=" sign and then it's value
- string means something made with alphabets , like the whole post I'm writing now is a string , the word gordonfreeman after that is the name of the string , then "print" is the value of the string , note : be sure to enclose the string in " quotation mark.
- first we write if then a bracket ( to give the condition . then , write the condition on which you want the code to be executed , like in the example , I say gordon multiplied by gordon (remember the number gordon? which we said is 2 , and the decimal number powerfulgordon which was for? so , if 2*2 is powerfulgordon the code we write below will be executed , if not then not. so , 2*2 = 4 and powerfulgordon is 4 , that means we are eligible to go ahead , Note the use of == double equal to marks instead of single ones. Single one's are used for assigning a value and double are for checking. then we close the bracket ) and open a new one , braces { then we write a comment which will not be executed by the compiler. then close the braces } now if the following statement was true (which is true) , the code inside the two braces will be executed.
- Let's come to while loops. Now they are like if , they have a condition to run the code , but there is a little bit twist , that instead of executing just one time they will execute until the statement you provided becomes false , now in the example , I said gordon multiplied by gordon is gordon which is 2 * 2 = 2 , which is false , the code I provided in the braces will not be executed , if it was gordon multiplied by gordon = 4 , it would have done everything possible in the world. Just kidding , nothing would have happened because the code we provided inside the braces is a comment which is ignored in the final program.
That was alot wasn't it? You might not think of reading this right now , but give it a time , perhaps like me , you'll also know , how to code basic stuff , Next post , I'll show how to give output and take input , I'll also give a little bit of work to do with it :P , see you soon.
-Parikshit