Nested If and Single line if statement

By: Issac  

Nested If statement

Option Strict On

Imports System

Module Module1

Sub Main( )

Dim temp As Integer = 8

If temp <= 8 Then

If temp = 8 Then

Console.WriteLine("temp = 8")

Else

Console.WriteLine("Temp: {0}", temp)

End If

End If

End Sub

End Module

Nested if statement helps to check multiple number of conditions here in this example we take a variable temp and assign the value of 8 to it now in the first if statement we check whether temp is less than or equal to 8(which is true) so it will get into the next if statement which checks whether temp is equal to 8(which is again true) and prints temp =8.

Single line if statement

public class Test

public Shared Sub Main

Dim Score As Integer

Score = 23

'Single line If statement.

If Score < 50 Then Console.WriteLine("Failed.") : Console.WriteLine("Please try again.")

'Multi-line If statement.

If Score < 50 Then

Console.WriteLine("Failed.")

Console.WriteLine("Please try again.")

End If

End Sub

End class

In this example we have shown both multi-line and single line if statements, single line if statements though may look complex if you have large number of conditions is effective and helpful to reduce the number of program lines.




Archived Comments


Most Viewed Articles (in VB.net )

Latest Articles (in VB.net)

Comment on this tutorial