What is "Short Circuiting"?

Posted at : May/31/2007
8292 Views | 0 Comments

Have you ever seen "AndAlso" and "OrElse" within VB code? Yup...if you ever, that's what call "Short Circuiting". What's the difference with "And" and "Or"? Its difference from the conditional execution process. Ok...let us see the simple example code below :

   1:          Dim strItem As String = "Mie Ayam Jamur"
   2:          Dim intQuantity As Integer = 5
   3:   
   4:          If (strItem = "Mie Ayam Jamur") And (intQuantity = 5) Then
   5:              Console.WriteLine("Bonus satu mangkuk lagi")
   6:          End If

If the first condition (strItem) meet the condition (true) then the compiler also check the second condition (intQuantity) whether its result is true or false, so the output will be display the message if the all condition meet the criteria. What the compiler do if we write this code :

   1:          If (strItem = "Mie Ayam Jamur") AndAlso (intQuantity = 5) Then
   2:              Console.WriteLine("Bonus satu mangkuk lagi")
   3:          End If

The compiler wont check the second condition if the first condition is not meet the criteria (false). It give us a little bit better performance if we use complex procedure to set the condition we want to check.

What about the "Or" and "OrElse"? Ok..let's see the code :

   1:          strItem = "Baso Tahu"
   2:          intQuantity = 3
   3:   
   4:          If (strItem = "Baso Tahu") Or (intQuantity = 3) Then
   5:              Console.WriteLine("Discount 5%")
   6:          End If

In the code above the compiler will check both condition to see the result. As we already know "Or" statement will be give us the true result if one of the condition has true value. But what obout this code?

   1:          If (strItem = "Baso Tahu") OrElse (intQuantity = 3) Then
   2:              Console.WriteLine("Discount 5%")
   3:          End If

If we use "OrElse" statement the compiler wont check the next condition if the first condition has true result, but if the first has false value the compiler will continue to check whether its result is true or false. So it will also give us a little bit better performance if we have to check the condition which have complex procedure...

 

[Comments]


[Write your comment]

Name (required)

Email (required-will not published)

Comment
UXET
Input code above below (Case Sensitive) :

ABOUT ME

Rully Yulian MF
Rully Yulian Muhammad Firmansyah | Founder & IT Trainer Native Enterprise | MCT (2008-2019) | MVP (2009-2016) | Xamarin Certified Professional | MTA | MCAD | MCPD | MOS | Bandung, West Java, Indonesia.

[Read More...]

TOP DOWNLOAD

Mapping Hak Akses User Pada MenuStrip Sampai Control Button
downloaded 6983 times

Bagaimana caranya menginstal database ketika deploying sebuah aplikasi?
downloaded 4893 times

Simple Voice Engine Application With Sound Player Class...
downloaded 4045 times

Change Group,Sort Order, Filtering By Date in Crystal Reports
downloaded 3460 times

WinForms DataGrid Paging With SqlDataAdapter
downloaded 2881 times


LINKS

CERTIFICATIONS

Xamarin Certified
MOS 2007
MCT
MCPD
MCTS
MCAD.NET
ASP.NET Brainbench

NATIVE ENTERPRISE

Native Enterprise - IT Training

FOLLOW ME

Youtube  Facebook  Instagram  LinkedIn   Twitter

RSS


NATIVE ENTERPRISE NEWS

© Copyright 2006 - 2024   Rully Yulian MF   All rights reserved.