Chapter 1 - Introduction To Basic Computer Programming
Chapter 1 - Introduction To Basic Computer Programming
Software
Instructions to command computer to perform
actions and make decisions
Hardware
Structured programming
Object-oriented programming
C++
For details more check out A History of C++: 1979−1991 Compiled by Dagne Walle 7
Characteristics of C++
C++ is…
Compiled. A separate program, the compiler, is used to
turn C++ source code into a form directly executed by the
CPU.
Strongly typed and unsafe: Conversions between variable
types must be made by the programmer (strong typing) but
can be avoided when needed (unsafe)
C compatible: call C libraries directly and C code is nearly
100% valid C++ code.
Capable of very high performance: The programmer has
a very large amount of control over the program execution
Object oriented: With support for many programming
styles (procedural, functional, etc.)
No automatic memory management: The programmer is in 8
Compiled by Dagne Walle
control of memory usage
C++ is Object-oriented programming
“Class Car”
OOP defines classes to represent
these things.
Classes can contain data and methods public interface
(internal functions).
Classes control access to internal data
and methods. A public interface is
used by external code when using the
class.
This is a highly effective way of
modeling real world problems inside
of a computer program.
Variable = Expression
First, expression on right is evaluated.
Then the resulting value is stored in the
memory location of Variable on left.
Example: 5
int count = 5;
int starting; 0
starting = count + 5;
Expression evaluation:
Get value of count: 5
Add 5 to it. 10
Assign to starting
Compiled by Dagne Walle 20
What is an Identifier?
simple structured
address
float double long double
pointer reference
Compiled by Dagne Walle 26
C++ Primitive Data Types
Primitive types
integral floating
unsigned
bool values
true (1) false(0)
age is 21 or 22
Statements Output
int x=3; double y =4.5;
cout <<“the value of x=“<<x; the value of x=3; y=4.5
cout<<“; y=“<<y<<endl;
x+y=7.5
cout <<“x+y”<<x+y;
int x=3; double y =4.5; x=3
cout <<“x = “<<x<<“\n”; y=4.5
cout<<“y=“<<y;
x+y=7.5
cout <<“\nx+y”<<x+y;
Compiled by Dagne Walle 48
Extraction Operator (>>)
Variable cin is predefined to denote an input
stream from the standard input device (the
keyboard)
The extraction operator >> called “get from”
takes 2 operands. The left operand is a stream
expression, such as cin--the right operand is a
variable of simple type.
Operator >> attempts to extract the next item
from the input stream and store its value in the
right operand variable.
Compiled by Dagne Walle 49
The Extraction Operator (>>)
To get input from the keyboard we use the
extraction operator and the object cin
Format: cin >> Variable;
No need for & in front of variable
The compiler figures out the type of the variable and
reads in the appropriate type
int X;
float Y;
cin >> X; // Reads in an integer
cin >> Y; // Reads in a float
cin >> x;
cin >> y;
int n; double x;
cout<<“enter an int n, and a double X: “;
cin>>n>>x; // use of cin
cout<<“You entered int n=“<<n;
cout<<“, and double x=“<<x<<endl;
integer1 45
integer2 72
sum = integer1 + integer2;
sum 117
C++ allows for C-style type casting with the syntax: (new
type) expression
TRUE
expression
statement(s) FALSE
false
if (Bool-Expr)
{
Statement_1
…
Statement_n
}
if (Bool-Expression )
{
“if clause”
}
else
{
“else clause”
}
false true
grade >= 60
false
true
case b case b action(s) break
false
.
.
.
true
case z case z action(s) break
false
default action(s)
switch (choice){
case 1:
cout << “The greatest ” << endl;
case 2:
cout << “Exciting team ”<< endl;
case 3:
cout << “Boring ” << endl;
case 4:
cout << “Bye Bye << endl;
}
Compiled by Dagne Walle 73
The switch Statement
1. Man United
2. Chelsea
3. Arsenal
4. Quit
Choose either 1, 2, 3 or 4:
Compiled by Dagne Walle 78
Example program to Demo
#include <iostream> //see displaymenu3.cpp
Using namespace std;
int main() {
int choice;
cout << "*********** MENU **************\n";
cout <<endl;
cout << " 1. Man United" << endl;
cout << " 2. Chelsea" << endl;
cout << " 3. Arsenal" << endl;
cout << endl;
cout << "Please choose 1, 2 or 3 : ";
cin >> choice;
switch (choice) {
case 1: cout << “The greatest team“ << endl;
break;
case 2: cout << “Exciting team“ << endl;
break;
case 3: cout << “Boring team“ << endl;
break;
case 4: cout << “Bye Bye << endl;
break;
}
return 0;
Compiled by Dagne Walle 79
}
The switch Default Statement captures errors
or perform default action
e.g. if user enter any other number
switch (choice){
case 1: cout << “The greatest ” << endl;
break;
case 2: cout << “Exciting team ”<< endl;
break;
case 3: cout << “Boring ” << endl;
break;
case 4: cout << “Bye Bye “ << endl;
break;
default: “Incorrect choice” << endl;
}
while
for
do..while
SYNTAX
while ( Expression )
{
… // loop body
}
No semicolon after the boolean expression
Loop body can be a single statement, a null
statement, or a block.
SYNTAX
do
{
… // loop body
} while ( Expression ); //note semi colon
Insured that the loop is executed at least once
The LCV is initialized/updated before the end of the loop.
Boolean expression is tested at the end of the loop.
There is a semicolon after the boolean expression.
do {
statement true
condition
} while ( condition );
false
do
{
cin >> answer ; // accept choice
Example:
for (count=1; count < 7; count++)
{
cout << count << endl;
}
for (j = 0; j < n)
int n;
int n;
OUTPUT
109
Compiled by Dagne Walle
num 1 Example of Repetition
int num;
OUTPUT
110
Compiled by Dagne Walle
num 1 Example of Repetition
int num;
true
OUTPUT
111
Compiled by Dagne Walle
num 1 Example of Repetition
int num;
OUTPUT
1Potato
112
Compiled by Dagne Walle
num 2 Example of Repetition
int num;
OUTPUT
1Potato
113
Compiled by Dagne Walle
num 2 Example of Repetition
int num;
true
OUTPUT
1Potato
114
Compiled by Dagne Walle
num 2 Example of Repetition
int num;
OUTPUT
1Potato
2Potato
115
Compiled by Dagne Walle
num 3 Example of Repetition
int num;
OUTPUT
1Potato
2Potato
116
Compiled by Dagne Walle
num 3 Example of Repetition
int num;
true
OUTPUT
1Potato
2Potato
117
Compiled by Dagne Walle
num 3 Example of Repetition
int num;
OUTPUT
1Potato
2Potato
3Potato 118
Compiled by Dagne Walle
num 4 Example of Repetition
int num;
OUTPUT
1Potato
2Potato
3Potato 119
Compiled by Dagne Walle
num 4 Example of Repetition
int num;
false
OUTPUT
1Potato
2Potato
3Potato 120
Compiled by Dagne Walle
num 4 Example of Repetition
int num;
false
1Potato
2Potato
3Potato
do
{
cout<< ”Enter salary, type -1 to exit”; // no one
earns negative salary
cin>>salary;
// process income
} while (salary > 0);
j is 50
j is 60
We are out of the loop as j=70.
Compiled by Dagne Walle 137
CONTINUE Statement
allows you to skip the rest of the loop body and go back
to the beginning of the loop.
do
{
cin>>x;
if (x % 2 == 0)
continue;
cout<<x<<endl;
} while (x <100);
//prints out all odd numbers entered less than 100
Primary
6. Execute CPU
Memory
Console
window: