0% found this document useful (0 votes)
7 views

CIT-0112-PRACTICAL

Uploaded by

hezronabahati143
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 views

CIT-0112-PRACTICAL

Uploaded by

hezronabahati143
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
You are on page 1/ 5

1.

Data Types Example


Explanation:
2. Identifiers Example
Explanation:
3. Variables Example
Explanation:
4. Constants Example
Explanation:
5. Expressions Example
Explanation:
6. Comparison and Logical Operators Example
Explanation:
7. Data Type Conversions Example
Explanation:
8. Using Variables and Constants Together
Explanation:
Summary of Key Concepts:

1. Data Types Example


In this example, we demonstrate different data types and how to use them.

Dim studentName As String = "John Doe" ' String (text)


Dim studentAge As Integer = 20 ' Integer (whole number)
Dim gpa As Double = 3.75 ' Double (decimal number)
Dim isEnrolled As Boolean = True ' Boolean (true or false)
Dim birthDate As Date = #1998-05-23# ' Date (date and time)

Explanation:

studentName is a String, used to hold text (names in this case).


studentAge is an Integer, representing whole numbers.
gpa is a Double, used to store decimal values like GPA.
isEnrolled is a Boolean, representing true/false conditions.
birthDate is a Date, storing the student’s date of birth.
2. Identifiers Example
Identifiers are the names you give to variables or functions. Here we use meaningful
names for a banking application.

Dim accountHolderName As String = "Alice Johnson"


Dim accountBalance As Double = 1000.75
Dim isAccountActive As Boolean = True

Explanation:

accountHolderName, accountBalance, and isAccountActive are identifiers


that describe their purpose in the program. Meaningful identifiers make the code
easier to understand.

3. Variables Example
Variables are storage locations for data that can change during the program’s
execution.

Dim score As Integer = 0


score = 10 ' The value of the score changes from 0 to 10

Explanation:

Initially, the variable score is set to 0, but it changes later to 10. Variables are
useful when you need to keep track of changing data, like a player’s score in a
game.

4. Constants Example
Constants store values that won’t change during the program’s execution.
Const PI As Double = 3.14159
Const maxAttempts As Integer = 5

Explanation:

PI holds the value of π (3.14159) and maxAttempts holds the maximum number
of login attempts allowed. These values remain fixed, and trying to change them
later in the program would cause an error.

5. Expressions Example
Expressions involve combining variables, constants, and operators to perform
calculations or logical operations.

Dim pricePerItem As Double = 50.0


Dim quantity As Integer = 3
Dim totalPrice As Double = pricePerItem * quantity

Explanation:

This expression calculates the total price by multiplying the pricePerItem by the
quantity. The result is stored in totalPrice.

6. Comparison and Logical Operators


Example
Comparison and logical operators are used to compare values and make decisions.

Dim userAge As Integer = 18


Dim isEligibleToVote As Boolean = (userAge >= 18) ' Comparison operator

Dim hasID As Boolean = True


Dim canVote As Boolean = isEligibleToVote And hasID ' Logical operator (And)
Explanation:

The first part checks if the userAge is greater than or equal to 18. If true, the user
is eligible to vote.
The second part checks if the user is eligible to vote and has an ID. The result is
stored in canVote. The logical And operator ensures that both conditions must be
true for the user to be able to vote.

7. Data Type Conversions Example


Converting from one data type to another, such as converting a string to an integer.

Dim ageText As String = "25"


Dim age As Integer = Convert.ToInt32(ageText)

Explanation:

Here, ageText is a String containing the value "25". We use Convert.ToInt32 to


convert this string into an Integer and store it in age. This is important when the
input is in text format (from a form, for example), but you need to treat it as a
number for calculations.

8. Using Variables and Constants Together


In this example, we demonstrate how variables and constants can be used together in
expressions.

Const discountRate As Double = 0.1


Dim originalPrice As Double = 200.0
Dim finalPrice As Double = originalPrice - (originalPrice * discountRate)

Explanation:

discountRate is a constant representing a 10% discount.


originalPrice holds the starting price of an item.
finalPrice is calculated by applying the discount (10% off) to the original price.

Summary of Key Concepts:


1. Data Types: Define the kind of data stored (text, numbers, etc.). Different types
like Integer, Double, and String help organize and process data.
2. Identifiers: These are names for variables and constants that make your code
clear. Good identifiers, like studentName, help describe what each piece of data
represents.
3. Variables: Used to store data that can change, like score or accountBalance.
Variables allow your program to store and update information as needed.
4. Constants: Store fixed values that don’t change, like PI. They’re useful for data
like tax rates or mathematical constants that remain the same throughout the
program.
5. Expressions: Combine values and operators to perform calculations or
comparisons. Examples include finding a total price or checking if someone is
eligible to vote.
6. Conversions: Converting between data types ensures the right type of data is
used in the right place, like turning text into a number for mathematical operations.

You might also like