You are on page 1of 55

Introduction to Programming

EECP1290

Preliminary Concepts
What is a computer?
How does a computer work?
Numbers

Programming Languages

Machine Level
Assembly Level
High Level

Machine Level Language


Basic language of computers
Machines only know two numbers:
Zero
One
01000101 01000101 01000011 01010000
00100000 0110001 0110010 0111001 0110000

Also called Object Code or Binary Code

Assembly Level Language


Use mnemonic or symbolic codes for problem
solving
Cannot be understood by computers
Requires an ASSEMBLER as translator
Assembly Language
Program

ASSEMBLER

Machine Language
Program

High Level Languages


Use general English words
More user friendly than Assembly language
Requires a COMPILER as translator

High Level Language


Program

COMPILER

Machine Language
Program

Levels of Languages
Fortran, C, Pascal

High Level Language


Assembly Language

Machine Language
Hardware

Programming Steps
1. Write the algorithm
2. Draw the flowchart
3. Implement the program

Step 1. Algorithm
Steps that are written in English in order to
solve a problem
Cannot be understood by computers but
understood by programmers

Step 2. Flowchart
Pictorial data of a solution to a problem
Used to check flow of information from start
to end
Use standard symbols

Examples
Adding two numbers
Printing a name

Exercise
Write an algorithm to read the marks of a
student and then calculate the sum and
average
Convert the algorithm to flowchart

Step 3. Implementation
Convert the flowchart to code

Creating program in Visual C++


1. Start > Microsoft Visual Studio 2010
2. Select Visual C++ Development setting and
choose Start Visual Studio
3. Click on New Project
4. Select Win32 Console Application
5. Provide File and choose path
6. Click Finish

Type this on your stations


#include stdafx.h
#include<iostream>
#include<conio.h>

Preprocessor Area

using namespace std;

int _tmain(int argc, _TCHAR* argv [])


Data Type
Beginning of function body
{
cout<<Hello World!;
getch();
main function body
return 0;
}
End of function body

Program Execution
Compile the program
Run the program

On your own, create a code to have


the output shown below

The file must be named ICT exercise and must be put in a folder named Practice on the
Desktop

Solution

Review
What are the thee levels of
programming language?
Machine Level
Assembly Level
High Level

Review
It converts High Level Language to
Machine Language
Compiler

Review
It converts High Level Language to
Machine Language
Compiler

Review
Give another name for Machine
Language
Object Code, Binary Code

Review
It use mnemonic code or symbolic
code
Assembly Level Language

Review
What are the three programming
steps?
1. Algorithm
2. Flowchart
3. Code

Review
Steps written in English to solve a
programming problem
Algorithm

Review
Flowchart symbol used for input or
output

Review
What is the meaning of this
symbol?

Start / Stop

Review
Which line has the definition for
function getch()?
a.
b.
c.

#include stdafx.h
#include <iostream>
#include <conio.h>

Type this on your stations


#include stdafx.h
#include<iostream>
#include<conio.h>

using namespace std;


int _tmain(int argc, _TCHAR* argv [])
{
int A;
cout<<Enter the input: ;
cin>>A;
cout<<The number is <<A;
getch();
return 0;
}

Recall
Adding two numbers using algorithm and
flowchart

Declaration of Variables
Integer
Float
Character

A variable is a symbol usually a letter used to represent a certain


value or quantity

Integer
Whole numbers including zero and negative
numbers
To declare an integer, use the keyword int
Example:
int A;
int A, B, C;

Float
Involves numbers with decimal parts
Use the keyword float for declaration
Example:
float X;
float Y, Z, W;

Character
Variables used to store alphabetical letters
To declare a character, use the keyword char
Example:
char M
char N, O, Y;

Syntax for Declaring Variable


datatype variable_name;
datatype variable1, variable2, variable3;

Rules for Variable Names


Variables may contain letters (A-Z, a-z), digits
(0-9), and underscores (_)
First letter can only be a letter or underscore
Variables cannot be a reserved word
C is case sensitive
auto
short
continue
switch
goto
volatile
union

const
struct
else
void
register
do
while

double
unsigned
for
case
sizeof
extern

float
break
long
default
typedef
return

int
if
signed
enum
char
static

Initializing a Variable
Giving a variable an initial value
Example:
int A=5;
int D, E, F=7, G;

Reading the Input, Writing the Output


>>
<<

extracts data from an input stream


inserts data into output stream

cin>>a;

inputs data into a

cout<<a; outputs the value of a

Operators
Arithmetic
Relational
Logical

Arithmetic Operators
OPERATOR

ACTION

+
*
/
%
-++

Subtraction
Addition
Multiplication
Division
Modulus
Decrement
Increment

Relational Operators
OPERATOR

MEANING

>
>=
<
<=
==
!=

Greater than
Greater than or equal
Less than
Less than or equal
Equal
Not Equal

Logical Operators
OPERATOR

MEANING

&&
||
!

AND
OR
NOT

Review
Give three different types of
variables
integer
float
character

Review
Make a declaration for a variable with
name age that can accept an integer
value.

int age;

Review
What is the data type in the code
below?
char name, address, GSM;
character

Review
Declare a variable X and give it an
initial value of 3.28

float X=3.28

Review
Which is a valid variable name?
A.
B.
C.
D.

cout
first name
2ndname
_GSMNumber

Review
Give three different types of
operators
Arithmetic
Relational
Logical

Review
Give at least three arithmetic
operators

--

++

Review
Give the three logical operators

&&

||

Review
Find the answer for the following
1. X = 9*2+3-7%8
2. M = 4*4+4-4/4
3. S = (8+2*3) 23%4

Writing Program Codes


Read the power (W) and current (I) of an
electrical line. Calculate the voltage (V) and
resistance (R) using the formula:
Voltage=Power/Current
Resistance= Voltage/Current
Print the voltage and resistance with proper
units.

Writing Program Codes


Read the power (W) and current (I) of an
electrical line. Calculate the voltage (V) and
resistance (R) using the formula:
Voltage=Power/Current
Resistance= Voltage/Current
Print the voltage and resistance with proper
units.

You might also like