![]() |
|
|||||||||||||||||||||
|
Why Use Programming Language VS Spoken Language We use programming languages for one reason: Programming languages are not ambiguous. English, which is the most widely used language (at least for business purposes) is ambiguous -one word many have multiple meanings. If the compiler used English as its language, there would be problems in the compiling process due to the ambiguity. For example the word "Light" has multiple meanings:
Let us assume that we did not have programming languages such as C++, instead there was conventional English. If you typed the term "light" and clicked on a button to compile your programme, the compiler would stumble upon the term "light" and would not know what you are referring to. The compiler would not know if you are referring to light as in not heavy or light as in the opposite of dark, etc and would consequently produce garbage for the output or perhaps even crash. |
||||||||||||||||||||||
|
FUNDAMENTALS:
|
||||||||||||||||||||||
|
You may think "English is ambiguous, so why not use Chinese or French". The same answer is applicable here: they contain ambiguous terms as well. The only way to remove the ambiguity is to create a specific programming language in which the compiler knows that there is specifically one and only one meaning to the words used. C++, as you know is a programming language. Your computer also has a language called machine language. The compiler is a tool for extracting the C++ language and converting it to machine language for your programme to run. You may ask yourself: "Why can't my computer language be C++?". There is such a multitude of programing languages that making the computer run C++ would be inconvienient. The same applies to any other language such as PERL. PERL comes with a compiler which converts the PERL language to machine language in order for your PERL programme to run. Programming Procedures This section applies to all programming languages, not just C++. When someone such as your professor gives you an assignment to complete, you cannot immediately go to your computer and start typing code. Actually you can do that, but it is not considered wise. The first step is to analyse the problem given to you by your instructor. Let's assume your professor told you to add 10 numbers and then multiply the 10 numbers. Your first step which consists of analysing the problem would consist of you asking yourself what exactly your professor wants. 1. Analyze the Problem Questions include:
Why did I highlight question number one as being the most important step? The answer is because if you don't know or understand what the problem is then you cannot even hope to solve it!! If you do not understand the problem, DON'T EVEN THINK OF GOING FURTHER!! You will only be wasting your time!! 2. Design a Solution This is the section where, after you have fully understood the problem/task, you design an algorithm to accomplish this task. You have the freedom to solve any algorithm the way you or your professor/ boss etc feels necessary. In this step you would ask yourself if a specific algorithm would be better solved using pointers or using a stack vs a queue, or how many functions your programme should consist of, etc. Let us assume your instructor wanted you to accept the 10 numbers from the user and print the sum and product to the screen. For this example, you would state "In order to add 10 numbers, I would have to divide this task into subtasks and tackle each one like so:
3. Code your Algorithm As stated, this is where you sit down and code your algorithm. This code will be explained in this section and next section, so don't worry. FIGURE 2.1 4. Test your Code As stated, this is where you test your code to ensure it works properly and accomplish the specific task desired. If your code does not work, perhaps your algorighm was incorrect or you made a mistake in your code. Programming Format When programming in C++, as with any language, a specific format must be followed. The same is with writing a letter or resume-- a specific format must be used. You do not have to follow the specific format, however following a standard format makes the code easier to read for others as well as yourself. Try to keep the curved brackets { } aligned vertically. Keep spacing consistant. Always document your code neatly so that if you leave your programme for x amount of time, you can understand what you were doing once you return back to your code by the comments made. Keep the comments aligned and neat as well. Always end each statement with a semicolon ";" The general format for C++ is to:
Notice below that the spacing is consistant and the brackets are aligned creating code that does not look confusing. Do not worry about what the code means, it will be explained as we go along. FIGURE 2.2 To illustrate the point of following a format, take a look at the code below: FIGURE 2.3 Now look at the code below in Figure 2.4. Notice that the code below is the same as the code above, however it looks much neater and is easier to understand and follow. FIGURE 2.4 Programming Statements There are 6 different types of statements in C++: TABLE 2.1
Directives The #include statements followed by iostream.h, cstring.h, etc are called header files, the whole statement is called a directive. Directives tell the compiler which files to use in order to run your program. Reserved words and built in functions are defined in the header files. When you type cout, cin, etc in your programme, the compiler looks to see if they are defined. The #include statements tell the compiler "I would like to use cout, cin, int, char, etc in my program, therefore, I have included the files which define those reserved words." It is the same thing as baking a cake. In order to bake a cake, you need eggs, flour, milk, sugar, etc. If you do not include one of the basic ingredients such as flour, the cake will be a disaster. You do not know very much about the process of making bleached flour, or the biochemical reactions which take place for the milk to be produced in the cow, however you know that it must be included in your cake. The same applies to the header files. You do not know what is in the header file or how it defines the reserved words (you will learn as you go on), but you know that without the files your programme will not work correctly. Another example would be your car. You know that in order for your car to run, the oil must be changed, there must be gas in the car, the engine belt must be changed, the valves must be running properly, etc etc. You do not know how to change the oil or the belt. All you know is that in order for the car to run, the mechanic needs to change the oil. Int Main() and Return() You may have noticed the int main() inclosed in curved brackets { }part of the programme. It means exactly what it states "This is the main part of the programme and it starts right here". The main part of the programme ends when the compiler reads the return(0) statement. It basically states "Compiler, you have now reached the end of the main part of the programme. Stop here." Output Statements An output statement is denoted by: cout << . It is read as "see out". It tells the compiler to print the output to the screen. For example, if you wanted to type out the statement "I am learning C++", it would be as follows: FIGURE 2.5 Input Statements An input statement is used to denote what the programme will accept from the user, you are typing input to the programme. It is denoted by "cin >>" read as "see in" Let's say you type a programme asking how old some one is and the compiler prints it out: FIGURE 2.6
|
||||||||||||||||||||||
|
|
||||||||||||||||||||||
|
Site Credits| Questions and Enquiries
| © 2001 | Contact
|