Assignment in C
Assignment in C
QUESTIONS
1) What is C ++ ?
a) int main ()
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. It
does not matter whether there are other functions with other names defined before or after it - the
instructions contained within this function's definition will always be the first ones to be executed in any
C++ program. For that same reason, it is essential that all C++ programs have a main function.
The word main is followed in the code by a pair of parentheses (()). That is because it is a function
declaration: In C++, what differentiates a function declaration from other types of expressions are these
parentheses that follow its name. Optionally, these parentheses may enclose a list of parameters within
them.
Right after these parentheses we can find the body of the main function enclosed in braces ({}). What is
contained within these braces is what the function does when it is executed.
E) function
- allow to structure program in segment of code to perform individual tasks.
g) <<
- the << operator inserts the data that follows it into the stream that preceds it .
h) hello world\n
- 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 current line. Though the sequence is represented by two characters , it takes up only one characters
worth of memory space. Hence the sequence \n is called the newline character. The actual procedure to
start new line is system dependent but that is handlel by the c++ standard library transparent to you.
i) \n
- \n is the newline character, when the newline character is output to the console, the console breaks a
new line.
j) ;
- 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 mostcommon syntax errors is indeed to forget to
include some semicolon after a statement )
k) return 0
- 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
\n
\n is the newline character. When the newlinw character is output to the console, the console breaks a
new line.
j) ;
this character is used to mark the end of the statement and in fact it must beincuded 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 )
k) return 0;
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.
You may have noticed that not all the lines of this program perform actions when the code is executed.
There were
lines containing only comments (those beginning by //). There were lines with directives for the compiler's
preprocessor (those beginning by #). Then there were lines that began the declaration of a function (in
this case,
the main function) and, finally lines with statements (like the insertion into cout), which were all included
within
the block delimited by the braces ({}) of the main function.
The program has been structured in different lines in order to be more readable, but in C++, we do not
have strict
rules on how to separate instructions in different lines. For example, instead of
int main ()
{
cout << " Hello World!";
return 0;
}
We could have written:
int main () { cout << "Hello World!"; return 0; }
All in just one line and this would have had exactly the same meaning as the previous code.
SYMBOL
1)==
2)>
3)<
4)!=
5)>=
6)<=
RELATION OPERATIONAL
MEANING
EQUAL TO
GREATER THAN
LESS THAN
NOT EQUAL TO
GREATER THAN OR EQUAL TO
LESS THAN OR EQUAL TO
SYMBOL
1)+
2)3)*
4)/
5)%
MATHEMATIC AL OPERATES
MEANING
ADDITION OR UNRY PLUS
SUBTRACTION OR UNARY MINUS
MULTIPLICATION
DIVISION
REMAINDER AFTER DIVISION
5) STATE STATEMENTS BELOW AND GIVE AND EXAMPLE APPLICATION IN C++ PROGRAM
a) Go to :The goto statement is a control flow statement that causes the CPU to jump to
another spot in the code. This spot is identified through use of a statement
label.
// goto_statement.cpp
#include <stdio.h>
int main()
{
int i, j;
for ( i = 0; i < 10; i++ )
{
printf_s( "Outer loop executing. i = %d\n", i );
for ( j = 0; j < 2; j++ )
{
printf_s( " Inner loop executing. j = %d\n", j );
if ( i == 3 )
goto stop;
}
}
// This message does not print:
printf_s( "Loop exited. i = %d\n", i );
stop:
printf_s( "Jumped to stop. i = %d\n", i );
}
b) While
c)
d)
e)
f)
g)
h)
i)
j)
k)
l)
// do_while_statement.cpp
#include <stdio.h>
int main()
{
int i = 0;
do
{
printf_s("\n%d",i++);
} while (i < 3);
}
D) 1
E) 2
F) 3
G) 4
H) 5
I) 6
J) 7
K) 8
L) 9
M) 10
N) 11
O) 12
P) 13
cout << nPrinted << " numbers were found" << endl;
int nPrinted = 0;
2
3
4
7
8
nPrinted++;
10
11
12
13
cout << nPrinted << " numbers were found" << endl;
Q) While true
- Executes statements repeatedly until the specified termination condition (the expression )
evaluates
R) Do / while
- Excutes a statement repeatedly until the specified termination condition the
(expression)evaluates to zero.
S) Jump / loop
- Executes a sequence of statements multiple times and abbreviates the code that manages
the loop variable.
T) If/ else
- An if statements can be followed by an optional else statements , which executes when the
Boolean expression is false