07-06-2011, 10:22
Hey guys, this will be the first part in a series of C++ tutorials. Enjoy !
Statements in C++ are basicly the sentences used to tell the compiler what to run, all statements are terminated by the semi colon ( ; ).
There are many different types of statments in C++, here are just a few common statements:
int x: This is called an declaration statement, x is declared to be a variable, all variables must be declared before they are used.
x = 5: This is called the assignment statment, x is assignmed to be the number 5
cout << x :This is an output statement.
The compiler is also capable of resolving expressions. An expression is an mathematical entity that evaluates to a value. For example, in math, the expression 2+3 evaluates to the value 5. Expressions can involve values (such as 2), variables (such as x), operators (such as +) and functions (which return an output value based on some input value). They can be singular (such as 2, or x), or compound (such as 2+3, 2+x, x+y, or (2+x)*(y-3)).
For example, the statement x = 2 + 3; is a valid assignment statement. The expression 2+3 evaluates to the value of 5. This value of 5 is then assigned to x.
Here we have the code :
#include <iostream>: This line indicates the preprocessor directive of the proram.Preprocessor directives tell the compiler to perform a special task. In this case, we are telling the compiler that we would like to use the iostream library.In this case the preprocessor directive tells the program what cout and endl do.
int main(): This line shows the main() function which every C++ program has.
{}:These brackets indicate the start and end of the main() function.
using namespace std;: This is the first statement of the program, you can tell that it is a statement because it ends with a semi colon ( ; ) . wWthin iostream, they live inside a special compartment named std (short for standard). This using statement tells the compiler to look inside a compartment named std if it cant find cout or endl defined anywhere else. In other words, this statement is also necessary so the compiler can find cout and endl, which we use on line 6.
cout << "Hello world!" << endl;: Cout is an output statement, which represents the console/screen. Cout understands that anything sent through the << operator should be printed onto the screen. Endl is a statement which ends the line and moves the cursor to the next line.
return 0;: This is called the return statement, it return a value to the operating system after an executable program has run, the value determines whether it was successfully run or not. The return value of 0 is given which means that everything went ok.
Cin is basicly the opposite of cout,whereas cout prints data to the console, cin reads data from the console.
Try compiling this program and running it for yourself. When you run the program, it will print Enter a number: and then wait for you to enter one. Once you enter a number (and press enter), it will print You entered followed by the number you just entered.
Structure of a program
Statements and expressions:
Statements in C++ are basicly the sentences used to tell the compiler what to run, all statements are terminated by the semi colon ( ; ).
There are many different types of statments in C++, here are just a few common statements:
PHP Code:
int x;
x = 5;
cout << x;
int x: This is called an declaration statement, x is declared to be a variable, all variables must be declared before they are used.
x = 5: This is called the assignment statment, x is assignmed to be the number 5
cout << x :This is an output statement.
Expressions:
The compiler is also capable of resolving expressions. An expression is an mathematical entity that evaluates to a value. For example, in math, the expression 2+3 evaluates to the value 5. Expressions can involve values (such as 2), variables (such as x), operators (such as +) and functions (which return an output value based on some input value). They can be singular (such as 2, or x), or compound (such as 2+3, 2+x, x+y, or (2+x)*(y-3)).
For example, the statement x = 2 + 3; is a valid assignment statement. The expression 2+3 evaluates to the value of 5. This value of 5 is then assigned to x.
Hello World!:
Here we have the code :
PHP Code:
// #include "stdafx.h" // Uncomment if Visual Studio user
#include <iostream>
int main()
{
using namespace std;
cout << "Hello world!" << endl;
return 0;
}
#include <iostream>: This line indicates the preprocessor directive of the proram.Preprocessor directives tell the compiler to perform a special task. In this case, we are telling the compiler that we would like to use the iostream library.In this case the preprocessor directive tells the program what cout and endl do.
int main(): This line shows the main() function which every C++ program has.
{}:These brackets indicate the start and end of the main() function.
using namespace std;: This is the first statement of the program, you can tell that it is a statement because it ends with a semi colon ( ; ) . wWthin iostream, they live inside a special compartment named std (short for standard). This using statement tells the compiler to look inside a compartment named std if it cant find cout or endl defined anywhere else. In other words, this statement is also necessary so the compiler can find cout and endl, which we use on line 6.
cout << "Hello world!" << endl;: Cout is an output statement, which represents the console/screen. Cout understands that anything sent through the << operator should be printed onto the screen. Endl is a statement which ends the line and moves the cursor to the next line.
return 0;: This is called the return statement, it return a value to the operating system after an executable program has run, the value determines whether it was successfully run or not. The return value of 0 is given which means that everything went ok.
Cin:
Cin is basicly the opposite of cout,whereas cout prints data to the console, cin reads data from the console.
PHP Code:
//#include "stdafx.h" // Uncomment this line if using Visual Studio
#include <iostream>
int main()
{
using namespace std;
cout << "Enter a number: "; // ask user for a number
int x;
cin >> x; // read number from console and store it in x
cout << "You entered " << x << endl;
return 0;
}
Try compiling this program and running it for yourself. When you run the program, it will print Enter a number: and then wait for you to enter one. Once you enter a number (and press enter), it will print You entered followed by the number you just entered.