Beruflich Dokumente
Kultur Dokumente
Bjarne Stroustrup
3. Describe the main purpose of the following syntax and grammar
#
o Lines beginning with a hash sign (#) are directives for the preprocessor. They are not
regular code lines with expressions but indications for the compiler's preprocessor.
#include
o Tells the pre-processor to include a standard file
iostream.h
o This specific file (iostream) includes the declarations of the basic standard input-output
library in C++, and it is included because its functionality is going to be used later in the
program
int main()
o This line corresponds to the beginning of the definition of the main function. The main
function is the point by where all C++ programs start their execution, independently of
its location within the source code.
Function
o Allow to structure program in segment of code to perform individual tasks.
cout
o cout represents the standard output stream in C++, and the meaning of the entire
statement is to insert a sequence of characters.
<<
o The << operator inserts the data that follows it into the stream that precedes it.
Hello world\n
o This line is a C++ statement. C++ strings are enclosed within double quotes ("). The
quotes themselves are not part of the string and hence not printed. The sequence \n is
used within a string to indicate the end of the current line. Though the sequence is
represented by two characters, it takes up only one character's worth of memory space.
Hence the sequence \n is called the newline character. The actual procedure to start a
new line is system-dependent but that is handled by the C++ standard library
transparent to you.
\n
o '\n' is the newline character. When the newline character is output to the console, the
console breaks a new line.
;
o
This character is used to mark the end of the statement and in fact it must be included at
the end of all expression statements in all C++ programs (one of the most common
syntax errors is indeed to forget to include some semicolon after a statement)
Return 0
o The return statement causes the main function to finish. Return may be followed by a
return code (in our example is followed by the return code 0). A return code of 0 for the
main function is generally interpreted as the program worked as expected without any
errors during its execution. This is the most usual way to end a C++ console program.
Meaning
1.
Add
2.
Subtract
3.
Multiply
4.
Divide
5.
Modulo
Relational Operators
Symbol
Meaning
1.
==
Equal to
2.
!=
Not equal to
3.
<
Less than
4.
>
More than
5.
<=
6.
>=
While
o A while statement conditionally executes a statement zero or more times? As long as a
Boolean test is true.
o To find the first occurrence of a value in an array.
While True
o Executes statement repeatedly until expression evaluates to zero.
Do/While
o Executes a statement repeatedly
(the expression) evaluates to zero.
until
the
specified
termination
condition
Jump/Loop
o Execute a sequence of statements multiple times and abbreviates the code that
manages the loop variable.
If/Else
o An if statement can be followed by an optional else statement, which executes when the
boolean expression is false.