02 iec t1_s1_plt_session_02
DESCRIPTION
New SlidesTRANSCRIPT
Installing Windows XP Professional Using Attended Installation
Slide 1 of 35Session 2Ver. 1.0
Programming Logic and Techniques
In this session, you will learn to:Identify variables and constants
Use data types
Use operators
Represent decisions in a flowchart
Objectives
Installing Windows XP Professional Using Attended Installation
Slide 2 of 35Session 2Ver. 1.0
Programming Logic and Techniques
Example:Flowchart to display the sum of two numbers
Variables and Constants
Start
Stop
Accept theSecond Number
Add the two Numbersand Store the Result
Display the Result
Accept theFirst Number
Installing Windows XP Professional Using Attended Installation
Slide 3 of 35Session 2Ver. 1.0
Programming Logic and Techniques
Variables and Constants (Contd.)
The internal memory consists of different locations in which data is stored.
A computer needs to identify the memory locations to be able to retrieve values from or store values in them.
The value of a variable changes each time the set of instructions is executed.
Installing Windows XP Professional Using Attended Installation
Slide 4 of 35Session 2Ver. 1.0
Programming Logic and Techniques
The values stored in the variables are known as constants.
10 15 25
nNum1 nNum2 nSum
Variables
Constants
Variables and Constants (Contd.)
Installing Windows XP Professional Using Attended Installation
Slide 5 of 35Session 2Ver. 1.0
Programming Logic and Techniques
Example:Flowchart to display the sum of two numbers using variables
Start
Stop
Accept nNum2
nSum = nNum1 + nNum2
Display nSum
Accept nNum1
Variables and Constants (Contd.)
Installing Windows XP Professional Using Attended Installation
Slide 6 of 35Session 2Ver. 1.0
Programming Logic and Techniques
Just a minute
Each day, the courier service delivers some letters. The number of letters is different each day. Regardless of the number of letters delivered by the courier service, they are paid a carrying charge of $5. Identify the variable and constant data in this situation.
Variable:
Constant:
Answer:Variable: Number of letters
Constant: Carrying charge $5
Installing Windows XP Professional Using Attended Installation
Slide 7 of 35Session 2Ver. 1.0
Programming Logic and Techniques
Identify the variables and constants from the following list:1. Age
2. Address
3. 21
4. “10, Kingsway Camp”
5. “Henri”
6. Name
7. “185”
Just a minute
Answer:Constants: 21, “10, Kingsway Camp”, “Henri”, “185”
Variables: Age, Address, Name
Installing Windows XP Professional Using Attended Installation
Slide 8 of 35Session 2Ver. 1.0
Programming Logic and Techniques
Numeric variables can contain only numbers.
These variables can be used in arithmetic operations.
Numeric
Installing Windows XP Professional Using Attended Installation
Slide 9 of 35Session 2Ver. 1.0
Programming Logic and Techniques
Character variables can contain any combination of letters, numbers, and special characters.
These variables cannot be used for calculation.
Character
Installing Windows XP Professional Using Attended Installation
Slide 10 of 35Session 2Ver. 1.0
Programming Logic and Techniques
Declaring Variables
Start
Stop
Accept nNum2
nSum = nNum1 + nNum2
Display nSum
Accept nNum1
numeric nNum1,nNum2, nSum
Declaration of Variables
Example:
Installing Windows XP Professional Using Attended Installation
Slide 11 of 35Session 2Ver. 1.0
Programming Logic and Techniques
The first letter of the variable may indicate the data type used.
The variable name should clearly describe its purpose.
In case of multiple words, the first letter of each word could be capitalized for better readability.
Variable Naming Conventions
Installing Windows XP Professional Using Attended Installation
Slide 12 of 35Session 2Ver. 1.0
Programming Logic and Techniques
Operators are symbols for some predefined operations.
The operators that are used in flowcharts are:Arithmetic operators
Relational operators
Logical operators
Using Operators
Installing Windows XP Professional Using Attended Installation
Slide 13 of 35Session 2Ver. 1.0
Programming Logic and Techniques
Arithmetic operators are used to perform arithmetic calculations.
The symbols that represent arithmetic operations are called arithmetic operators (*, /, +, -, %).
Arithmetic Operators
Installing Windows XP Professional Using Attended Installation
Slide 14 of 35Session 2Ver. 1.0
Programming Logic and Techniques
Relational operators are used to test the relationship between two variables or the relationship between a variable and a constant.
There are six relational operators (=,>,<,!=,>=,<=).
Relational Operators
Installing Windows XP Professional Using Attended Installation
Slide 15 of 35Session 2Ver. 1.0
Programming Logic and Techniques
Logical operators (AND, OR, NOT) are used to combine expressions containing relational operators:
nNum1 = 7 AND nNum2 > 5
nNum1 = 7 OR nNum2 > 5
NOT nNum2 <= 5
The order of precedence for the execution of logical operators is NOT, AND, and OR.
Logical Operators
Installing Windows XP Professional Using Attended Installation
Slide 16 of 35Session 2Ver. 1.0
Programming Logic and Techniques
Just a minute
Draw a flowchart to accept item name, price, and quantity. You need to calculate the value as the product of price and quantity, and display the calculated value and the item name using variables.
Installing Windows XP Professional Using Attended Installation
Slide 17 of 35Session 2Ver. 1.0
Programming Logic and Techniques
Just a minute (Contd.)
Answer:Start
Stop
Accept nPrice
Accept nQuantity
Display cItemName, nValue
Accept cItemName
character cltemNamenumeric nPrice, nQuantity, nValue
nValue = nPrice * nQuantity
Installing Windows XP Professional Using Attended Installation
Slide 18 of 35Session 2Ver. 1.0
Programming Logic and Techniques
Many problems require decisions to be made.
All decisions may or may not state an action to be taken if the condition is false.
Following is a flowchart segment to compare two numbers and check for equality.
Representing Decisions in a Flowchart
Is nNum1 = nNum2 ?
Yes
No
Display “The numbers are
equal”
Display “The numbers are
not equal”
Installing Windows XP Professional Using Attended Installation
Slide 19 of 35Session 2Ver. 1.0
Programming Logic and Techniques
Example:Accept two numbers and print the larger of the two numbers
Representing Decisions in a Flowchart (Contd.)
Start
Accept nNum2
Accept nNum1
numeric nNum1,nNum2
A
Installing Windows XP Professional Using Attended Installation
Slide 20 of 35Session 2Ver. 1.0
Programming Logic and Techniques
Example (Contd.):
Representing Decisions in a Flowchart (Contd.)
A
Is nNum1=nNum2?
Is nNum1>nNum2?
Stop
Display nNum2
Display nNum1
Display “ The numbers are equal”
Yes
Yes
No
No
Installing Windows XP Professional Using Attended Installation
Slide 21 of 35Session 2Ver. 1.0
Programming Logic and Techniques
Example:Print the value of nX only if the value of nX is greater than 10 and nX is an even number
Representing Decisions in a Flowchart (Contd.)
Start
Stop
Display nX
Accept nX
numeric nX
IsnX>10 AND nX%2=0?
No
Yes
Installing Windows XP Professional Using Attended Installation
Slide 22 of 35Session 2Ver. 1.0
Programming Logic and Techniques
Example:Accept the year and then determine whether the year is a leap year or not. A leap year is one that is divisible by 4, other than a century year, such as 1900. A century year, which is divisible by 400, such as 2000, is also a leap year.
To evaluate the given condition, we can interpret this as:If year is divisible by 4 AND not divisible by 100 OR divisible by 400, it is a leap year.
Representing Decisions in a Flowchart (Contd.)
Installing Windows XP Professional Using Attended Installation
Slide 23 of 35Session 2Ver. 1.0
Programming Logic and Techniques
Flowchart to determine a leap year
Representing Decisions in a Flowchart (Contd.)
Start
Accept nYear
numeric nYear
A
Display “Please enter a year”
Installing Windows XP Professional Using Attended Installation
Slide 24 of 35Session 2Ver. 1.0
Programming Logic and Techniques
Flowchart to determine a leap year (Contd.)
Representing Decisions in a Flowchart (Contd.)
A
Is nYear % 4=0 AND (nYear % 100 !=0 OR nYear % 400=0) ?
Stop
Display “This is a leap year”
Display “This is not a leap year”
No
Yes
Installing Windows XP Professional Using Attended Installation
Slide 25 of 35Session 2Ver. 1.0
Programming Logic and Techniques
Example:To decide about the discount percentage on a TV, the sales person needs to check the type of TV. If the TV is Black and White [B], the discount will be 5 percent of the selling price. If the type of TV is colored[C], then he has to verify the size of TV screen. For 14 inches screen, discount is 8 percent of the selling price and for 21 inches screen, the discount is 10 percent of the selling price.
Representing Decisions in a Flowchart (Contd.)
Installing Windows XP Professional Using Attended Installation
Slide 26 of 35Session 2Ver. 1.0
Programming Logic and Techniques
Flowchart to calculate discount
Representing Decisions in a Flowchart (Contd.)
Start
Stop
Accept cTypeAccept nScreen
numeric nScreen, nDiscountcharacter cType
IscType=‘B’?
Yes
No
IscType=‘C’?
IsnScreen=21?
IsnScreen=14?
nDiscount=5% of SP
nDiscount=8% of SP
nDiscount=10% of SP
Yes
No No
No
Yes
Yes
Installing Windows XP Professional Using Attended Installation
Slide 27 of 35Session 2Ver. 1.0
Programming Logic and Techniques
Exercises
Installing Windows XP Professional Using Attended Installation
Slide 28 of 35Session 2Ver. 1.0
Programming Logic and Techniques
Study the given flowchart and answer the following questions.
What will be the output when:nNum=7
nNum=3
nNum=11
Exercise 1
Is nNum>10?
Is nNum>5?
Stop
Display “REJECT”
Display “OK”
Display “ GOOD”Yes
Yes
No
No
Start
Accept nNum
numeric nNum
Installing Windows XP Professional Using Attended Installation
Slide 29 of 35Session 2Ver. 1.0
Programming Logic and Techniques
Study the flowchart and answer the following questions.
Exercise 2
Is nX > nY ?
Is nY > 100 ?
Stop
Display nY
Display “ GOOD”Yes
No
No
Yes
Start
Accept nX
numeric nX, nY
Accept nY
Is nX > 100 ?
Yes
No
Installing Windows XP Professional Using Attended Installation
Slide 30 of 35Session 2Ver. 1.0
Programming Logic and Techniques
Exercise 2 (Contd.)
What will be the output when:nX=150 and nY=75
nX=90 and nY=50
nX=40 and nY=80
Installing Windows XP Professional Using Attended Installation
Slide 31 of 35Session 2Ver. 1.0
Programming Logic and Techniques
Draw a flowchart to accept a number and then find out whether or not the number is divisible by 5.
Exercise 3
Installing Windows XP Professional Using Attended Installation
Slide 32 of 35Session 2Ver. 1.0
Programming Logic and Techniques
Draw a flowchart to accept three numbers and display the largest number.
Exercise 4
Installing Windows XP Professional Using Attended Installation
Slide 33 of 35Session 2Ver. 1.0
Programming Logic and Techniques
Candidates have to enter their age. The age cannot be negative. If a negative age is entered, an error message has to be displayed, otherwise the age is displayed. Represent the error checking logic for this situation using a flowchart.
Exercise 5
Installing Windows XP Professional Using Attended Installation
Slide 34 of 35Session 2Ver. 1.0
Programming Logic and Techniques
In this session, you learned that: Data can be categorized as a constant or variable.
Data types can be:Numeric
Character
The operators are:Arithmetic
Relational
Logical
Arithmetic operators are used to perform arithmetic calculations. The symbols that represents arithmetic operations are called arithmetic operators (*,/,+,-,%).
Summary
Installing Windows XP Professional Using Attended Installation
Slide 35 of 35Session 2Ver. 1.0
Programming Logic and Techniques
Relational operators are used to test the relationship between two variables. The symbols that represent relational operations are called relational operators (<,>,=,!=).
Logical operators (AND, OR, NOT) are used to combine expressions containing relational operators.
The decision box is used to apply conditions by asking a question in a flowchart.
Summary (Contd.)