0% found this document useful (0 votes)
3 views

06- Program Logic - Introduction

The document outlines programming logic, including definitions of arithmetic, logical, and relational operators, as well as the structure theorem which divides programs into smaller tasks for efficiency. It discusses programming errors, types of operators, and the importance of structured programming through sequence, selection, and repetition. Examples illustrate how structured programming can simplify complex tasks and reduce errors.

Uploaded by

susebstein
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

06- Program Logic - Introduction

The document outlines programming logic, including definitions of arithmetic, logical, and relational operators, as well as the structure theorem which divides programs into smaller tasks for efficiency. It discusses programming errors, types of operators, and the importance of structured programming through sequence, selection, and repetition. Examples illustrate how structured programming can simplify complex tasks and reduce errors.

Uploaded by

susebstein
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 33

Program Logic

Objectives

– Define Programming Logic


– Arithmetic Operators
– Logical Operators
– Relational Operators
– The Structure Theorem
o Sequence
o Selection structure (branching)
o Loop or iteration structure (repetition)
Programming Logic

– Refers to precise ordered steps needed to transform computer input, through


processing, into output.

– Processing usually includes the following:


o using expressions (A + B = C),
o sequence of steps (Step1, Step2, Step3…),
o Selection (if...else),
o Repetition (looping),
o or a combination of all these structures.
Programming Errors

– When programming logic is not done properly, it may introduce errors.

– We have two types of errors in programming:


1) Syntax Errors : occurs when we make a mistake in our coding, e.g.: forgetting a
semicolon to indicate the end of a statement.
2) Logic Errors : occurs when we have all the correct syntax but we coded a
portion of the program with an error, such as maybe, divide by zero

– We need to know precisely how to process input into output, and this reduces logic and
syntax errors in our program solution.
Expressions

– Refer to a combination of values and functions that are combined to create a new value.
Operators & Operands

– The operators indicate what action or operation to perform.

– Examples include:

+, -, /, *, =, +=, -=, etc.

– The operands indicate what items to apply the action to.


Type of Operators

We have different types of operators:


Arithmetic Operators

• These are mathematical operations performed using arithmetic operators like addition,
multiplication, subtraction, division and modulus.

Assume variable A holds


10 and variable B holds
20 then:
Types of Arithmetic operators
• They are classified based on the number of operands used in the operations.

• The following are the two types:


Binary operators
• They perform operations on two operands.
Unary operators
• They perform operations on a single operand.

e.g.: +5 indicates positive value

e.g.: -5 indicates negative value

e.g.: ++num increases value on


num by 1
e.g.: --num decreases value on
num by 1
e.g.: !True is False
Relational Operators

Assume
variable A holds 10
and variable B holds
20
Logical Operators

Assume variable A holds 1 and variable B holds 0


Assignment Operator
Exercise

Go on Teams

Open Programming Logic Exercise Day 6.pdf


The Structure Theorem

– According to the structure theorem, a whole computer program can be


divided into small pieces of codes that are used to perform a particular task.

– These smaller tasks can be merged in such a way that these tasks can perform
any larger task without making any errors.
Example: The Structure Theorem
Real-life scenario:
If someone wants to place an order for a pizza at a restaurant along with cold
drinks, they have to follow some steps to do that.
Step 1: place an order for the pizza first
Step 2: place order for cold drinks.
Step 3: If the cold drink option is not available, then the individual may look for another
option.
Step 4: If all the conditions get satisfied, then the complete order can be placed in steps.

Here, it can be seen that the whole order is placed into two small orders to
avoid any errors in placing the overall order.
Example: The Structure Theorem
A similar concept is used in computer programming and it is called structure theorem.

If a program consists of a large task, the computer


takes lots of time to perform the task. But, if the
larger task is divided into smaller tasks, the
computer can do it easily and fast. If there are any
errors in the smaller tasks, it would be easy to find
and rectify those errors. In computer science,
these smaller tasks are known as subprograms of a
main program.
The Structure Theorem
In the structured program theorem, the three different ways for
subprograms to combine into a large program:

1) Sequence structure (simple and straight


program)

2) Selection structure (branching)

3) Loop or iteration structure (repetition)


1. Sequence Structure
The sequence control structure is a basic structure of
a simple program.

Receive number input


It follows straightforward execution of a program.

In sequence structure, all the statements are


Perform calculation
executed one after another, in the sequence they
are written in.
Assign values
The statements written first are executed first.

The sequence structure follows the top down


approach.
Sequence Structure

Receive number input

Perform calculation

Display values
2.Selection

– Used when the flow of control of the program is based on a


condition or a choice between two actions.
Selection Continued …
To make the correct choice or selection, it is completely dependent on whether the given
condition is true or false.

The selection structure program helps in making the decision abilities of the computer
program.
Selection Continued …
Consider the task of finding the smaller number of two given numbers. The following
program will find out the smaller number among the two numbers. If number A is smaller
than number B, then number A will be displayed. Otherwise, number B will be displayed.

Pseudocode: Flowchart:

BEGIN
Input A, B
IF A is smaller than
B THEN
Display A
OTHERWISE
Display B
END IF
END
Types of IF Selection
Statements
a) Simple if statement
b) Simple IF with an Else
c) Nested IF statements
d) CASE Statement
a) Simple IF
– Test a condition if the condition is true the Action is taken otherwise nothing is done.

– Testing the condition, the condition has two values only TRUE OR FALSE
b) Simple IF with Else
The condition is tested if true, the statements on the true part are executed and if false the
statements on the false part are executed

Syntax:
IF(Condition) THEN
true statements
Else
False statements
EndIF
c) Nested IF
Nested if statements is when an IF statement is inside another IF such that for it to be
executed the condition of the first if statement has to be true therefore it is a way 0f testing
several conditions.

IF (logical-expression)
THEN statements
IF (logical-expression)
THEN statements
ELSE statements
END IF
d) Case Statement
BEGIN
number = INPUT
switch(number)
BEGIN
case 10: PRINT "case 10"
break
case 20: PRINT "case 10"
break
case 30: PRINT "case 10"
break
default: PRINT "None
matches"
break
END
3) Repetition
Used when a program needs to repeat a set of statements more than once, based on certain conditions.

Pseudocode:

BEGIN
Let i = 1
Do While i <= 5
Display
“Hello”
increase i +=
1
End loop
Display “Program
Over”
END
Types of loops/ Iteration

1. Counted
a. For
b. While

2. Sentinel/ Stop Value Based


a. While
b. Do… While
Exercise
Go on Teams

Attempt: The Structure Theorem Exercise.pdf


The End

You might also like