Tutorial Basic of C++ Lesson 1 en
Tutorial Basic of C++ Lesson 1 en
Tutorial Basic of C++ Lesson 1 en
The first thing you need to do, before starting on the C + +, is to ensure that you have a compiler. What compiler, you ask? A compiler converts a program you write to an executable that your computer iscan correctly understand and execute. If you take the course, you may have one administered through your school. If you start out on your own, your best bet is to use Code :: Blocks with MinGW. If you are on Linux,You can use g + +, and if you are on Mac OS X, you can use Xcode
Let's take a look at the elements of the program. The # include is a "preprocessor" directive that tells the compiler to put the code in the header called iostream into our program before actually making the executable. by including header file, you get access to different functions. For example, the function requires iostream court. Follow include the statement, "using namespace std; '. This line tells the compiler to use the group functions is part of the standard library (std). By including this line at the top of the file, you allow the program to using functions like a court. The semicolon is part of the syntax of C + +. This tells the compiler that you at the end of the command. You will see later that the semicolon is used to end most commands in C + +.
The next important line is int main (). This line tells the compiler that a function called main, and that function returns an integer, hence int. The "curly braces" ({and}) signal the beginning and end of the function and other code blocks. You can think of them as the meaning of BEGIN and END. The next line of the program may seem strange. If you have programmed in other languages, you may wish to print will be a function used to display text. In C + +, however, the courts are used for object display text (pronounced "C out"). It uses the << symbols, known as "insertion operator", to show what is the output. cout << result in the function call with the following text as an argument to the function. citing informed compiler that you want to output a string literal is. The sequence '\ n' is actually treated as a single character stands for a new line (we'll discuss this in more detail later). This moves the cursor on your screen to the line next. Again, notice the semicolon: is added to the last line, such as calling a function, in C + +. The next command is cin.get (). This is another function call: it reads the input and expect the user to press the button again. Many compiler environments will open a new console window, run the program, and then close the window. This command makes the window from closing because the program is not finished because you are waiting for press enter. Including the line that gives you time to see the running program. Having achieved the primary end, clamp shut, our program will return the value 0 (and integer, then why we say the main to return int) for the operating system. The return value is important because it can used to tell the OS whether our program succeeded or not. A return value of 0 means success and returned automatically (but only for the main, other functions require you to manually return value), but if we want to return something else, such as 1, we have to do with return statement:
#include <iostream> using namespace std; int main() { cout<<" Qrembiezs Intruder was here!\n"; cin.get(); return 1; }
Final closing brace of the function. You should try to compile and run this program. You can cut and paste the code into the file, save it as a cpp file.
Declaring Variables in C++ To declare a variable that you use the syntax "<name> type:". Here are some examples of variable declarations:
int x; char letter; float the_float;
It is permissible to declare multiple variables of the same type on the same line, each must be separated by commas.
int a, b, c, d;
You may have seen that the variable declarations are followed by a semicolon (note that this is a procedure that the same used when you call a function). Common Errors when Declaring Variables in C++ If you try to use a variable that you have stated, your program will not compile or run, and you will receive an error message informing you that you have made a mistake. Typically, this is called Undeclare Variable
Case Sensitivity
Now is a good time to talk about an important concept that can easily throw you off: sensitivity of the case. Basically, in C + +, if you use uppercase or lowercase letters p. Cat and kitten word meaning different for the compiler. In C + +, all language keywords, all functions and all variables are case sensitive. Differences in variable declarations between you and the use of variables is one reason you might get error variable
Using Variables
Ok, so you now know how to tell the computer about the variables, but how to use them? Here is a sample program that demonstrates the use of variable
#include <iostream> using namespace std; int main() { int thisisanumber; cout<<"Please enter a number: "; cin>> thisisanumber; cin.ignore(); cout<<"You entered: "<< thisisanumber <<"\n"; cin.get(); }
Let's take a break and check this program line by line. Int declare thisisanumber keyword to an integer. Function cin >> to read the value into thisisanumber, the user must press enter before the number is read by the program. cin.ignore () is a function Another read and discard characters. Remember that when you type input into the program, it takes the enter as well. We do not need this, so we throw it away. Note that the variable is declared integer, if the user tries to type a decimal number, it will be truncated (ie, components of decimal numbers will be ignored). Try typing in sequence of characters or a decimal number when you run the example program; response will vary from input to input, but in case it is not too pretty. Note that when printing out a variable quotation marks are not used. Is there any sign of quotation, the output will be "You Entered:. Thisisanumber" The lack of quotation marks tells the compiler that there are variables, and because the program should check the value of a variable that can change the variable names with variables when running the output function. Do not be confused with the inclusion of two separate insertion operators on one line. Including the operator insertion in one lines perfectly acceptable and all output will go to the same place. In fact, you must separate string literals (strings enclosed in quotation marks) and variables by giving each its own insertion operators (<<). trying to put two variables together with only one << will give an error message, do not try it. Do not forget to end functions and declarations with a semicolon. If you forget the semicolon, the compiler will give an error when You try to compile the program.
Other forms of the same, ==, not a way to assign values to variables. Instead, it checks to see whether the variables are the same. This is useful in other areas of the C + +, for example, you will often use == in constructs such as conditional statements and loops. You can probably guess how <dan> function. They are larger than and less than operators.
a < 5 // Checks to see if a is less than five a > 5 // Checks to see if a is greater than five a == 5 // Checks to see if a equals five, for good measure
Comparing the variables are not really useful until you have several ways to use it
Special Thanxs to My Team : Prabujoyoboyo Loiz Xempaxping Thanxs to Whitehat ./Blacknote Cancer Linuxer46 Shamus Cybermuttaqin See u in Lesson 2 Basic Of c++ (I Hope I can write again)