CSC 201 Computer Programming
CSC 201 Computer Programming
COMPUTER PROGRAMMING
TABLE OF CONTENTS
2. Fundamentals of Programming
– Algorithm
– Flowchart
– Pseudocode
– Variables
- Data Types
- Keywords
- Operators
- Control Structures
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
Syntax
Syntax refers to the rules and structure of a programming language. If syntax rules are
violated, an error occurs.
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:
Corrected Version:
Dim name As String = “John”
Console.WriteLine(name)
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.
Corrected Version:
Dim total As Integer = 10
Dim average As Double = total / 3 ' Using Double to store decimal values
Console.WriteLine(average)
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.
1. Variables
2. Data Types
3. Keywords
4. Operators
5. Control Structures
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.
Example:
Dim age As Integer
Dim name As String
Dim price As Double
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.
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: &
Dim a As Integer = 10
Dim b As Integer = 5
Dim sum As Integer = a + b
Console.WriteLine(sum)
Output:
15
Output:
True
Output:
False
Example:
Console.WriteLine("Hello")
Console.WriteLine("Welcome to VB.NET")
Console.WriteLine("Goodbye")
These control structures allow a program to execute certain parts of the code
based on conditions.
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
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
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
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
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
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
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