Download as pdf or txt
Download as pdf or txt
You are on page 1of 14

Basic of Programming

February 14, 2023


One of the objectives of this course is let the students design a program
that will help them compute/solve CE problems.

For your compiler/programming language you can use VB Excel just refer
to this link for quick toturial
https://www.youtube.com/watch?v=78GvLEAZecs

Or you can use any other compiler/programming language you are


comfortable with.
Programming Environment - these are different programming
languages/compilers

Data Types
string - consists of one or more characters
numeric data
integer (if no decimal on numeric data)
float/double, single (if decimal is present, double has higher space
allocation than single)
Boolean data - true/false, or 0/1

Variables - these are the names you give to the computer memory
allocations which are used to store values in computer program
Keyword - is a word reserved by a program because the word has a special
meaning
example: ”printf”, ”MsgBox”

Logical Arithmetic Operators


Arithmetic Operators
+, −, ∗,÷, % (modulo)
Relation Operators
A == B ”A is equal to B”
A! = B ”A is not equal to B”
>, < , <=, >=
Logical Operators
&& AND
|| OR
! NOT
If else/ Decision Making
If statement
If (condition)
task task is perfomred if the given condition is satisfied
If - else statement
If (condition)
task1 task1 is perfomred if the given condition is satisfied,
else
task2 otherwise it will perform task2
If - else - if statement
If (condition1)
task1 task1 is perfomred if the given condition1 is satisfied,
else if (condition2)
task2 task2 is performed if condition1 fails but condition2 is satisfied,
else
task3 otherwise it will perform task3
Illustrations:
For this illustration we will assume that the variable age = 16.
1 if (age < 20)
task = write the word ”hello”

Expected output: hello

2 if (age > 20)


task1 = write the word ”hello”
else
task2 = write the word ”hi”

Expected output: hi
Loops - is a sequence of instructions that is continually repeated until a
certain condition is reached

For Next Loop - allows you to perform task/s for the specified
number of time

Do While Loop - allows you to execute first the task/s before the
while condition is checked

While - allows you to check for a condition and run the loop while
that condition is met
Illustrations:
For this illustration we will assume that the variable age = 16.
1 For (i = 1 to age)

task = write the word ”hello”

Expected output: hellohellohello . . . hello (there are 16 ”hello’s”)

2 Do while loop
i = 16
Do
task = write the word ”hello”
i =i +1
while (i < age)

Expected output: hello


(only one ”hello” since condition is not satisfied thus the loop is
terminated)
Illustrations:
For this illustration we will assume that the variable age = 16.
1 While loop
i = 16
While (i < age)
task = write the word ”hello”
i =i +1

No output
( since condition is not satisfied thus the loop is terminated before the
rask is performed)
Practice!
Design a program that will compute the sum of all even integers from 1 to
20.

Solution using For Loop:


integer i, sum =0 (initialize variable sum to 0)
For (i = 1 to 20) (i runs from 1 to 20)
If (i mod 2 = 0) ( mod 2 gives a remainder when i is divided by 2)
sum = sum + i (value of i is added to variable sum)
End Loop
Practice!
Design a program that will compute the sum of all even integers from 1 to
20.

Solution While Loop:


integer sum =0 (initialize variable sum to 0)
integer i =1 (initialize variable i to 1)
While (i <= 20 ) (condition to be satisfied to run the loop)
If (i mod 2 = 0) ( mod 2 gives a remainder when i is divided by 2)
sum = sum + i (value of i is added to variable sum)
i =i +1 (increment variable i)
End Loop
Practice!
Design a program that will compute the sum of all even integers from 1 to
20.

Solution Do While Loop:


integer sum =0 (initialize variable sum to 0)
integer i =1 (initialize variable i to 1)
Do
If (i mod 2 = 0) ( mod 2 gives a remainder when i is divided by 2)
sum = sum + i (value of i is added to variable sum)
i =i +1 (increment variable i)
While (i <= 20 ) (condition to be satisfied to run the loop)
End Loop
Function -is used if you want your program to perform a task that returns
a result

example:
Function Area (x (var type), y (var type)) (return type)
Area = x*y
End Function

Main Program
z (var type)
z = Area (3,5)
End Program
ACTIVITY 1

Design a program that will compute the product of all integers form 1
to 100 that are divisible by 8.
Submit a handwritten answer, write your name and section on the
upper left corner of your paper.
Submit your work on or before Feb. 16, 2023.

You might also like