[go: up one dir, main page]

0% found this document useful (0 votes)
25 views34 pages

LBEPS Session 11

The document discusses variables, constants, data types, and operators used in logic building and problem solving. It defines variables and constants, describes how to declare variables and assign values to them. It also explains different data types and provides examples of arithmetic, relational, and logical operators.

Uploaded by

Dilleiju
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPSX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views34 pages

LBEPS Session 11

The document discusses variables, constants, data types, and operators used in logic building and problem solving. It defines variables and constants, describes how to declare variables and assign values to them. It also explains different data types and provides examples of arithmetic, relational, and logical operators.

Uploaded by

Dilleiju
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPSX, PDF, TXT or read online on Scribd
You are on page 1/ 34

Logic Building and Effective

Problem Solving
Objectives

In this chapter, you will learn to:


Use variables and constants
Identify data types
Identify operators
Variables and Constants

A variable refers to the memory location that can store only


one value at a time but can vary throughout the program
execution.
A constant refers to a memory location that holds a value,
which remains fixed throughout the program execution.
Variables and Constants
(Contd.)

Consider an example of a program that accepts two numbers


and displays their sum.
This program needs three variables, two to store the numbers
entered by the user and one to store the sum of the two
numbers.
For the given example, the following variables are declared:
first_number
second_number
Sum
Variables and Constants
(Contd.)

The following figure shows the graphical representation of the


variables in memory.
3.1 Let’s Practice

Let us perform the following exercises:


1. You have to write a pseudocode that accepts two numbers and
displays their sum and product. Considering the preceding
requirements, identify the variables and constants required for
writing the pseudocode.

Solution:
While writing a pseudocode that accepts two numbers and displays their
sum and product, you need to use the following variables:
nFirstNumber
nSecondNumber
nSum
nProduct
3.1 Let’s Practice (Contd.)

2. You have to write a pseudocode that accepts the product name


and quantity as input and displays the total price using the
unit prices already stored in the system. Considering the
preceding requirements, identify the variables and constants
required for writing the pseudocode.

Solution:
While writing a pseudocode that accepts the product name and
quantity as input and displays the total price using the unit prices already
stored in the system, you need to use the following variables:
nProductName
nQuantity
nTotalPrice
In addition, you need to use the following constant:
nPrice
Data Types

Data values can be broadly categorized in the following types:


Numeric
Character
A variable should be declared along with the data type.
The data type specifies the type of value to be stored by the
variable.
Each data type allocates a fixed amount of memory space,
according to the programming language specification.
Declaring Variables

It is essential to declare a variable so that memory is


allocated for a value before it is used in a program.
While naming a variable, the following rules must be followed:
It can consist of letters (uppercase as well as lowercase), digits,
or underscores.
It must begin with a letter or an underscore.
It must not contain any embedded space and symbols, such
as ?, ! @ # $ % ^ & * ( ) { } [ ] . , : ; “ ‘ / \.
Declaring Variables

In addition to the variable naming rules, you may consider the


following variable naming conventions:
The first letter of the variable may indicate the data type used.
The variable name should clearly describe its purpose.
In case the variable name consists of multiple words, the
first letter of each word could be capitalized for better readability.
Syntax to declare a variable is:
data_type variable_name
Assigning Values to
Variables

All variables need to be assigned a value before their use.


This is to ensure that the memory space allocated to the
variable is initialized with a valid value.
The variables can be assigned values by using the following
two methods:
Direct assignment
Accept statement
Assigning Values to
Variables (Contd.)

Direct assignment:
Assigns values to variables by using the equal to (=) sign.
Syntax:
variable_name = value
For example:
begin
numeric nHeight, nAge, nCounter
character cCode
nHeight = 172
nAge = 35
nCounter = nAge + 10
cCode = ‘XYZ’
end
Assigning Values to
Variables (Contd.)

Accept statement:
Assigns values to variables by using the accept keyword that
gets the value from a user.
Syntax:
begin

accept variable_name

end
For example:
begin
character cName
numeric nAge
display ‘Enter your name:’
accept cName
Assigning Values to
Variables (Contd.)

display ‘Enter your age:’


accept nAge


end
Just a Minute

Which one of the following variables does not comply with the
variable naming conventions?
1. nQuantity
2. nUnit Price
3. nProductName
4. nCost

Solution:
2. nUnit Price
3.2 Let’s Practice

Let us perform the following exercises:


1. Which of the following variables is/are declared as per variable
naming conventions:
nAverage Salary
%Score
cFirst Name
@Address
cName
cStreet_Address
Unit$Price
customerName

Solution:
cName
cStreet_Address
customerName
3.2 Let’s Practice (Contd.)

2. Write a pseudocode to accept two numbers, multiply the numbers


and then, display their product.

Solution:
begin
numeric nNum1, nNum2, nProduct
accept nNum1
accept nNum2
nProduct = nNum1 * nNum2
display ‘The product of ‘ + nNum1 + ‘ and ‘ + nNum2
+ ‘ is ‘ + nProduct
end
3.2 Let’s Practice (Contd.)

3. Write a pseudocode that accepts the temperature in Celsius,


converts it into Fahrenheit, and then, displays the result.
Hint: Temperature in Fahrenheit = (9/5)*Temperature in
Celsius+32

Solution:
begin
numeric nCelsius, nFahrenheit
accept nCelsius
nFahrenheit = (9/5) * nCelsius + 32
display ‘The temperature in Fahrenheit is ‘ +
nFahrenheit
end
Using Operators

All computer languages provide tools for some predefined


operations.
These tools are known as operators. The operators are
categorized in the following ways:
Arithmetic operators
Relational operators
Logical operators
Arithmetic Operators

Arithmetic operators are used to perform arithmetic calculations.


The following table describes some of the commonly used
arithmetic operators.
Operator Description Example Value of nNum Value of nNum
before operation after operation
* Multiplies two numbers
and returns the product nNum = nNum * 2 4 8

Divides two numbers


/ and returns the quotient nNum = nNum / 2 4 2

Adds two numbers and


+ returns the sum nNum = nNum + 2 4 6

Subtracts two numbers


- and returns the nNum = nNum – 2 4 2
difference
Arithmetic Operators

Operator Description Example Value of nNum Value of nNum


before operation after operation
Divides two numbers
% and returns the nNum = nNum % 3 4 1
remainder
Arithmetic Operators
(Contd.)

The precedence of arithmetic operators is shown in the


following table.
Operator Description

* High

+
Low
-
Relational Operators

Relational operators are used to compare two variables or


constants.
The following table describes the commonly used relational
operators assuming that the value of a variable, nNum1 is 25
and that of a variable, nNum2 is 40.
Operator Description Example Result

== Equal to nNum1 == nNum2 False

> Greater than nNum1 > nNum2 False

< Less than nNum1 < nNum2 True

!= Not equal to nNum1 != nNum2 True

>= Greater than equal to nNum1 >= nNum2 False


Logical Operators

Logical operators are used to combine expressions containing


relational operators.
The following table describes the evaluation of the various
logical operators assuming that the value of a variable, nNum1
is 7 and that of a variable, nNum2 is 6.
Operator Description Example Result
Evaluates to true only
if all the individual nNum1 == 7 AND
AND conditions evaluate to True
nNum2 > 5
true.
Evaluates to true even
if one of the individual nNum1 == 7 OR
OR conditions evaluates True
nNum2 < 5
to true.
Evaluates to true if the
NOT condition is not met. NOT nNum2 <= 5 True
Logical Operators (Contd.)

The precedence of logical operators is shown in the following


table.
Operator Description

High
NOT

AND
Low
OR
Logical Operators (Contd.)

For example, the following Word document shows the flowchart


to decide whether discount needs to be calculated based on
certain conditions.

Calculate
Discount
Logical Operators (Contd.)

The preceding flowchart can be converted into the following


pseudocode segment:
begin
character cDay
numeric nDiscount
accept cDay, nDiscount
if ((cDay != ‘Sunday’) AND (nAmount > 5000))
begin
//Compute Discount
end
end
Scratch

Several tools are available that let you create pseudocodes and
view their output.
You will use a tool called Scratch for this purpose.
Scratch allows you to execute and debug pseudocode.
Scratch has its own syntax and semantics that need to be
followed to ensure the creation of error free pseudocode.
Scratch (Contd.)

Let us see how to use Scratch to create


a pseudocode to add two numbers.
Scratch (Contd.)

Click the following link to view a fun example using Scratch.

Game
Scratch (Contd.)

Click the following link to view another fun example using


Scratch.

Bouncing Ball
Just a Minute

Which one of the following expressions returns the result as


true?
1. (4+3)>(5+2)
2. (2+2-2)>(3+3-5)
3. 56>=(9*7)
4. 76>(8*5+66)

Solution:
2. (2+2-2)>(3+3-5)
Summary

In this chapter, you learned that:


A variable refers to the memory location that can store only one
value at a time but can vary throughout the program execution.
The common data types are:
Numeric
Character
The variables can be assigned values by using the following two
methods:
Direct assignment
Accept statement
All computer languages provide tools for some predefined
operations. These tools are known as operators.
What’s Next?

Before the next session, please ensure to:


Read LBEPS Chapter 3.
Complete the Lab@Home exercises as per the learning plan.
The learning plan is available on the technology page.
Go through the Additional Reference Links of LBEPS Chapter
3 through CloudScape.

You might also like