0% found this document useful (0 votes)
8 views5 pages

CIT-0112-PRACTICAL-2

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)
8 views5 pages

CIT-0112-PRACTICAL-2

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

Example 1: IF Statement (Simple Decision)

Example 2: IF-ELSEIF-ELSE Statement (Grading System)


Example 3: IF Statement with Logical Operators
Example 4: CASE Statement (Menu Selection)
Example 5: CASE Statement (Grading System)
Example 6: CASE Statement (Day of the Week)
These practical examples illustrate how IF and CASE statements work in real-
world scenarios.

Here are simple and practical examples of how you can use IF statements and CASE
statements in VB.NET, based on the explanations provided.

Example 1: IF Statement (Simple Decision)


Let’s say you’re writing a program that checks if a student has passed or failed based
on their score.

Dim studentScore As Integer = 75

If studentScore >= 50 Then


Console.WriteLine("The student has passed.")
Else
Console.WriteLine("The student has failed.")
End If

Explanation:

This program checks if studentScore is greater than or equal to 50.


If the condition is true, it prints "The student has passed."
If the condition is false (i.e., the score is less than 50), it prints "The student has
failed."

Example 2: IF-ELSEIF-ELSE Statement


(Grading System)
This example adds more conditions to assign letter grades to students based on their
scores.
Dim studentScore As Integer = 85

If studentScore >= 90 Then


Console.WriteLine("Grade: A")
ElseIf studentScore >= 80 Then
Console.WriteLine("Grade: B")
ElseIf studentScore >= 70 Then
Console.WriteLine("Grade: C")
ElseIf studentScore >= 60 Then
Console.WriteLine("Grade: D")
Else
Console.WriteLine("Grade: F")
End If

Explanation:

The program checks the score and assigns a grade.


If the score is 90 or higher, the student gets an A.
The conditions continue checking in descending order, with the lowest scores
getting an F.

Example 3: IF Statement with Logical


Operators
This example demonstrates using logical operators (And, Or) to check multiple
conditions. It checks if a user is logged in and has admin privileges.

Dim isLoggedIn As Boolean = True


Dim isAdmin As Boolean = False

If isLoggedIn And isAdmin Then


Console.WriteLine("Welcome, Admin!")
ElseIf isLoggedIn Then
Console.WriteLine("Welcome, User!")
Else
Console.WriteLine("Please log in.")
End If

Explanation:

This checks if the user is logged in and if they are an admin.


If both conditions are true, it welcomes the admin.
If only the login condition is true, it welcomes the regular user.
If neither is true, it asks the user to log in.

Example 4: CASE Statement (Menu Selection)


Here’s an example where a Select Case structure is used to handle different options
in a simple menu.

Dim choice As Integer = 2

Select Case choice


Case 1
Console.WriteLine("You selected option 1: View account balance.")
Case 2
Console.WriteLine("You selected option 2: Transfer funds.")
Case 3
Console.WriteLine("You selected option 3: Change password.")
Case Else
Console.WriteLine("Invalid selection. Please try again.")
End Select

Explanation:

The program checks the choice variable.


Based on the value, it prints the corresponding message for each option.
If the user enters something other than 1, 2, or 3, the program defaults to printing
"Invalid selection."

Example 5: CASE Statement (Grading System)


Here’s another way to implement a grading system using a CASE statement.

Dim grade As Char = "B"

Select Case grade


Case "A"
Console.WriteLine("Excellent work!")
Case "B"
Console.WriteLine("Good job!")
Case "C"
Console.WriteLine("You passed.")
Case "D"
Console.WriteLine("Needs improvement.")
Case Else
Console.WriteLine("Failed.")
End Select

Explanation:

This program checks the grade variable and prints a message based on the
value.
Each grade from A to D has a corresponding message.
If the grade is anything else (e.g., F or another value), the program prints "Failed."

Example 6: CASE Statement (Day of the


Week)
This example demonstrates using a CASE statement to print the day of the week based
on a numeric input.

Dim dayNumber As Integer = 4

Select Case dayNumber


Case 1
Console.WriteLine("Today is Monday.")
Case 2
Console.WriteLine("Today is Tuesday.")
Case 3
Console.WriteLine("Today is Wednesday.")
Case 4
Console.WriteLine("Today is Thursday.")
Case 5
Console.WriteLine("Today is Friday.")
Case 6
Console.WriteLine("Today is Saturday.")
Case 7
Console.WriteLine("Today is Sunday.")
Case Else
Console.WriteLine("Invalid day number.")
End Select

Explanation:

The program checks the dayNumber variable, which represents the day of the
week.
Based on the number, it prints the corresponding day.
If the number is outside the range of 1 to 7, it prints "Invalid day number."
These practical examples illustrate how IF
and CASE statements work in real-world
scenarios.
IF statements are great for situations with fewer conditions or where complex
logic is required.
CASE statements are perfect for handling multiple discrete conditions with simple
outcomes.

Both control structures are key to creating dynamic and responsive programs.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy