C++ Basic Syntax and Fundamental Logics

Justin_Romile

Beta member
Messages
3
Location
Philippines
To start with, you need to provide yourself a compiler. A compiler is where you type, modify, and compile your source code. Please follow this link to download Dev C++ Compiler.

Code:
 [URL="http://sourceforge.net/projects/dev-cpp/files/Binaries/Dev-C%2B%2B%204.9.9.2/devcpp-4.9.9.2_setup.exe/download?use_mirror=nchc&utm_expid=6384-3&utm_referrer=http%3A%2F%2Fwww.bloodshed.net%2Fdev%2Fdevcpp.html"]Download Dev-C++ from SourceForge.net[/URL]
First, create a new blank Project.

Familiarize yourself with the basic formats. Type this in your compiler.
Code:
#include<iostream>
#include<conio.h>

using namespace std;

int main()
{
//LOGICAL CODES HERE(ALGORITHM).

getch();
return 0;
}
NOTE: "//" sign means comment. Anything you type within it will not be read and execute by the compiler. It is usually used to explain your codes or to denote something.

Everything you see there in the code has its own purpose and explanations but for the mean time, all you have to do is to leave it by and just type it with our algorithm. Let's pretend that all of those is constant.

Programming Example 1:
Output "HELLO WORLD!".
Code:
#include<iostream>
#include<conio.h>

using namespace std;

int main()
{
cout<<"Hello World!"<<endl;
getch();
return 0;
}
cout is the command to ouput something such as statements. It is usually used to notify and instruct users. Make sure that you type it exactly like that including the capitalization and correct punctuations. Codes are always case sensitive.

endl means "end of the line". This is used to moved the cursor into the next line after the statement. This is not necessarily to be there. I suggest you to try experimenting between the two so you can easily understand and see the difference.
Code:
cout<<"Hello World!"<<endl;
Code:
cout<<"Hello World!";
Programming Example 2: Ask the user to input two numbers and output its sum. (Simple calculator for addition)

Code:
#include<iostream>
#include<conio.h>

using namespace std;

int main()
{
int a,b;//Declaration of variables. Always declare all the variables that you are going to use.

cout<<"Please insert two numbers respectively: "<<endl;
cin >> a >> b; // This is how you get input from the user. 

cout<<"The sum of two numbers is: "<<a+b<<endl; //OUTPUT THE SUM.

getch();
return 0;
}
There you go, you are now familiar with the MOST basic syntax of C++. All you have to do is to practice until you become very familiar and almost perfect with it. I STRONGLY SUGGEST THAT YOU TYPE THE CODES INSTEAD OF COPY PASTE. It will help you alot in fluency and accuracy in typing codes.
 
ive never studied c++. ive always wondered what the << and >> mean.

They're stream operators.

<< is an operator to go to the ostream and output buffer, and >> is an operator to go to the istream and input buffer.

I've had to make a program that overloaded those operators to output / input matrices and do different operations on them.
 
Back
Top Bottom