Formated code in c++ landguage
Program comments are helpful to human readers but ignored by the compiler. Another aspect of source code that is largely irrelevant to the compiler but that people find valuable is its formatting. Imagine the difficulty of reading a book in which its text has no indentation or spacing to separate one paragraph from another. In comparison to the source code for a computer program, a book’s organization is quite simple.
//Unformated Code
#include <iostream>
int main ( )
{
int
x
;
x
= 10 ;
std ::
cout <<
x <<
'\n'
;
}
#include <iostream>
int main()
{
int x;
x=10;
std::cout<<x<<'\n';
}
Both reformatted programs are valid C++ and compile to the same machine language code as the original version. Most would argue that the original version is easier to read and understand more quickly than either of the reformatted versions.
codeing style in c++ language with example AndroWep-Tutorials
Errors and Warnings
Beginning programmers make mistakes writing programs because of inexperience in programming in general or because of unfamiliarity with a programming language.
- compile-time error
- run-time error
- logic error
Compile-time Errors
A compile-time error results from the programmer’s misuse of the language. A syntax error is a common compile-time error. For example, in English one can say
the number of the subject(singular form)disagrees with the number of the verb (plural form). It contains a syntax error. It violates a grammatical rule of the English language. Similarly, the C++ statement
x = y + 2;
Run-time Errors
The compiler ensures that the structural rules of the C++ language are not violated. It can detect, for example, the malformed assign. Some violations of the language can not be detected at compile time,however.
// File dividedanger.cpp
#include <iostream>
int main()
{
int dividend, divisor;
// Get two integers from the user
std::cout << "Please enter two integers to divide:";
std::cin >> dividend >> divisor;
// Divide them and report the result std::cout << dividend << "/" <<
divisor << " = " << dividend/divisor << '\n';
}
Please enter two integers to divide: 32 4 32/4 = 8
Logic Errors
dividend/divisor; if divisor/dividend;
The program compiles with no errors. It runs, and unless a value of zero is entered for the dividend, no run-time errors arise. However, the answer it computes is not correct in general. The only time the correct answer is printed is when dividend=divisor.