Cpprog1 - Chapter 4 PDF
Cpprog1 - Chapter 4 PDF
Objectives:
a.) Discuss the VB.Net operators.
b.) Differentiate the types of conditional statements that is being
used in VB.Net.
c.) Create a program that will test and verify the conditions in a
program.
Page | 1
MODULE INTRO TO VB.NET PROGRAMMING– CPPROG1
Arithmetic Operators
The Arithmetic Operators in VB.NET, used to perform mathematical operations such
as subtraction, addition, multiplication, division, etc. on the operands in VB.NET. These are as
follows:
Arithmetic Operators in VB.NET
Arithmetic_Operator.vb
1. Imports System
2. Module Arithmetic_Operator
3. Sub Main()
4. 'Declare a, b And c as integer Data Type()
5. Dim a, b, c As Integer
6. Dim d As Single
7. a = 17
8. b=4
Page | 2
MODULE INTRO TO VB.NET PROGRAMMING– CPPROG1
Page | 3
MODULE INTRO TO VB.NET PROGRAMMING– CPPROG1
Comparison Operators
As the name suggests, the Comparison Operator is used to compare the value of two variables
or operands for the various condition such as greater, less than or equal, etc. and returns a
Boolean value either true or false based on the condition.
<> It is a Non-Equality Operator that checks whether the (A <> B), check Non-
value of the two operands is not equal; it returns true; Equality
otherwise, it shows false.
> A greater than symbol or Operator is used to determine (A > B); if yes, TRUE,
whether the value of the left operand is greater than the Else FALSE
value of the right operand; If the condition is true, it
returns TRUE; otherwise, it shows FALSE value.
< It is a less than symbol which checks whether the value (A < B); if the
of the left operand is less than the value of the right condition is true,
operand; If the condition is true, it returns TRUE; returns TRUE else
otherwise, it shows FALSE value. FALSE
Is The Is Operator is used to validate whether the two result = obj1 Is obj2
objects reference the same variable or object; If the test
is true, it returns True; otherwise, the result is False. In
short, it checks the equality of the objects. An Is Operator
is also used to determine whether the object refers to a
valid object.
Page | 4
MODULE INTRO TO VB.NET PROGRAMMING– CPPROG1
IsNot The IsNot Operator is similar to Is Operator, except that Result = obj1 IsNot
the two object references the different object; if yes, the obj2
result is True; otherwise, the result is False.
Like The Like Operator is used to check the pattern expression result = string Like
of string variable; And if the pattern matched, the result the pattern, the
is True; otherwise, it returns False. pattern represents
the series of
characters used by
Like Operator.
Example of Comparison Operators in VB.NET
Comparison_Operator.vb
1. Imports System
2. Module Comparison_Operator
3. Sub Main()
4. 'declaration of Integer, Object and String Data Type variables
5. Dim x As Integer = 5
6. Dim y As Integer = 10
7. Dim Result, obj, obj2 As Object
8. Dim str, str2 As String
9. str = "Apple12345"
10. str2 = "Apple12345"
11. obj = 10
12. obj2 = 20
13.
14. Console.WriteLine(" Program of Comparison Operator")
15. 'Use of > Operator
16. Console.WriteLine(" Output of x > y is {0}", x > y)
17.
18. 'Use of < Operator
19. Console.WriteLine(" Output of x < y is {0}", x < y)
20.
21. 'Use of = Operator
22. Console.WriteLine(" Output of x = y is {0}", x = y)
23.
24. 'Use of <> Operator
25. Console.WriteLine(" Output of x <> y is {0}", x <> y)
26.
27. 'Use of >= Operator
Page | 5
MODULE INTRO TO VB.NET PROGRAMMING– CPPROG1
Page | 6
MODULE INTRO TO VB.NET PROGRAMMING– CPPROG1
And The And Operator represents, whether both the operands are true; (A And B),
the result is True. result =
False
Not The Not Operator is used to reverse the logical condition. For Not A
example, if the operand's logic is True, it reveres the condition and Or
makes it False. Not(A And
B) is True
Page | 7
MODULE INTRO TO VB.NET PROGRAMMING– CPPROG1
7. c = 10
8. d = 20
9.
10. 'Use of And Operator
11. If A And B Then
12. Console.WriteLine(" Operands A And B are True")
13. End If
14.
15. 'Use of Or Operator
16. If A Or B Then
17. Console.WriteLine(" Operands A Or B are True")
18. End If
19.
20. 'Use of Xor Operator
21. If A Xor B Then
22. Console.WriteLine(" Operands A Xor B is True")
23. End If
24.
25. 'Use of And Operator
26. If c And d Then
27. Console.WriteLine(" Operands c And d is True")
28. End If
29.
30. 'Use of Or Operator
31. If c Or d Then
32. Console.WriteLine(" Operands c Or d is True")
33. End If
34.
35. 'Use of AndAlso Operator
36. If A AndAlso B Then
37. Console.WriteLine(" Operand A AndAlso B is True")
38. End If
39.
40. 'Use of OrElse Operator
41. If A OrElse B Then
42. Console.WriteLine(" Operand A OrElse B is True")
43. End If
44.
45. 'Use of Not Operator
46. If Not (A And B) Then
47. Console.WriteLine(" Output of Not (A And B) is True")
Page | 8
MODULE INTRO TO VB.NET PROGRAMMING– CPPROG1
48. End If
49.
50. Console.WriteLine(" Press any key to exit?")
51. Console.ReadKey()
52. End Sub
53. End Module
Now compile and execute the above code by pressing the F5 button or Start button in Visual
studio, it returns the following output:
Operator Description
AND The Binary AND Operator are used to copy the common binary bit in the result if
the bit exists in both operands.
OR The Binary OR Operator is used to copy a common binary bit in the result if the
bit found in either operand.
XOR The Binary XOR Operator in VB.NET, used to determine whether a bit is available
to copy in one operand instead of both.
Not The binary NOT Operator is also known as the binary Ones' Compliment
Operator, which is used to flip binary bits. This means it converts the bits from 0
to 1 or 1 to 0 binary bits.
<< The Binary Left Shift Operator is used to shift the bit to the left side.
>> The Binary Right Shift Operator is used to shift the bit to the right side.
Page | 9
MODULE INTRO TO VB.NET PROGRAMMING– CPPROG1
Assignment Operators
The Assignment Operators are used to assign the value to variables in VB.NET.
Assignment Operators in VB.NET
Page | 11
MODULE INTRO TO VB.NET PROGRAMMING– CPPROG1
&= It is a concatenate string assignment Operator used to Str &= name, which is
bind the right-hand string or variable with the left-hand same as Str = Str &
string or variable. And then, the result will be assigned name
to the left operand.
Example of Assignment Operator in VB.NET:
Assign_Operator.vb
1. Imports System
2. Module Assign_Operator
3. Sub Main()
4. 'Declare variable and b As Integer
5. Dim A As Integer = 5
6. Dim B As Integer
7. Dim Str, name As String
8. name = "come"
9. Str = "Wel"
10.
11. 'Use of = Operator
12. B=A
13. Console.WriteLine(" Assign value A to B is {0}", B)
14.
15. 'Use of += Operator
Page | 12
MODULE INTRO TO VB.NET PROGRAMMING– CPPROG1
16. B += A
17. Console.WriteLine(" Output of B += A is {0}", B)
18.
19. 'Use of -= Operator
20. B -= A
21. Console.WriteLine(" Output of B -= A is {0}", B)
22.
23. 'Use of *= Operator
24. B *= A
25. Console.WriteLine(" Output of B *= A is {0}", B)
26.
27. 'Use of /= Operator
28. B /= A
29. Console.WriteLine(" Output of B /= A is {0}", B)
30.
31. 'Use of = Operator
32. B \= A
33. Console.WriteLine(" Output of B \= A is {0}", B)
34.
35. 'Use of ^= Operator
36. B ^= A
37. Console.WriteLine(" Output of B ^= A is {0}", B)
38.
39. 'Use of &= Operator
40. Str &= name
41. Console.WriteLine(" Output of Str &= name is {0}", Str)
42.
43. Console.WriteLine(" Press any key to exit...")
44. Console.ReadKey()
45. End Sub
46. End Module
Now compile and execute the above code by pressing the F5 button or Start button in Visual
studio, it returns the following output:
Page | 13
MODULE INTRO TO VB.NET PROGRAMMING– CPPROG1
Concatenation Operators
In VB.NET, there are two concatenation Operators to bind the operands:
& It is an ampersand symbol that is used to bind two or more operand Result =
together. Furthermore, a nonstring operand can also be Wel &
concatenated with a string variable ( but in that case, Option Strict is come,
on). Result =
Welcome
MyProgram.vb
1. Imports System
2. Module MyProgram
3. Sub Main()
4. Dim str As String = "Wel"
5. Dim str2 As String = "come"
6. Dim str3 As String = " "
7. Dim str4 As String = "to JavatPoint"
8. Dim result As String
9. Dim result2 As String
10. result = str & str2
11. Console.WriteLine(" Result = str & str2 gives = {0}", result)
12. result2 = str + str2 + str3 + str4
13. Console.WriteLine(" Result = str + str2 + str3 +str4 gives = {0}", result2.ToString)
14. Console.ReadLine()
15. End Sub
16. End Module
Now compile and execute the above code by pressing the F5 button or Start button in Visual
studio, it returns the following output:
Page | 14
MODULE INTRO TO VB.NET PROGRAMMING– CPPROG1
Miscellaneous Operators
Page | 15
MODULE INTRO TO VB.NET PROGRAMMING– CPPROG1
Operator precedence is used to determine the order in which different Operators in a complex
expression are evaluated. There are distinct levels of precedence, and an Operator may belong
Page | 16
MODULE INTRO TO VB.NET PROGRAMMING– CPPROG1
to one of the levels. The Operators at a higher level of precedence are evaluated first. Operators
of similar precedents are evaluated at either the left-to-right or the right-to-left level.
The Following table shows the operations, Operators and their precedence -
Await Highest
Exponential ^
Integer division \
All comparison Operators =, <>, <, <=, >, >=, Is, IsNot, Like,
TypeOf …is
Negation Not
Page | 17
MODULE INTRO TO VB.NET PROGRAMMING– CPPROG1
7. Dim r As Integer = 10
8. Dim s As Integer = 5
9. Dim result As Integer
10.
11. Console.WriteLine("Check Operator Precedence in VB.NET")
12. 'Check Operator Precedence
13. result = (p + q) * r / s ' 45 * 10 / 5
14. Console.WriteLine("Output of (p + q) * r / s is : {0}", result)
15.
16. result = (p + q) * (r / s) ' (45) * (10/5)
17. Console.WriteLine("Output of (p + q) * (r / s) is : {0}", result)
18.
19. result = ((p + q) * r) / s ' (45 * 10 ) / 5
20. Console.WriteLine("Output of ((p + q) * r) / s is : {0}", result)
21.
22. result = p + (q * r) / s ' 30 + (150/5)
23. Console.WriteLine("Output of p + (q * r) / s is : {0}", result)
24.
25. result = ((p + q * r) / s) ' ((30 + 150) /5)
26. Console.WriteLine("Output of ((p + q * r) / s) is : {0}", result)
27.
28. Console.WriteLine(" Press any key to exit...")
29. Console.ReadKey()
30. End Sub
31. End Module
Now compile and execute the above code by pressing the F5 button or Start button in Visual
studio, it returns the following output:
For more knowledge about operators, please check the link provided;
https://www.youtube.com/watch?v=5-
zYdoUBYf8&list=PLsJBMeqEdtggJi2khGAjgnQ3ssgzWw_uz&index=4
Page | 18
MODULE INTRO TO VB.NET PROGRAMMING– CPPROG1
The above diagram shows that if the defined condition is true, statement_1 will be executed, and
if the condition is false, statement_2 will be executed.
VB.NET provides the following conditional or decision-making statements.
o If-Then Statement
o If-Then Else Statement
o If-Then ElseIf Statement
o Select Case Statement
o Nested Select Case Statements
Page | 19
MODULE INTRO TO VB.NET PROGRAMMING– CPPROG1
If-Then Statement
The If-Then Statement is a control statement that defines one or more conditions, and if the
particular condition is satisfied, it executes a piece of information or statements.
Syntax:
1. If condition Then
1. Module Module1
2. ' Declaration of variable str
3. Dim str As String = "JavaTpoint"
4. Sub Main()
5. ' if str equal to "JavaTpoint", below Statement will be executed.
6. If str = "JavaTpoint" Then
7. Console.WriteLine("Welcome to the JavaTpoint")
8. End If
9. Console.WritLine("press any key to exit?")
10. Console.ReadKey()
11. End Sub
12. End Module
Now compile and execute the above program by clicking on the Start or F5 button, it shows the
following output:
As we can see in the above example, if the value of str is equal to JavaTpoint, the condition
is true, and it prints the Statement.
Page | 20
MODULE INTRO TO VB.NET PROGRAMMING– CPPROG1
Example 2: Write a program to print a number is greater than another number in VB.NET.
if_statment2.vb
1. Module if_statement2
2. Sub Main()
3. ?Definition of variables
4. Dim no1, no2 As Integer
5. Console.WriteLine("Enter any two number:")
6. no1 = Console.ReadLine() ?read no1 from user
7. no2 = Console.ReadLine() ?read no2 from user
8. If no1 > no2 Then
9. Console.WriteLine("First number is greater than second number")
10. End If
11. If no1 < no2 Then
12. Console.WriteLine("Second number is greater than First number")
13. End If
14. Console.WriteLine("press any key to exit...")
15. Console.ReadKey()
16. End Sub
17. End Module
Now compile and execute the above program by clicking on the Start or F5 button, it shows the
following output:
In the above program, we enter two numbers to find the greater number using the relational
operator. And if the first number is greater than the other, the first statement is executed;
otherwise, the second statement will be executed.
If-Then-Else Statement
The If-Then Statement can execute single or multiple statements when the condition is true, but
when the expression evaluates to false, it does nothing. So, here comes the If-Then-
Else Statement. The IF-Then-Else Statement is telling what If condition to do when if the
statement is false, it executes the Else statement. Following is the If-Then-Else statement syntax
in VB.NET as follows:
Page | 21
MODULE INTRO TO VB.NET PROGRAMMING– CPPROG1
Syntax:
1. If (Boolean_expression) Then
2. 'This statement will execute if the Boolean condition is true
3. Else
4. 'Optional statement will execute if the Boolean condition is false
5. End If
Flow chart
The above diagram represents that if the Boolean expression (condition) is true, the if statement
will execute, and if the Boolean expression is false, Else code or statement will be executed. After
that, the control transfer to the next statement, which is immediately after the If-Then-Else
control statement.
Example 1: Write a program to check whether the number is even or odd.
If_Else_statment.vb
1. Module If_Else_statement
2. Sub Main()
3. Dim num As Integer
4. Console.WriteLine("Enter the Number")
5. num = Console.ReadLine() 'read data from console
6.
7. If (num Mod 2 = 0) Then ' if condition is true, print the if statement
8. Console.WriteLine("It is an even number")
9.
10. Else 'otherwise, Else statement is executed.
Page | 22
MODULE INTRO TO VB.NET PROGRAMMING– CPPROG1
Example 2: Write a program to print the larger and smaller of the two numbers.
if_else_statment2.vb
1. Module if_else_statement2
2. Sub Main()
3. Dim a As Integer
4. Dim b As Integer
5. Console.WriteLine("Enter the first number : ")
6. a = Console.ReadLine()
7.
8. Console.WriteLine("Enter the second number : ")
9. b = Console.ReadLine()
10.
11. If a > b Then
12. Console.WriteLine(" larger number = {0} and smaller number = {1} ", a, b)
13. Else
14. Console.WriteLine(" larger number = {0} and smaller number = {1} ", b, a)
15. End If
16.
17. Console.WriteLine("press any key to exit...")
18. Console.ReadKey()
19. End Sub
20. End Module
Page | 23
MODULE INTRO TO VB.NET PROGRAMMING– CPPROG1
Now compile and execute the above program by clicking on the Start or F5 button, it shows the
following output:
9. End If
Flowchart
The following diagram represents the functioning of the If-Else-If Statement in the VB.NET
programming language.
Page | 24
MODULE INTRO TO VB.NET PROGRAMMING– CPPROG1
If this condition is true in the flowchart of the if-else-if statement, the statement is executed
within the if block. If the condition is not true, it passes control to the next ElseIf condition to
check whether the condition is matched. And if none of the conditions are matched, the else
block is executed.
Example 1: Write a program to show the uses of If... ElseIf statements.
if_elseIf.vb
1. Module if_elseIf
2. Sub Main()
3. Dim var1 As Integer
4.
5. Console.WriteLine(" Input the value of var1: ")
6. var1 = Console.ReadLine()
7. If var1 = 20 Then
8. 'if condition is true then print the following statement'
9. Console.WriteLine(" Entered value is equal to 20")
10. ElseIf var1 < 50 Then
11. Console.WriteLine(" Entered value is less than 50")
12.
13. ElseIf var1 >= 100 Then
14. Console.WriteLine(" Entered value is greater than 100")
15. Else
16. 'if none of the above condition is satisfied, print the following statement
17. Console.WriteLine(" Value is not matched with above condition")
Page | 25
MODULE INTRO TO VB.NET PROGRAMMING– CPPROG1
18. End If
19. Console.WriteLine(" You have entered : {0}", var1)
20. Console.WriteLine(" press any key to exit...")
21. Console.ReadKey()
22. End Sub
23. End Module
Now compile and execute the above program by clicking on the Start or F5 button, it shows the
following output:
Example 2: Write a program to use the If-Then-ElseIf Statement for calculating the division
obtained by the student. Also, take the marks obtained by the student in 5 different subjects
from the keyboard.
if_elseIf2.vb
1. Module If_elseIf2
2. Sub Main() ' execution start from Main() method
3. Dim m1, m2, m3, m4, m5, per As Integer
4. Console.WriteLine("Enter marks in five subjects ")
5. ' Read the marks of five subject
6. m1 = Console.ReadLine()
7. m2 = Console.ReadLine()
8. m3 = Console.ReadLine()
9. m4 = Console.ReadLine()
10. m5 = Console.ReadLine()
11. per = (m1 + m2 + m3 + m4 + m5) / 5
12. If (per >= 70) Then
13. 'if condition is true, print the first division
14. Console.WriteLine(" First division")
15. ElseIf (per >= 60) Then
16. 'if ElseIf condition is true, print the second division
17. Console.WriteLine(" Second division")
18. ElseIf (per >= 50) Then
19. 'if ElseIf condition is true, print the third division
Page | 26
MODULE INTRO TO VB.NET PROGRAMMING– CPPROG1
For more knowledge about IF Then statement, please check the link provided;
https://www.youtube.com/watch?v=MRnlg_V_0BI&list=PLC601DEA22187BBF1&ind
ex=13
Page | 27
MODULE INTRO TO VB.NET PROGRAMMING– CPPROG1
Using the select case statement in VB.NET programming, you can replace the uses of multiple If-
Then-Else If statement from the program for better readability and easy to use.
Syntax
Following is the syntax of the Select Case statement in VB.NET, as follows:
1. Select Case [variable or expression]
2. Case value1 'defines the item or value that you want to match.
3. // Define a statement to execute
4.
5. Case value2 'defines the item or value that you want to match.
6. // Define a statement to execute
7.
8. Case Else
9. // Define the default statement if none of the conditions is true.
10. End Select
Furthermore, you can also set more than one condition in a single case statement, such as:
6. Statement2
7.
8. Case Else
9. // define the default statement if none of the condition is true
10. End Select
Page | 28
MODULE INTRO TO VB.NET PROGRAMMING– CPPROG1
In Flowchart, the Select Case statement represents the evaluating of the process start from top
to bottom. If the expression or value is matched with the first select case, statement -1 is
executed else the control transfer to the next case for checking whether the expression is
matching or not. Similarly, it checks all Select case statements for evaluating. If none of the cases
are matched, the Else block statement will be executed, and finally, the Select Case Statement
will come to an end.
Example 1: Write a program to display the Days name using the select case statement in VB.NET.
Select_case.vb
1. Imports System
2. Module Select_case
3. Sub Main()
4. 'define a local variable.
5. Dim Days As String
6. Days = "Thurs"
7. Select Case Days
8. Case "Mon"
9. Console.WriteLine(" Today is Monday")
10. Case "Tue"
11. Console.WriteLine(" Today is Tuesday")
Page | 29
MODULE INTRO TO VB.NET PROGRAMMING– CPPROG1
In the select case statement, the value of Days "Thurs" will compare all the available select cases'
values in a program. If a value matched with any condition, it prints the particular statement, and
if the value is not matched with any select case statement, it prints the default message.
Example 2: Write a program to perform an arithmetic operation using the Select case statement
in VB.NET.
Operation.vb
Operation.vb
1. Imports System
2. Module Operation
3. Sub main()
Page | 30
MODULE INTRO TO VB.NET PROGRAMMING– CPPROG1
Page | 31
MODULE INTRO TO VB.NET PROGRAMMING– CPPROG1
In the above example, we defined Select with multiple case statements, and if the user-
defined input is matched with any defined case statement, it executes that statement. And if the
condition is not matched with any case, it executes a default statement in VB.NET.
Here, we provide 'M' as input, which checks all case statements, and if any case is matched with
M, it executes the statement within the respective Case statement.
For more knowledge about select case statement, please check the link provided;
https://www.youtube.com/watch?v=3wvvD4Vfr4k&list=PLC601DEA22187BBF1&inde
x=24
REFERENCES
https://www.javatpoint.com/vb-net-operators
https://www.javatpoint.com/vb-net-control-statements
Page | 32