Take a look at simple input in C++.
We will write a program that prompts the user for a value in miles. The program then converts this value to kilometers and prints it out.
Our conversion program uses variables capable of storing integer values and real values, as well as
const variables. Notice
that in C++, all variables must be declared before they can be used, but unlike in C, they need not be at the head of a block.
Move your mouse cursor over the lines of code surrounded by red rectangles to receive tooltip information with respect to the code.
Programming languages are independent of specific computer architecture, but they are human creations and follow certain conventions.
To ease the translation process, those conventions are much stricter than they are for human languages.
When you talk to another person, and you scramble or omit a word or two, your conversation partner will usually still understand what you have to say.
Compilers are less forgiving.
For example, if you omit the quotation mark close to the end of the instruction,
if (int_rate > 100) message_box("Interest rate error);
the C++ compiler will get quite confused and complain that it cannot translate an instruction containing this error which is a good thing. If the compiler were
to try to guess what you did wrong and try to fix it, it might not guess your intentions correctly.
In that case, the resulting program would do the wrong thing with side effects.
When a compiler reads programming instructions in a programming language, it will translate them into machine code only if the input follows the language conventions exactly.
Just as there are many human languages, there are many programming languages.
Consider the instruction
if (int_rate > 100) cout << "Interest rate error";
This is how you must format the instruction in C++. But in Visual Basic (a popular programming language for business applications) the same instruction would be written as
if int_rate > 100
then System.Console.Write("Interest rate error")
Compilers are language-specific and the C++ compiler will translate only C++ code, whereas a Visual Basic compiler will reject anything but legal Visual Basic code.
For example, if a C++ compiler reads the instruction
if int_rate > 100 then ...,
it will complain, because the condition of the if statement is not surrounded by parentheses ( ), and the compiler does not expect the word then.
The choice of the layout for a language construct such as the if statement is somewhat arbitrary. The designers of different languages make different tradeoffs among readability, easy translation, and consistency with other constructs.