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

CSC 201 Computer Programming

The document provides a comprehensive overview of computer programming, focusing on fundamentals such as algorithms, flowcharts, and pseudocode, as well as programming syntax and semantics. It introduces Visual Basic .NET, covering its basic structure, components, and control structures, including variables, data types, and operators. Additionally, it explains how to compile and run programs in VB.NET, along with examples of common programming errors and their corrections.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

CSC 201 Computer Programming

The document provides a comprehensive overview of computer programming, focusing on fundamentals such as algorithms, flowcharts, and pseudocode, as well as programming syntax and semantics. It introduces Visual Basic .NET, covering its basic structure, components, and control structures, including variables, data types, and operators. Additionally, it explains how to compile and run programs in VB.NET, along with examples of common programming errors and their corrections.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

CSC 201

COMPUTER PROGRAMMING

TABLE OF CONTENTS

1. 1. Introduction to Computer Programming

2. Fundamentals of Programming

– Algorithm
– Flowchart
– Pseudocode

Programming Syntax and Semantic


– Syntax
– Semantic
– Types of Computer Programming Errors

Components of Computer Programming

2. Introduction to Visual Basic .NET

– Basic Program Structure


– How to Compile & Run the Program
– Types of Interfaces in Visual Basic .NET

3. 7. Components of Visual Basic .NET

– Variables

- Data Types

- Keywords

- Operators
- Control Structures

- Sequential Control Structures

- Selection Control Structures (If, Else, Select Case)

- Iteration Control Structures (For, While, Do While, Do Until)

1. Introduction to Computer Programming

Computer Programming
Computer programming is the process of designing, writing, testing, and maintaining
instructions (code) that a computer can execute. It involves using programming
languages such as Python, C++, Java, and Visual Basic .NET to create software
applications.

2. Fundamentals of Programming

Algorithm
An algorithm is a step-by-step set of instructions for solving a problem. It must be
clear, efficient, and finite.

Example:
A simple algorithm for adding two numbers:

Step 1 Start
Step 2 Input two numbers A and B
Step 3 Compute Sum = A + B
Step 4 Display Sum
Step 5 Stop

Flowchart
A flowchart is a diagrammatic representation of an algorithm, using standard
symbols such as:
Oval (Start/Stop)
Parallelogram (Input/Output)
Rectangle (Process)
Diamond (Decision-making)
Example: A flowchart for the addition of two numbers follows the same steps as the
algorithm but represented graphically.

Pseudocode
Pseudocode is a text-based representation of an algorithm that is structured like
code but does not use actual programming syntax.

Example:

BEGIN
INPUT A, B
SUM = A + B
OUTPUT SUM
END

3. Programming Syntax and Semantic

Syntax
Syntax refers to the rules and structure of a programming language. If syntax rules are
violated, an error occurs.

Example: In Visual Basic.net, missing a colon in an if statement is a syntax error:

If x == 5
Console.WriteLine(“Hello”) # Missing colon

Semantic
Semantics refers to the meaning of code. A program with correct syntax but incorrect
semantics may still not work as intended.

Example:

X = “5” + 5 # This causes an error (string + integer mismatch)

Types of Computer Programming Errors


Syntax Error
A syntax error occurs when the code violates the grammatical rules of a
programming language. It prevents the program from being compiled or
executed.
Causes of Syntax Errors:
• Incorrect spelling of keywords
• Missing punctuation (e.g., semicolon, parentheses, brackets, quotes)
• Incorrect case sensitivity (some languages are case-sensitive)
• Wrong function or variable declaration

Example of a Syntax Error in VB.NET:


Dim name As String = “John ‘ Missing closing quote
Console.WriteLine(name)

Corrected Version:
Dim name As String = “John”
Console.WriteLine(name)

Fixing Syntax Errors:


• Check for typos or missing characters.
• Use an IDE or compiler to identify syntax errors.

2. Semantic Error
A semantic error occurs when the code is grammatically correct but produces
incorrect or unintended results. It means the logic of the program is wrong.

Causes of Semantic Errors:


• Wrong variable assignment
• Incorrect use of operators
• Misuse of conditions and loops

Example of a Semantic Error in VB.NET:


Dim total As Integer = 10
Dim average As Integer = total / 3 ' This will cause unintended results
Console.WriteLine(average) ' Expected decimal value, but stored as Integer

Corrected Version:
Dim total As Integer = 10
Dim average As Double = total / 3 ' Using Double to store decimal values
Console.WriteLine(average)

Fixing Semantic Errors:


• Debug the program step by step.
• Use print statements (Console.WriteLine()) to track variable values.
• Use code reviews to detect logical mistakes.
4. Components of Computer Programming
The core components of a programming language include:
Variables – Store data values.
Data Types – Define types of values (e.g., integers, strings).
Operators – Perform computations.
Control Structures – Define logic flow (e.g., loops, if-statements).

5. Introduction to Visual Basic .NET

Introduction to Visual Basic .NET

Visual Basic .NET (VB.NET) is an object-oriented programming language developed by


Microsoft, commonly used for Windows applications.

VB.Net – Basic Program Structure


A VB.Net program consists of the following key parts:
• Namespace declaration (e.g., Imports System)
• A class or module (e.g., Module Module1)
• One or more procedures (e.g., Sub Main())
• Variables (to store data)
• The Main procedure (program entry point)
• Statements & expressions (logic of the program)
• Comments (non-executable notes for clarity)

Example: “Hello World” Program

Imports System

Module Module1
‘ This program displays Hello World
Sub Main()
Console.WriteLine(“Hello World”)
Console.ReadKey()
End Sub
End Module
Explanation of the Code
Imports System: Includes the System namespace, which provides built-in
functionalities.
Module Module1: Defines a module (like a container for code).
Sub Main(): The main procedure where execution begins.
Console.WriteLine(“Hello World”): Prints “Hello World” on the screen.
Console.ReadKey(): Waits for user input before closing the window.

How to Compile & Run the Program


Using Visual Studio:
1. Open Visual Studio and create a Console Application.
2. Write the code in the editor.
3. Press F5 or click Run to execute.

Using Command Line:


1. Save the code as helloworld.vb.
2. Open Command Prompt and navigate to the file location.
3. Compile using: vbc helloworld.vb
4. Run the program: helloworld
5. Output: “Hello World” appears on the screen.

Types of Interfaces in Visual Basic .NET (VB.NET)


In VB.NET, an interface defines a contract that a class or structure must follow. It
specifies method signatures but does not provide an implementation. VB.NET supports
different types of interfaces that can be categorized into:
1. Graphical User Interface (GUI) Interfaces
2. Programming Interfaces (Code-Based Interfaces)

Components of Visual Basic .NET (VB.NET)

1. Variables

2. Data Types

3. Keywords

4. Operators

5. Control Structures

6. Functions and Procedures


7. Forms and GUI Components

1) Variables
A variable is a named storage location in a program that holds data. It allows the
programmer to store, retrieve, and manipulate values during the execution of the
program.

Rules for Naming Variables in VB.NET:


• Must begin with a letter and can include numbers and underscores (e.g.,
student_Age)
• Cannot use reserved keywords (e.g., Dim, If, For)
• Should be meaningful to improve code readability

Declaring Variables in VB.NET:


VB.NET uses the Dim statement to declare variables, specifying their name and data
type.

Example:
Dim age As Integer
Dim name As String
Dim price As Double

Assigning Values to Variables:


Variables can be assigned values during or after declaration.

Example:
Dim age As Integer = 25
Dim name As String = “John”
Dim price As Double
Price = 45.99

2) Data Types
Data types define the type of value a variable can store.

Classification of Data Types in VB.NET:


Data types in VB.NET are divided into:
1. Numeric Data Types
2. Non-Numeric Data Types
3. Composite Data Types
Examples of Data Types in VB.NET

1. Numeric Data Types (Store numbers)

Integer → Dim age As Integer = 25

Double → Dim price As Double = 99.99

Single → Dim temperature As Single = 36.5

Decimal → Dim salary As Decimal = 2500.75

Byte → Dim smallNumber As Byte = 255

Long → Dim distance As Long = 1000000000

2. Non-Numeric Data Types (Store text, characters, and Boolean values)

String → Dim name As String = "John Doe"

Char → Dim grade As Char = "A"

Boolean → Dim isPassed As Boolean = True

3. Composite Data Types (Store multiple values)

Array → Dim numbers() As Integer = {1, 2, 3, 4, 5}

3) Keywords
Keywords are reserved words in a programming language that have a predefined
meaning and cannot be used as variable names, function names, or identifiers. They
form the basic syntax and structure of a programming language. VB.NET has specific
keywords used for declaring variables, controlling program flow, defining data types,
handling errors, and more.

4) Operators
Operators are symbols or keywords used in programming to perform mathematical,
logical, and comparison operations on variables and values. There are four
categories of operators which are;
Arithmetic Operators: +, -, *, /, Mod
Comparison Operators: =, <>, <, >, <=, >=
Logical Operators: And, Or, Not
Concatenation Operators: &

Examples of Operators in VB.NET


1. Arithmetic Operators Example

Dim a As Integer = 10
Dim b As Integer = 5
Dim sum As Integer = a + b
Console.WriteLine(sum)

Output:
15

2. Comparison Operators Example


Dim x As Integer = 10
Dim y As Integer = 20
Dim result As Boolean = (x < y)
Console.WriteLine(result)

Output:
True

3. Logical Operators Example


Dim isAdult As Boolean = True
Dim hasID As Boolean = False
Dim canEnter As Boolean = isAdult And hasID
Console.WriteLine(canEnter)

Output:
False

4. Concatenation Operator Example


Dim firstName As String = “John”
Dim lastName As String = “Doe”
Dim fullName As String = firstName & “ “ & lastName
Console.WriteLine(fullName)
Output:
“John Doe”

5) Control Structures in Visual Basic .NET


Control structures direct the flow of execution in a program, determining which
statements execute and under what conditions.

Types of Control Structures


Control structures in VB.NET are classified into three main types:

1. Sequential Control Structures


These control structures that execute statements line by line from top to
bottom.No decision-making or repetition involved.

Example:
Console.WriteLine("Hello")
Console.WriteLine("Welcome to VB.NET")
Console.WriteLine("Goodbye")

The above statements execute sequentially without skipping any line.

2. Selection Control Structures (Decision Making)

These control structures allow a program to execute certain parts of the code
based on conditions.

(a) If...Then Statement

Executes a block of code if the condition is true.

Syntax:
If condition Then
' Code to execute if condition is true
End If
Example:
Dim age As Integer = 18
If age >= 18 Then
Console.WriteLine("You are eligible to vote.")
End If
(b) If...Then...Else Statement

Executes one block of code if the condition is true, and another block if the
condition is false.

Syntax:
If condition Then
' Code if condition is true
Else
' Code if condition is false
End If
Example:
Dim num As Integer = 10
If num Mod 2 = 0 Then
Console.WriteLine("Even number")
Else
Console.WriteLine("Odd number")
End If

(c) If...ElseIf...Else Statement

Checks multiple conditions and executes the corresponding block.

Syntax:
If condition1 Then
' Code if condition1 is true
ElseIf condition2 Then
' Code if condition2 is true
Else
' Code if none of the conditions are true
End If
Example:
Dim score As Integer = 85
If score >= 90 Then
Console.WriteLine("Grade: A")
ElseIf score >= 80 Then
Console.WriteLine("Grade: B")
ElseIf score >= 70 Then
Console.WriteLine("Grade: C")
Else
Console.WriteLine("Grade: F")
End If
(d) Select Case Statement

Used instead of multiple If...ElseIf statements for checking the value of a


variable.

Syntax:
Select Case variable
Case value1
' Code for value1
Case value2
' Code for value2
Case Else
' Code if no match is found
End Select
Example:
Dim day As Integer = 3
Select Case day
Case 1
Console.WriteLine("Monday")
Case 2
Console.WriteLine("Tuesday")
Case 3
Console.WriteLine("Wednesday")
Case Else
Console.WriteLine("Invalid Day")
End Select

3. Iteration Control Structures (Loops)

Looping structures repeat a block of code multiple times until a condition is


met.

(a) For...Next Loop

Executes a block of code a specific number of times.

Syntax:
For counter = start To end Step increment
' Code to execute
Next
Example:
For i = 1 To 5
Console.WriteLine("Iteration: " & i)
Next

Output:

Iteration: 1
Iteration: 2
Iteration: 3
Iteration: 4
Iteration: 5

(b) While...End While Loop

Executes a block of code while the condition is true.

Syntax:
While condition
' Code to execute
End While
Example:
Dim count As Integer = 1
While count <= 5
Console.WriteLine("Count: " & count)
count += 1
End While

(c) Do While Loop

Executes a block of code at least once, then continues looping while the
condition is true.

Syntax:
Do While condition
' Code to execute
Loop
Example:
Dim x As Integer = 1
Do While x <= 5
Console.WriteLine("Number: " & x)
x += 1
Loop

(d) Do Until Loop

Executes a block of code until the condition becomes true.

Syntax:
Do Until condition
' Code to execute
Loop
Example:
Dim x As Integer = 1
Do Until x > 5
Console.WriteLine("Number: " & x)
x += 1
Loop

You might also like