Table of Contents

    Constants

    Programming Mastery

    Constants

    Learn what constants are in programming, why they are used, how they differ from variables, and how fixed values make programs safer, cleaner, and easier to maintain.

    What is a Constant?

    A constant is a named value that should not change during the execution of a program.

    In simple words, a constant is like a fixed value stored with a name. Once the value is assigned, it is expected to remain the same throughout the program.

    A constant is a fixed value that should not change while the program is running.

    For example, the number of days in a week is always 7. If a program needs this value, we can store it as a constant named DAYS_IN_WEEK.

    Easy Real-Life Example

    Constant as a Locked Box

    Imagine a box that is locked after placing something inside it. You can read what is written on the box, but you cannot change what is inside.

    A constant works in a similar way. It stores a fixed value, and the program should not change that value later.

    Why are Constants Important?

    Constants are important because some values in a program should remain fixed. If such values are accidentally changed, the program may produce incorrect results.

    Importance of Constants

    • Constants store fixed values.
    • They prevent accidental changes to important values.
    • They make code easier to read and understand.
    • They make programs easier to maintain.
    • They reduce repeated hard-coded values.
    • They help avoid magic numbers in code.
    • They make updates easier because the value is defined in one place.
    • They improve reliability and consistency in programs.

    Simple Example of a Constant

    The following pseudocode shows how a constant can be used.

    CONSTANT DAYS_IN_WEEK = 7
    
    DISPLAY DAYS_IN_WEEK

    Expected Output

    7

    Here, DAYS_IN_WEEK is a constant because the number of days in a week should not change during program execution.

    Variable vs Constant

    Variables and constants both store values, but the major difference is whether the value can change.

    Variable Constant
    Value can change during program execution. Value should remain fixed during program execution.
    Used for data that may vary. Used for fixed rules, limits, or standard values.
    Example: score = 50, then score = 80 Example: PASS_MARK = 35
    Useful for dynamic data. Useful for stable and repeated values.

    Constants Store Fixed Data

    Constants are commonly used to store values that have a fixed meaning in the program.

    CONSTANT PASS_MARK = 35
    CONSTANT DAYS_IN_WEEK = 7
    CONSTANT PI = 3.14159
    CONSTANT MAX_LOGIN_ATTEMPTS = 3

    These values are important program rules. If they are changed accidentally, the program may behave incorrectly.

    Common Examples of Constants

    Constants are useful in many real-world programs.

    Constant Name Value Purpose
    PI 3.14159 Used in circle-related calculations.
    DAYS_IN_WEEK 7 Represents number of days in a week.
    PASS_MARK 35 Represents minimum marks required to pass.
    MAX_ATTEMPTS 3 Limits number of attempts.
    TAX_RATE 0.18 Stores tax percentage used in calculation.
    APP_NAME "Programming Mastery" Stores application or course name.

    Constant Declaration

    Constant declaration means creating a constant and assigning its fixed value.

    Different programming languages use different syntax for constants. In this course, we will use language-neutral pseudocode.

    CONSTANT CONSTANT_NAME = value

    Example

    CONSTANT PASS_MARK = 35
    CONSTANT COURSE_NAME = "Programming Mastery"

    Here, PASS_MARK and COURSE_NAME are constants.

    Constants Should Not Be Changed

    Once a constant is defined, its value should not be changed later in the program.

    Correct Usage

    CONSTANT PASS_MARK = 35
    
    IF marks >= PASS_MARK THEN
        DISPLAY "Pass"
    ELSE
        DISPLAY "Fail"
    END IF

    Incorrect Idea

    CONSTANT PASS_MARK = 35
    
    SET PASS_MARK = 40

    This is not correct because a constant should not be reassigned after it is defined.

    Constants Make Code More Readable

    Constants help explain the meaning of fixed values. This makes the program easier to understand.

    Without Constant

    IF marks >= 35 THEN
        DISPLAY "Pass"
    END IF

    Here, the value 35 is used directly. A beginner may not immediately know what 35 means.

    With Constant

    CONSTANT PASS_MARK = 35
    
    IF marks >= PASS_MARK THEN
        DISPLAY "Pass"
    END IF

    This version is easier to understand because PASS_MARK clearly explains the purpose of the value.

    Magic Numbers and Constants

    A magic number is a number used directly in code without explaining its meaning.

    Magic numbers can make code confusing. Constants help remove magic numbers by giving meaningful names to fixed values.

    Magic Number Named Constant
    total = price * 0.18 total = price * TAX_RATE
    IF marks >= 35 IF marks >= PASS_MARK
    IF attempts <= 3 IF attempts <= MAX_ATTEMPTS

    Constants Make Maintenance Easier

    If a fixed value is used many times in a program, constants make updates easier.

    Without Constant

    SET tax1 = price1 * 0.18
    SET tax2 = price2 * 0.18
    SET tax3 = price3 * 0.18

    If the tax rate changes, the programmer must update every place where 0.18 is used.

    With Constant

    CONSTANT TAX_RATE = 0.18
    
    SET tax1 = price1 * TAX_RATE
    SET tax2 = price2 * TAX_RATE
    SET tax3 = price3 * TAX_RATE

    Now the tax rate is defined in one place. If it changes, the programmer updates only the constant value.

    Constants Improve Safety

    Constants protect important values from accidental modification.

    For example, if MAX_LOGIN_ATTEMPTS is accidentally changed during program execution, the login security logic may behave incorrectly.

    CONSTANT MAX_LOGIN_ATTEMPTS = 3
    
    IF attempts > MAX_LOGIN_ATTEMPTS THEN
        DISPLAY "Account locked"
    END IF

    Using a constant makes it clear that MAX_LOGIN_ATTEMPTS is a fixed rule.

    Constants Can Store Different Data Types

    Constants are not limited to numbers. They can store different kinds of fixed values.

    CONSTANT PASS_MARK = 35
    CONSTANT PI = 3.14159
    CONSTANT COURSE_NAME = "Programming Mastery"
    CONSTANT IS_PUBLIC_COURSE = true
    CONSTANT DEFAULT_LANGUAGE = "English"
    Constant Value Possible Data Type
    PASS_MARK 35 Integer
    PI 3.14159 Decimal / Float
    COURSE_NAME "Programming Mastery" String / Text
    IS_PUBLIC_COURSE true Boolean

    Naming Constants

    Constants should have clear and meaningful names. Many programmers write constant names in uppercase letters to show that the value should not change.

    Poor Constant Name Better Constant Name
    x PASS_MARK
    n DAYS_IN_WEEK
    r TAX_RATE
    m MAX_LOGIN_ATTEMPTS
    Best Practice: Use meaningful constant names that clearly explain the fixed value.

    Types of Constants by Purpose

    Constants can be grouped based on how they are used in a program.

    1

    Mathematical Constants

    Fixed values used in formulas and calculations.

    Example: PI

    2

    Rule Constants

    Fixed values used as program rules.

    Example: PASS_MARK, MAX_ATTEMPTS

    3

    Configuration Constants

    Fixed values used for settings or configuration.

    Example: APP_NAME, DEFAULT_LANGUAGE

    4

    Limit Constants

    Fixed values that define limits or boundaries.

    Example: MAX_USERS, MIN_AGE

    Complete Example Using Constants

    The following language-neutral example shows constants used in a student result program.

    /*
    This program checks whether a student passed or failed.
    */
    
    CONSTANT PASS_MARK = 35
    CONSTANT SUBJECT_COUNT = 3
    
    ENTRY POINT
        INPUT studentName
        INPUT mathMarks
        INPUT scienceMarks
        INPUT englishMarks
    
        SET totalMarks = mathMarks + scienceMarks + englishMarks
        SET averageMarks = totalMarks / SUBJECT_COUNT
    
        IF averageMarks >= PASS_MARK THEN
            SET result = "Pass"
        ELSE
            SET result = "Fail"
        END IF
    
        DISPLAY studentName
        DISPLAY totalMarks
        DISPLAY averageMarks
        DISPLAY result
    END ENTRY POINT

    Constant Breakdown

    Constant Value Purpose
    PASS_MARK 35 Stores the minimum average mark required to pass.
    SUBJECT_COUNT 3 Stores the number of subjects used for average calculation.

    How Constants Help Debugging

    Constants help debugging because fixed values are named clearly and defined in one place.

    Debugging Questions

    • Is the constant value correct?
    • Is the constant name meaningful?
    • Is the program using a constant instead of repeated literal values?
    • Is the fixed rule defined only once?
    • Is the constant accidentally being treated like a variable?
    • Is the correct constant used in the formula or condition?
    • Does the constant value match the problem requirement?

    Best Practices for Constants

    Good constant usage makes programs cleaner, safer, and easier to update.

    Recommended Practices

    • Use constants for values that should not change.
    • Use meaningful constant names.
    • Avoid magic numbers in code.
    • Define constants in one place when possible.
    • Use constants for fixed rules, limits, rates, and configuration values.
    • Do not use constants for values that are expected to change frequently during execution.
    • Use a consistent naming style, such as uppercase names for constants.
    • Choose the correct data type for the constant value.
    • Use constants to improve readability and maintainability.
    • Review constants when program requirements change.

    Common Beginner Mistakes

    Mistakes

    • Using repeated numbers directly in code.
    • Using unclear constant names.
    • Trying to change a constant after defining it.
    • Using a variable when the value should be fixed.
    • Using constants for values that should actually change.
    • Defining the same constant in many places.
    • Not updating constant values when requirements change.
    • Using magic numbers instead of named constants.

    Better Habits

    • Create constants for fixed rules and fixed values.
    • Use descriptive names like PASS_MARK.
    • Keep constant values unchanged during execution.
    • Use variables for changing values.
    • Store repeated fixed values as constants.
    • Keep constants organized and easy to find.
    • Use constants to make formulas easier to understand.
    • Replace magic numbers with named constants.

    Prerequisites Before Learning Constants

    To understand constants properly, students should know a few basic programming concepts.

    Basic Prerequisites

    • What is data?
    • What is a data type?
    • Common data types.
    • Variables.
    • Identifiers and naming rules.
    • Statements and expressions.
    • Input, process, and output model.
    • Basic conditions and calculations.

    Practice Activity: Identify Constants

    This activity helps students identify constants and understand why they are useful.

    Task

    Read the following pseudocode and identify all constants, their values, and their purpose.
    CONSTANT TAX_RATE = 0.18
    CONSTANT DELIVERY_CHARGE = 50
    
    ENTRY POINT
        INPUT productPrice
    
        SET taxAmount = productPrice * TAX_RATE
        SET finalAmount = productPrice + taxAmount + DELIVERY_CHARGE
    
        DISPLAY finalAmount
    END ENTRY POINT

    Sample Answer

    Constant Value Purpose
    TAX_RATE 0.18 Stores the fixed tax rate used for tax calculation.
    DELIVERY_CHARGE 50 Stores the fixed delivery charge added to the bill.

    Mini Quiz

    1

    What is a constant?

    A constant is a fixed value that should not change during program execution.

    2

    Can the value of a constant change?

    No. A constant should keep the same value after it is defined.

    3

    Why are constants used?

    Constants are used to store fixed values, improve readability, prevent accidental changes, and make programs easier to maintain.

    4

    What is the difference between variable and constant?

    A variable can change its value, while a constant should remain fixed.

    5

    What is a magic number?

    A magic number is a number used directly in code without a meaningful name or explanation.

    Interview Questions on Constants

    1

    Define constant in programming.

    A constant is a named value that remains fixed and should not be changed during program execution.

    2

    Why should constants be used instead of repeated literal values?

    Constants make code easier to read, easier to update, and safer by avoiding repeated hard-coded values.

    3

    Give examples of constants.

    Examples of constants are PI, DAYS_IN_WEEK, PASS_MARK, TAX_RATE, and MAX_ATTEMPTS.

    4

    How do constants improve maintainability?

    If a fixed value is used many times, defining it as a constant allows the programmer to update it in one place.

    5

    When should you not use a constant?

    You should not use a constant when the value is expected to change during program execution.

    Quick Summary

    Concept Meaning
    Constant A fixed value that should not change during execution.
    Variable A value that can change during execution.
    Named Constant A constant represented using a meaningful name.
    Magic Number A direct number used in code without explanation.
    Maintainability Ease of updating and managing code.
    Fixed Rule A program rule that should remain unchanged.

    Final Takeaway

    Constants are fixed values that should not change while a program runs. They are useful for storing rules, limits, rates, and standard values. In the Programming Mastery Course, students should understand that constants make programs easier to read, safer from accidental changes, and easier to maintain. A good programmer uses variables for changing values and constants for fixed values.