Chapter 4&5
Chapter 4&5
Introduction to C++
Basics of C++ Programming
Memory
A computer provides a Random Access Memory
(RAM) for storing executable program code as
well as the data the program manipulates.
This memory can be thought of as a contiguous
sequence of bits, each of which is capable of
storing a binary digit (0 or 1).
Typically, the memory is also divided into groups
of 8 consecutive bits (called bytes).
The bytes are sequentially addressed. Therefore
each byte can be uniquely identified by its
address
Cont.
Return 0;
Return an integer value.
}
This brace marks the end of the body of main.
Let us add an additional instruction to our
first program:
Hello World! I'm a C++ program
// my second program in C++
#include<iostream>
int main ()
{
cout << "Hello World! ";
cout << "I'm a C++ program";
return 0;
}
Assignment (=)
Arithmetic operators (+, -, *, /, %)
Compound assignment (+=, -=, *=, /=)
Increment and decrement (++, --)
Relational and equality operators ( ==, !=, >,
<, >=, <= )
Logical operators ( !, &&, || )
Conditional operator ( ? )
Assignment (=)
expression is equivalent to
value += increase; value = value + increase;
a -= 5; a = a - 5;
a /= b; a = a / b;
price *= units + 1; price = price * (units + 1);
c++;
c+=1;
c=c+1;
are all equivalent in its functionality: the
three of them increase by one the value of c.
Example 1 Example 2
B=3; B=3;
A=++B; A=B++;
// A contains 4, // A contains 3,
B contains 4 B contains 4
Chapter Five
Control Structures
Conditional structure:
The if Statement
The if keyword is used to execute a statement or
block only if a condition is fulfilled. Its form is:
if (condition) statement
Where condition is the expression that is being
evaluated. If this condition is true, statement
is executed. If it is false, statement is ignored
(not executed) and the program continues
right after this conditional structure.
Forexample, the following code fragment
prints x is 100 only if the value stored in the x
variable is indeed 100:
if (x == 100)
cout << "x is 100";
Cont.
We can additionally specify what we want to
happen if the condition is not fulfilled by using
the keyword else. Its form used in conjunction
with if is:
if (x == 100)
cout << "x is 100";
else
cout << "x is not 100";
Cont.
A program that prints out a result as +ve, -ve or 0.
int x=7;
if (x>0){
cout<<x<<“is +ve”;
}
else if (x<0){
cout<<x<<“ is -ve”;
}
else
cout<<x<<“ is Zero”;
return 0;
}
Iteration structures (loops)
Loops have as purpose to repeat a statement a
certain number of times or while a condition is
fulfilled.
Types of loops
for loop
while loop
do-while loop
The for loop
4. finally, whatever is specified in the increase field is executed and the loop
gets back to step 2.
Here is an example of countdown using a for loop:
10, 9, 8, 7, 6, 5, 4, 3, 2, 1, FIRE!
// countdown using a for loop
#include<iostream.h>
int main ()
{
for (int n=10; n>0; n--) {
cout << n << ", ";
}
cout << "FIRE!\n";
return 0;
}
Jump statements.
0,1,2,3,4,_6,7,8,9,10
0,1,2,3,4,
// break statement example
for(int i=0;i<=10; i++){
if (i==5){
continue;
}
cout<<i<<“,”;
}
The while loop
Output
//while loop 1,2,3,4,5,6,7,8,9,10,
#include<iostream>
int i=1;
do {
cout<<i<<“,”;
++i;
}
while (i <=10);
return 0;
}
NEXT
Nested loop