Programming Fundamentals PDF
Programming Fundamentals PDF
2
Objectives
3
Agenda
Fundamentals
Operators
Flow Controls
4
VB.NET Source File Structure
Declaration order
1. Imports statement '
Used to reference namespace. ' * Created on Feb 22, 2006
When class names are to be referred in the ' * First VB.NET Program
imports directive, aliases for the classes can '
be used.
Imports System
using alias-name = namespace.class-name
Imports A = System.Console
6
VB.NET Source File Structure (Continued)
'
Class ' * Created on Feb 22, 2006
' * First VB.NET Program
Every VB.NET program '
includes at least one class
definition. The class is the Imports System
fundamental component of all Imports A = System.Console
VB.NET programs. Class is a
keyword Namespace VBNETSchool
Public Class VBNetOne
VBNetOne is a VB.NET
''' <summary>
identifier that specifies the
''' </summary>
name of the class to be
''' <param name="args"></param>
defined
Public Shared Sub Main(ByVal args As String())
' print a message
A class definition contains all A.WriteLine("Welcome to VB.NET!")
the variables and methods End Sub
that make the program work. End Class
This is contained in the class
body indicated by the opening End Namespace
and closing braces.
7
VB.NET Source File Structure (Continued)
'
' * Created on Feb 22, 2006
Main() method ' * First VB.NET Program
'
This line begins the Main() Imports System
method. This is the line at which Imports A = System.Console
the program will begin executing.
Namespace VBNETSchool
Public Class VBNetOne
''' <summary>
''' </summary>
''' <param name="args"></param>
Public Shared Sub Main(ByVal args As String())
ByVal args As ' print a message
String() A.WriteLine("Welcome to VB.NET!")
Declares a parameter named End Sub
args, which is an array of string. End Class
It represents command-line
arguments. End Namespace
8
VB.NET Source File Structure (Continued)
'
' * Created on Feb 22, 2006
' * First VB.NET Program
VB.NET
'
statement
A complete unit of work in a Imports System
VB.NET program. Imports A = System.Console
A statement is always
terminated with a line break. Namespace VBNETSchool
Except when the _ character is Public Class VBNetOne
used. ''' <summary>
''' </summary>
Console.WriteLine(); ''' <param name="args"></param>
Public Shared Sub Main(ByVal args As String())
' print a message
This line outputs the string A.WriteLine _
Welcome to VB.NET! followed ("Welcome to VB.NET!")
by a new line on the screen. End Sub
End Class
End Namespace
9
VB.NET Keywords
Keywords are an essential part of language definition as they implement
specific features of the language.
10
VB.NET Keywords
11
Identifiers
12
Literals
Literals are the way in which the values that are stored in variables are
represented.
VB.NET Literals
13
Variable and Data Types
14
Data Types
Predefined value types are also known as simple types (or primitive types).
SimpleTypes
Integral Decimal
Floating Point
Types Types
Types
Signed Unsigned
Types Types
16
Data Types
sbyte byte
short ushort
int uint
long ulong
17
Value Data Types
float double
18
Value Data Types (Continued)
Decimal Type
Is a high precision 128-byte data type that is designed for use in financial and
monetary calculations.
It can store values in the range 1.0 10e28 to 7.9 10e28.
To specify a number to be decimal type, append the character M (or m) to the
value e.g 123.45M.
Boolean Types
Are declared using the keyword, bool.
They have two values: true or false. In languages, such as C and C++, boolean
conditions can be satisfied where 0 means false and anything else means true.
In VB.NET the only values that satisfy a boolean condition is true and false,
which are official keywords.
Character Types
Are declared using the keyword char.
char type assumes a size of two bytes but can hold only a single character.
It is designed to hold a 16bit Unicode character.
19
Value Data Types (Continued)
s1.Name = John
S1.RollNumber = 0200789
20
Value Data Types (continued)
Enumerations
Are a user-defined integer type which provides a way to attach names to
numbers.
Help increase comprehensibility of the code.
The enum keyword is used to define an enumeration.
A list of words in an enum is automatically assigned values of 0,1,2, etc.
The default value of the first enum member is set to 0 while each subsequent
member is incremented by one
Can be changed by assigning specific values to the members.
21
Reference Data Types
22
Variables
Default Values
Variables are either explicitly assigned a value or automatically assigned a
default value.
23
Constant Variables
Const m As Integer = 10
Const age As Integer = m * 5
Is illegal
Dim m As Itneger= 10
Const age as Integer = m * 5
24
Variable Declaration & Initialization
25
Value Type Declaration
Identifier type
name age 07
1
initialization/assignment
age = 17
Identifier value
name stack
26
Reference Type Declaration
27
Scope of Variable
Member Variables ..
Declared inside the class but Public Class TestClass
outside of all methods.
Accessible by all methods of Dim x as Integer =0
the class. Public Sub TestSub
Dim n As Integer =5 Ok
Block2
Dim x As Integer =0 Ok
Local Variables .
Available only within the End Sub
Block1
method where they were
declared. Method parameters Public Sub Sub2
have local scope. n = n+1 wrong, n not available here
Block3
Dim m As Integer = 20 ok
x = m ok
End Sub
End Class
28
Boxing and UnBoxing
29
Agenda
Fundamentals
Operators
Flow Controls
30
Operators and Assignments
Unary operators
Arithmetic operators
String operators
Relational operators
Conditional operators
Logical operators
Assignment operators
Shift operators
Bitwise operators
Primitive Casting
31
Unary Operators
- Negative sign
32
Arithmetic Operators
- Subtract
* Multiply
/ Divide
33
String Operators
Sample code:
Sample output:
My full name is: Ford, Henry D.
You can call me Henry!
I'm 21 years old.
34
Relational Operators
= Equals
35
Relational Operators
Sample code:
Dim name1 As String = "Marlon"
Dim weight1 As Integer = 140, height1 As Integer = 74
Dim name2 As String = "Katie"
Dim weight2 As Integer = 124, height2 As Integer = 78
Dim isLight As Boolean = weight1 < weight2, isLightEq As Boolean = weight1 <= weight2
System.Console.WriteLine("Is " + name1 + " lighter than " + name2 + "? " + isLight)
System.Console.WriteLine("Is " + name1 + " lighter or same weight as " + name2 + "? " +
isLightEq)
Dim isTall As Boolean = height1 > height2, isTallEq As Boolean = height1 >= height2
System.Console.WriteLine("Is " + name1 + " taller than " + name2 + "? " + isTall)
System.Console.WriteLine("Is " + name1 + " taller or same height as " + name2 + "? " +
isTallEq)
Dim isWeighEq As Boolean = weight1 = weight2, isTallNotEq As Boolean = height1 <> height2
System.Console.WriteLine("Is " + name1 + " same weight as " + name2 + "? " + isWeighEq)
System.Console.WriteLine("Is " + name1 + " not as tall as " + name2 + "? " + isTallNotEq)
System.Console.WriteLine("So who is heavier?")
System.Console.WriteLine("And who is taller?") Is Marlon lighter than Katie? false
37
Logical Operators
40
Casting (Type Conversion)
41
Summary of Operators
42
Agenda
Fundamentals
Operators
Flow Controls
43
Flow Controls
If-Else statement
Select statement
While statement
Do-While statement
For statement
For Each statement
Exit statement
Continue statement
44
If-Else
45
Select
Syntax:
Select performs Select Case exp
statements based on Case val
'Do something
multiple conditions. Case val
exp can be char byte 'do something
Case Else
short int, val should be 'If no match it will pass here
a unique constant of exp. End Select
Output:
I'm a male.
46
While
Output: Timer: 10
Timer: 9
Timer: 8
Timer: 7
Timer: 6
Timer: 5
Timer: 4
Timer: 3
Timer: 2
Timer: 1
47
Do- Loop While
Output: Timer: 0
Timer: 1
Timer: 2
Timer: 3
Timer: 4
Timer: 5
Timer: 6
Timer: 7
Timer: 8
Timer: 9
48
For Next
Example:
For age As Integer = 18 To 29
System.Console.WriteLine("Enjoy life while you're
" + age)
Next
Output:
Enjoy life while you're 18
Enjoy life while you're 19
Enjoy life while you're 20
Enjoy life while you're 21
Enjoy life while you're 22
Enjoy life while you're 23
Enjoy life while you're 24
Enjoy life while you're 25
Enjoy life while you're 26
Enjoy life while you're 27
Enjoy life while you're 28
Enjoy life while you're 29
49
For Each
50
Exit
51
Continue
52
return
The return branching statement is used to exit from the current method.
There are two forms: Example 1:
return <value>; Public Function sum(ByVal x As Integer, ByVal y As
return; Integer) As Integer
Return x + y
End Function
Example 2:
Public Function sum(ByVal x As Integer, ByVal y As
Integer) As Integer
x=x+y
If x < 100 Then
Return x
Else
Return x + 5
End If
End Function
Public Sub getSum(ByVal x As Integer)
System.Console.WriteLine(x)
Return
53
End Sub
Key Points
54
Key Points (Continued)
55
Questions and Comments
56