06- Program Logic - Introduction
06- Program Logic - Introduction
Objectives
– 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
– Examples include:
• 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
Logical Operators
Go on Teams
– 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.
Perform calculation
Display values
2.Selection
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