G.E.S J.H.S Programming

Variable Declaration & Constants

Variables in programming are named storage locations that hold data which can be manipulated during the execution of a program.

Variables are used to store and manage data in a program. They allow programmers to assign values, perform calculations, and manipulate data throughout the program's execution.

In order to fully understand variables, you must first learn how the computer memory works.

Computer memory is organized into small units called bytes, where each byte can store a specific amount of data. The size of a variable determines how many bytes it occupies in memory.

When a variable is declared, the computer allocates a specific amount of memory to store the variable's value. The variable is then associated with a name, which can be used to access and manipulate the data stored in that memory location.

Variables can hold different types of data, such as numbers, characters, or strings.

In programming, variables are essential for storing and manipulating data, allowing programmers to create dynamic and interactive programs.

Before the memory allocation, the programmer must indicate the type of data the variable will hold.

This is done by specifying the variable's data type during its declaration.

The data type determines the kind of value a variable can store and the operations that can be performed on it.

It also indicates the amount of memory that will be allocated for the variable.

Each programming language has its own set of data types that can be used to declare variables.

Programming languages and their data types

In C programming, some common data types include:

In Python, some common data types include:

In Java, some common data types include:

In JavaScript, some common data types include:

To reserve memory for a variable, you must declare it with a specific data type and name.

The syntax for declaring most variables is as follows:

data_type variable_name;

For example, in C programming, you can declare an integer variable named "age" as follows:

int age;

In Python, you can declare a variable and assign a value to it without specifying the data type:

age = 25

In Java, you can declare a variable and assign a value to it as follows:

int age = 25;

In JavaScript, you can declare a variable using the "var", "let", or "const" keyword:

let age = 25;

As you can see, in some programming languages, you need to specify the data type when declaring a variable, while in others, the data type is inferred automatically.

Choosing the Right Data Type

When declaring a variable, it is important to choose the appropriate data type based on the kind of data you want to store and the operations you intend to perform on that data.

For example, if you want to store whole numbers, you would choose an integer data type.

If you want to store decimal numbers, you would choose a floating-point data type.

If you want to store text, you would choose a string data type.

Examples of integer data types include:

  1. int: Used to store integer values (whole numbers).
  2. long: Used to store long integer values (larger than int).

Examples of floating-point data types include:

  1. float: Used to store single-precision floating-point numbers.
  2. double: Used to store double-precision floating-point numbers (more precise than float).

Examples of string data types include:

  1. char: Used to store single characters.
  2. String: Used to store sequences of characters (text).

Choosing the right data type is crucial for efficient memory usage and optimal performance in your programs.

Possible information for integer data types

  1. Age
  2. Year of Birth
  3. Number of Students
  4. Number of Apples
  5. Number of Cars

The above information mostly relates to integers and hence the integer data type is appropriate.

Possible information for floating-point data types

  1. Height
  2. Weight
  3. Temperature
  4. Price of an Item
  5. Distance between Two Points

The above information mostly relates to decimal numbers and hence the floating-point data type is appropriate.

When building an application and the information could be both integers and decimals, the floating-point data type is appropriate since every whole number can be represented as a floating-point number (e.g., 5 can be represented as 5.0) and the floating-point data type provides the necessary precision for decimal values.

Possible information for string data types

  1. Name
  2. Address
  3. Email
  4. Phone Number
  5. City of Residence

The above information mostly relates to text data and hence the string data type is appropriate.

Possible information for char data types

  1. First Initial
  2. Gender
  3. Grade Level

Note: char data types are used to store single characters such as 'A', 'a','B','b', 'C', 'c', etc.

Rules for giving variable names

When naming variables, it is important to follow certain rules to ensure your code is readable and maintainable:

Rules for Variable Names

  1. Variable names must start with a letter (a-z, A-Z) or an underscore (_).
  2. Variable names cannot contain spaces or special characters (except underscores).
  3. Variable names are case-sensitive.
  4. Variable names should be descriptive and meaningful.
  5. Variable names should not be reserved keywords (e.g., int, float, string, class) in the programming language.

Examples of valid variable names include:

  1. age
  2. yearOfBirth
  3. number_of_students
  4. height
  5. name
  6. first_initial

Examples of invalid variable names include:

  1. 1age (starts with a number)
  2. year of birth (contains spaces)
  3. number-of-students (contains special characters)
  4. height! (contains special characters)
  5. name@ (contains special characters)
  6. first initial (contains spaces)

Naming Conventions

There are several naming conventions that are commonly used in programming:

  1. Snake Case: Words are separated by underscores (e.g., first_name, number_of_students)
  2. Camel Case: The first word is lowercase, and each subsequent word starts with an uppercase letter (e.g., firstName, numberOfStudents)
  3. Pascal Case: Each word starts with an uppercase letter (e.g., FirstName, NumberOfStudents)

When to declare variables

Variables should be declared when you need to store data that will be used in your program. It is good practice to declare variables as close to their first use as possible.

Declaring variables at the beginning of a program or function can help improve readability and organization, but it is also important to avoid declaring unnecessary variables that may consume memory and reduce performance.

Only declare variables when you need to store information such as data collected from the user or calculations performed in your program.

If you can simply perform a calculation without storing the result in a variable, it is better to do so to save memory and improve performance.

Since we will be using python as the programming language for this course, we will be declaring variables accordingly.

Assuming you are a programmer in an institution and you have been contacted to build a management system for students in the institution, what are the data you will collect as entries and their data types?

The following could be the possible entries and their data types:

  1. Student ID (Integer or string if it contains non-numeric characters)
  2. First Name (String)
  3. Last Name (String)
  4. Age (Integer)
  5. Grade Level (Integer)
  6. Address (String)
  7. Email (String)
  8. Phone Number (String)

To save such information on the memory, you have to declare variables for each of these entries.

The above data could be declared in python as follows:

  1. student_id = 12345
  2. first_name = "John"
  3. last_name = "Doe"
  4. age = 20
  5. grade_level = 12
  6. address = "123 Main Street"
  7. email = "john.doe@example.com"
  8. phone_number = "555-1234"

Explanation to the above declarations

  1. Tells the computer to give a memory location the name student_id and assign 12345 to it.
  2. Tells the computer to give a memory location the name first_name and assign "John" to it.
  3. Tells the computer to give a memory location the name last_name and assign "Doe" to it.
  4. Tells the computer to give a memory location the name age and assign 20 to it.
  5. Tells the computer to give a memory location the name grade_level and assign 12 to it.
  6. Tells the computer to give a memory location the name address and assign "123 Main Street" to it.
  7. Tells the computer to give a memory location the name email and assign "john.doe@example.com" to it.
  8. Tells the computer to give a memory location the name phone_number and assign "555-1234" to it.

Why phone number is string

Phone numbers are stored as strings because they may contain special characters such as plus signs (country codes), dashes, parentheses, or spaces. Storing them as strings ensures that these characters are preserved and the phone number is displayed correctly.

Additionally, phone numbers may have leading zeros, which can be lost if stored as integers. Storing them as strings allows for accurate representation and formatting of phone numbers.

The possible data for the staff may include:

  1. Staff ID (Integer or string if it contains non-numeric characters)
  2. First Name (String)
  3. Last Name (String)
  4. Age (Integer)
  5. Department (String)
  6. Salary (Float)

Explanation to the above declarations

  1. Tells the computer to give a memory location the name staff_id and assign 56789 to it.
  2. Tells the computer to give a memory location the name first_name and assign "Jane" to it.
  3. Tells the computer to give a memory location the name last_name and assign "Smith" to it.
  4. Tells the computer to give a memory location the name age and assign 30 to it.
  5. Tells the computer to give a memory location the name department and assign "Engineering" to it.
  6. Tells the computer to give a memory location the name salary and assign 75000.0 to it.

The values assigned are retrieved using their variable names.

For instance, if we wish to print the stored value of the staff_id variable, we would use the print() function with the variable name as an argument.

In Python, you can print the value of the staff_id variable as follows:

print(staff_id)

This will output: 56789 since 56789 is the value assigned to the variable, staff_id (the memory location).

Importance of variables

Variables are essential in programming as they allow you to store and manipulate data throughout your program. They provide a way to reference data by name, making your code more readable and maintainable. Variables also enable you to perform calculations and make decisions based on the data they hold.

Without variables, you would have to hard-code values directly into your program, which would make it inflexible and difficult to update or modify. Variables allow you to create dynamic programs that can adapt to different inputs and conditions.

Constants

Constants are similar to variables but their values cannot be changed once they are assigned. They are used to store fixed values that should not be modified throughout the program.

Examples of data that can be stored as constants include mathematical constants like π (pi) or e (Euler's number), or configuration values that remain fixed during the execution of the program.

In C programming language, you can define a constant using the #define CONSTANT_NAME VALUE

#define PI 3.14159

#define EULER_NUMBER 2.71828

#define GRAVITY 9.81

In Python, you can define a constant by convention using uppercase letters:

  1. PI = 3.14159
  2. EULER_NUMBER = 2.71828
  3. GRAVITY = 9.81

Difference between Variables and Constants

The main difference between variables and constants is that variables can have their values changed during the execution of a program, while constants cannot be modified once they are assigned.

Variables are used to store data that may need to be updated or changed, while constants are used to store fixed values that should not be altered.

Using constants can help improve the readability and maintainability of your code by providing meaningful names for fixed values and preventing accidental modification of important data.

Note: when a constant value is changed, it mostly produces syntax errors or unexpected behavior.