Programming Fundamental Mid Term
Programming Fundamental Mid Term
C++ Compiler
A C++ compiler is itself a computer program which’s only job is to
convert the C++ program from our form to a form the computer
can read and execute. The original C++ program is called the
“source code”, and the resulting compiled code produced by the
compiler is usually called an “object file”. Before compilation the
preprocessor performs preliminary operations on C++ source
files. Preprocessed form of the source code is sent to compiler.
After compilation stage object files are combined with predefined
libraries by a linker, sometimes called a binder, to produce the
final complete file that can be executed by the computer. A library
is a collection of pre-compiled “object code” that provides
operations that are done repeatedly by many computer programs.
Comments
First three lines of the above program are comments and are
ignored by the compiler. Comments are included in a program to
make it more readable. If a comment is short and can be
accommodated in a single line, then it is started with double slash
sequence in the first line of the program. However, if there are
multiple lines in a comment, it is enclosed between the two
symbols /* and */.
#include<iostream>
int main ( )
The word main is a function name. The brackets ( ) with main tells
that main ( ) is a function. The word int before main ( ) indicates
that integer value is being returned by the function main (). When
program is loaded in the memory, the control is handed over to
function main ( ) and it is the first function to be executed.
cout<<"Hello World!";
This statement prints our "Hello World!" message on the screen.
cout understands that anything sent to it via the << operator
should be printed on the screen.
return 0;
Programming fundamental Prepared by: Waseem
This is a new type of statement, called a return statement. When
a program finishes running, it sends a value to the operating
system. This particular return statement returns the value of 0 to
the operating system, which means “everything went okay!”.
Variable Initialization
Variable is a location in the computer memory which can store
data and is given a symbolic name for easy reference. The
variables can be used to hold different values at different times
during the execution of a program.
Declaration of a variable
Before a variable is used in a program, we must declare it. This
activity enables the compiler to make available the appropriate
type of location in the memory.
float total;
You can declare more than one variable of same type in a single
statement
int x, y;
Initialization of variable
When we declare a variable it's default value is undetermined. We
can declare a variable with some initial value.
int a = 20;
STEP 1:
Now first we'll allocate memory for storing numbers. Location of
the computer memory which is used to store data and is given a
symbolic name for reference is known as variable. We need three
variables, two for storing input and third for storing result. Before a
variable is used in a program, we must declare it. This activity
enables the compiler to make available the appropriate type of
location in the memory.
STEP 2:
Following statement stores value in first variable
x = 25;
STEP 3:
Following statement stores value in second variable
y = 10;
STEP 4:
Now, add these two numbers together and store the result of the
addition in third variable
z = x + y;
STEP 5:
Print the result cout << "The sum is ";
cout << sum;
You can combine above two statements in one statement
cout << "The sum is " << sum;
Output:
The sum is 35
1)Identifiers
Symbolic names can be used in C++ for various data items used
by a programmer in his program. A symbolic name is generally
known as an identifier. The identifier is a sequence of characters
taken from C++ character set. In previous program x, y and z are
identifiers of variables. The rule for the formation of an identifier
are:
An identifier can consist of alphabets, digits and/or underscores.
It must not start with a digit
C++ is case sensitive that is upper case and lower-case letters
are considered different from each other.
It should not be a reserved word.
2)Keywords
3)Literals
Literals (often referred to as constants) are data items that never
change their value during the execution of the program. The
following types of literals are available in C++.
Integer-Constants
Character-constants
Floating-constants
Strings-constants
A) Integer Constants: Integer constants are whole number
without any fractional part. C++ allows three types of integer
constants.
B) Decimal integer constants: It consists of sequence of digits
and should not begin with 0 (zero). For example, 124, - 179,
+108.
C) Octal integer constants: It consists of sequence of digits
starting with 0 (zero). For example. 014, 012. Hexadecimal
integer constant: It consists of sequence of digits preceded by ox
or OX
Character constants
A character constant in C++ must contain one or more characters
and must be enclosed in single quotation marks. For example, 'A',
'9', etc. C++ allows nongraphic characters which cannot be typed
directly from keyboard, e.g., backspace, tab, carriage return etc.
4) Punctuators
The following characters are used as punctuators in C++.
Brackets [ ]
Opening and closing brackets indicate single and
multidimensional array subscript.
Parentheses ( )
Opening and closing brackets indicate functions calls,; function
parameters for grouping expressions etc.
Braces { }
Opening and closing braces indicate the start and end of a
compound statement.
Comma ,
5) Operators
Operators are special symbols used for specific purposes. C++
provides six types of operators. Arithmetical operators, Relational
operators, Logical operators, Unary operators, Assignment
operators, Conditional operators, Comma operator.We will talk
more about operators in the coming sections
Types of Operators:
1)Arithmetical operators:
Arithmetical operators +, -, *, /, and % are used to performs an
arithmetic (numeric) operation. You can use the operators +, -, *,
and / with both integral and floating-point data types. Modulus or
remainder % operator is used only with the integral data type.
Operators that have two operands are called binary operators.
2)Relational operators:
Constants
A variable which does not change its value during execution of a
program is known as a constant variable. Any attempt to change
the value of a constant will result in an error message. A constant
in C++ can be of any of the basic data types, const qualifier can
be used to declare constant as shown below:
const float PI = 3.1415;
The above declaration means that PI is a constant of float types
having a value
3.1415.
Examples of valid constant declarations are:
const int RATE = 50;
const float PI = 3.1415;
const char CH = 'A';
Type Conversion
Implicit conversion
Data type can be mixed in the expression. For example
double a;
int b = 5;
float c = 8.5;
a = b * c;
When two operands of different type are encountered in the same
expression, the lower type variable is converted to the higher type
variable. The following shows the order of data types.
Order of data types
long double this the order of data types highest to lowest
double
float
long
int char
The int value of b is converted to type float and stored in a
temporary variable before being multiplied by the float variable c.
The result is then converted to double so that it can be assigned
to the double variable a.
Explicit conversion
Input/Output (I/O)
The standard C++ library includes the header file iostream, which
can be used to feed new data into the computer or obtain output
on an output device such as: VDU, printer etc.
The following C++ stream objects can be used for the input/output
purpose.
cout console output
cin console input
cout object
cout is used to print message on screen in conjunction with the
insertion operator <<
cout << "Hello World"; // prints Hello world on screen
cout << 250; // prints number 250 on screen
cout << sum; // prints the content of variable sum on screen
To print constant strings of characters we must enclose them
between double quotes (").
If we want to print out a combination of variables and constants,
the insertion operator (<<) may be used more than once in a
single statement
cin object
cin can be used to input a value entered by the user from the
keyboard. However, the extraction operator >> is also required to
get the typed value from cin and store it in the memory location.
Let us consider the following program segment:
int marks;
cin >> marks;
In the above segment, the user has defined a variable marks of
integer type in the first statement and in the second statement he
is trying to read a value from the keyboard.
// input output example
#include<iostream>
using namespace std;
int main ()
{
int length;
int breadth;
int area;
cout << "Please enter length of rectangle: ";
cin >> length;
cout << "Please enter breadth of rectangle: ";
cin >> breadth;
Flow of Control
Statements
Statements are the instructions given to the computer to perform
any kind of action. Action may be in the form of data movement,
decision making etc. Statements form the smallest executable
unit within a C++ program. Statements are always terminated by
semicolon. There are different type of statements,
A) Compound Statement
A compound statement is a grouping of statements in which each
individual statement ends with a semi-colon. The group of
statements is called block. Compound statements are enclosed
between the pair of braces ({}.). The opening brace ({) signifies
the beginning and closing brace (}) signifies the end of the block
C) Conditional Statements
Sometimes the program needs to be executed depending upon a
particular condition. C++ provides the following statements for
implementing the selection control structure.
if statement
if else statement
nested if statement
switch statement
Looping statement
if statement
syntax of the if statement
if (condition)
{statement(s);
}
if condition is true, statement is executed; otherwise it is skipped.
if else statement
syntax of the if - else statement
if (condition)
statement1;
while loop
do-while loop
for loop
While loop
Syntax of while loop
while(condition)
{
statement(s);
}
#include <iostream>
int main()
{
double weightInKG = 67;
double heightInMeters {2};
double bmi = 0;
bmi = weightInKG/(heightInMeters*heightInMeters);
#include <iostream>
int main()
{
int marks = 0;
cout << "Please enter your marks!" << endl;
cin >> marks;
// else{