0% found this document useful (0 votes)
7 views22 pages

Programming Constructs

The document explains programming constructs including variables, expressions, statements, and pseudocode. It highlights the importance of variables for storing data, the types of expressions and statements, and the use of pseudocode for planning and communicating program logic. Additionally, it covers flowcharting techniques and the use of subroutines for efficient programming design.

Uploaded by

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

Programming Constructs

The document explains programming constructs including variables, expressions, statements, and pseudocode. It highlights the importance of variables for storing data, the types of expressions and statements, and the use of pseudocode for planning and communicating program logic. Additionally, it covers flowcharting techniques and the use of subroutines for efficient programming design.

Uploaded by

Siva sai Danush
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Programming Constructs

by Praveen Kumar
What is a Variable? Variable Representation (Conceptual)
A variable is a named memory location used to store data
age = 25
that can change during program execution.

< Think of a variable as a container that holds a value.


age ³ variable name
Why Do We Need Variables?
25 ³ value stored in memory
To store user input
To store intermediate results
Rules for Variable Names
To reuse values in a program Must start with a letter or underscore

To make programs flexible and dynamic Can contain letters, digits, underscore
Should not be a keyword
Case-sensitive (age and Age are different)
What is an Expression?
An expression is a combination of variables, constants, and operators 3. Logical Expressions
that produces a value.
(age > 18) AND (citizen = true)
< Expressions are evaluated, not executed.

Types of Expressions 4. Assignment Expressions


1. Arithmetic Expressions
x=x+1

total = a + b
area = length * breadth
Example

2. Relational (Comparison) Expressions sum = num1 + num2

a>b
marks >= 50 num1 + num2 ³ expression
Result is assigned to sum
What is a Statement? 3. Output Statement

A statement is a complete instruction that tells the computer to perform an


PRINT total
action.

< Statements are executed. 4. Conditional Statement

Types of Statements IF marks >= 50 THEN


1. Assignment Statement PRINT "Pass"
ENDIF
total = price * quantity
5. Loop Statement
2. Input Statement
FOR i = 1 TO 5
READ number PRINT i
ENDFOR
Difference Between Variable, Expression, and Statement
Aspect Variable Expression Statement

Meaning Stores data Produces a value Performs an action

Example x x + 10 x = x + 10

Execution Stored Evaluated Executed

Real-World Analogy
Programming Real World

Variable Container

Expression Calculation

Statement Instruction
What is Pseudocode?
Pseudocode is an informal, human-readable way of Why Do We Use Pseudocode?
describing the logic of a program.
It looks like a mix of plain English and programming Pseudocode helps to:
constructs, but it is not written in any specific
Understand the problem logic clearly
programming language.
'

' Plan the solution before writing code


' Focus on what to do, not how to code

' Communicate ideas easily with others


' Reduce errors during actual implementation

Think of pseudocode as a bridge between problem understanding and actual coding.


Key Characteristics of Pseudocode Common Pseudocode Keywords
Simple English statements
Purpose Keyword
Ë No strict syntax rules
í Language-independent Start program START, BEGIN

Uses common programming concepts


End program END, STOP
¨

~ Easy to read and modify


Input READ, INPUT

Output PRINT, DISPLAY

Decision IF, ELSE, ENDIF

Loop FOR, WHILE, REPEAT

Assignment SET, =
Basic Structure of Pseudocode Example 1: Add Two Numbers

START START
Read input READ num1
Process the data READ num2
Display output SET sum = num1 + num2
END PRINT sum
END
Example 2: Check Even or Odd xample 3: Loop Example

START START
READ number FOR i = 1 TO 5
IF number MOD 2 = 0 THEN PRINT i
PRINT "Even Number" ENDFOR
ELSE END
PRINT "Odd Number"
ENDIF
END
Pseudocode vs Flowchart Where Pseudocode is Commonly Used
Algorithm design
Pseudocode Flowchart
€

ù Teaching beginners
Text-based Diagram-based ÷ Exam answers
Technical interviews
Easy to write Visual representation
d

í Problem-solving discussions
Preferred by Preferred for
programmers presentations
Efficient Flowcharting Techniques

1. Purpose of Efficient Flowcharting Standard Flowchart Symbols (Use Consistently)

Efficient flowcharting helps to:

7 Clearly understand program logic


7 Identify logical errors early
7 Improve communication among team members
7 Simplify complex processes
7 Serve as documentation for future reference
Symbol Shape Purpose

Terminator Oval Start / End

Process Rectangle Calculation / Processing

Input / Output Parallelogram Read / Print

Decision Diamond Yes / No condition

Flow line Arrow Direction of flow

Connector Circle Connects flow


3. Follow a Clear Flow 4. Use One Entry and One Exit
Direction Technique:
Technique: Each flowchart should have:
One START
Flow should start at the top and move downward
One END
Avoid leftward or upward flows unless necessary

Benefit: Benefit:
Prevents ambiguous logic
Enhances readability
Ensures structured design
Reduces confusion
5. Keep Flowcharts Simple and 6. Use Meaningful Descriptions
Modular Technique:
Technique: Use clear, concise text inside symbols
Prefer action verbs
Break large flowcharts into smaller sub-flowcharts
Use predefined process (subroutine) symbols o Bad:

Benefit: Process Data


Easier debugging
Better maintenance
' Good:

Calculate Total Marks


7. Limit Decision Complexity Clearly Label Decision Paths
Technique:
Each decision should test only one condition
Avoid combining multiple conditions in one diamond

o Bad:

marks > 50 AND attendance > 75

' Good:

Split into two decisions

Benefit:
Easier testing
Clear logic paths

Technique:
Label arrows as YES / NO or TRUE / FALSE

Benefit:
Eliminates ambiguity
Helps beginners understand logic
9. Avoid Crossing Flow Lines 11. Maintain Consistent Spacing and Alignment
Technique: Technique:
Reorganize layout to avoid intersecting arrows Align symbols vertically
Use connectors if unavoidable Maintain equal spacing

Benefit: Benefit:
Improves visual clarity Professional appearance
Easier comprehension
10. Use Connectors Effectively
Types: 12. Show Loops Clearly
On-page connector ³ small circle
Off-page connector ³ pentagon

Benefit:
Keeps flowchart neat
Prevents overcrowding
What is a Subroutine in a Flowchart?

A subroutine is a group of instructions that performs a specific task


and can be reused multiple times.

< In flowcharts, a subroutine is represented using a Predefined


Process symbol.

Symbol:
Shape: Rectangle with double vertical sides
Meaning: Calls another set of steps (subroutine)
2. Why Use Subroutines in 3. When Should You Use a
Flowcharts? Subroutine?
Using subroutines helps to: Use a subroutine when:

7 Avoid repetition of logic The same steps are repeated


7 Reduce flowchart size A task is logically independent

7 Improve readability A process is complex and lengthy

7 Simplify debugging You want clean and professional flowcharts

7 Promote modular thinking


7 Reflect real programming practices
(functions/methods)
4. How Subroutines Work in 5. Example Scenario
Flowcharts Problem:
Flow: A program needs to:

1. Main flowchart reaches a subroutine symbol Read marks for multiple students
2. Control transfers to the subroutine flowchart Calculate total and average
3. Subroutine executes its steps Display results
4. Control returns to the next step in main flowchart
< The calculation logic repeats for every student.
6. Flowchart WITHOUT Subroutine 7. Flowchart WITH Subroutine (Efficient
(Inefficient) Design)
Calculation steps repeated multiple times Main Flowchart
Flowchart becomes long and cluttered

o Not recommended
8. Representing Subroutines 9. Passing Data to
Correctly Subroutines
Best Practices: Conceptually:
Give meaningful names (e.g., CalculateSalary) Inputs ³ Passed to subroutine

Keep subroutines single-purpose Outputs ³ Returned to main flow


Avoid input/output inside subroutine unless necessary Example:
Clearly indicate return to calling point

Call Calculate_Average(marks)
Receive average

You might also like