A complete program in C ++ must
have a function called main which contains statements between braces {and }. Each
statement can extend to more than one line but must end with a semicolon .
Comments must be enclosed by /* and*/ or preceded by // extending to the rest of the line. The first can be used for comments that stand
on many lines while the second for a whole line or an end part of a line. One
of them can be used to comment out the other. Comments and blank lines are
ignored by the compiler and contribute only to the readability of a program . Below is a simple program that calculates the
sum of all integers between two given integers. The user is prompted to enter these two
integers from the standard input stream (usually the terminal screen)
represented by cin . The result is output to the standard output stream (usually
the terminal screen) represented by cout. The streams cin and cout are defined
in a C ++ standard library called<iostream>. The program reads:
/*A sample program to illustrate some basic features of C++
It adds all integers between two given integers and
outputs the sum to the screen*/
cout <<"Enter two integers: \n"; // output to //screen
cin >> n>> m; //input will be assigned to n , m
if (n > m )
{// if n is bigger than m, swap them
int temp= n;// declare temp and initialize it
n = m; //assign value of m to n
m = temp;
} // assign value of temp to m
double sum = 0.00; //sum has double precision
// a loop , i changes from n to m with increment 1 each time
for(int i= n; i<= m; i++)
{
sum+=i;//sum += i: sum = sum + I;
}
cout <<"Sum of integers from "<< n<<" to"<< m
<<" is"<< sum<<"\n";// output sum to screen
return 0;
}
The first three
lines in the program are comments enclosed by /* and
*/ which are usually used for multiline comments. Other comments in the
program are short
ones and preceded by two slashes //; they can start from
the beginning or middle of a line extending to the
rest of it.
Input and output
are not part of C++ and their declarations are provided
in a header file
called iostream that must be included at
the beginning of
the program using
#include
<iostream>
with the sign #
standing in the first column. The statement using namespace std
lets the program
gain access to declarations in the namespace std. All standard libraries are defined in a namespace called std. The mechanism namespace enables one to group
related declarations and definitions together and supports modular programming.
The compiler will
not recognize the identifiers cin and cout if the standard library <iostream> is not included. Similarly, the math
library <math.h> must be included when mathematical functions like
sin (sine), cos (cosine), atan(arctangent),exp (exponential), sqrt(square root), and log (logarithm) are used. The header files for standard
libraries reside somewhere in the C++ system, and a programmer normally does
not
have to care
where. When they are included in a program, the system will find them automatically.
The symbol << is called the output operator and >>the input operator.
The statement
cout
<<"Enter two integers: \n";
outputs the
string “Enter two integers” followed by a new
line to the screen, telling the user to type in, on the keyboard, two integers
separated by a whitespace. Then the statement
cin >> n
>> m;
causes the
computer to pause while characters are entered from the key
board and stored
in memory at locations identified by n and m. It is equivalent to the following
two statements
cin >> n;
// first input from screen is stored in n
cin >> m;//
second input from screen is stored //in m
Notice that a variable
must be declared before it can be used. The integer
variables n and m are declared by
the statement,
int n, m; //
declare n and m to be integers
The character ‘\n’ represents a newline. A character string like “Sum of
integers from “must appear between double quotation marks, while a single
character appear
between single quotation marks such as ‘A’,’5’ and ‘\n’.
Note that ‘\n’ is
a single character.
The words int, double, if, and for are reserved
words in C++ and can not be used for the name of a variable. The reserved word int stands for
integers while double stands for double precision floating point numbers.
The variable sum is declared to have double precision by
double sum =0.00//
sum is initialized to 0
This statement
not only declares sum to have type double, but also assigns
it an initial value 0.00. A variable may not have to be initialized when it is declared.
In the if conditional statement,
if (n >m )// if n is bigger than m, swap them
{
int temp = n;//
declare temp and initialize it
n = m;// assign
value of m to n
m = temp;//
assign value of temp to m
}
the statements
inside the braces will be executed if the condition n > m
(n is strictly larger than m) is true and will be skipped otherwise. This if statement instructs the computer to swap interchange the values of n and m if n is larger than m. Thus, after this if statement, n stores the smaller of the two input integers and m stores the larger one. Notice that a temporary variable temp is used to swap n and m. The braces in an if statement can be omitted when there is only one statement inside them.
For example, the following two statements are equivalent,
if (n > m) m = n+100;// one statement
if (n > m){ // an equivalent statement
m = n+100;
}
0 Comments:
Post a Comment
If you have any doubts . Please let me know.