What is "Short Circuiting"?

Created at : May/31/2007  
676 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)
URL (optional)
Example : http://www.yulianmf.com  
Comment

SFCV
Input code above below (Case Sensitif) :
About Me 
Rully Yulian MF
My Name is Rully Yulian Muhammad Firmansyah. I am an IT Trainer, IT Consultant and Application Developer spesializing in Microsoft .NET technology and SQL Server database. I live in Bandung, Indonesia. My hobby is to play Guitar. [Read More...]
Top Download 
Bagaimana caranya menginstal database ketika deploying sebuah aplikasi? : Downloaded 2235 times  
Change Group,Sort Order, Filtering By Date in Crystal Reports : Downloaded 2192 times  
Simple Voice Engine Application With Sound Player Class... : Downloaded 1523 times  
DataGridView Grouping : Downloaded 1243 times  
WinForms DataGrid Paging With SqlDataAdapter : Downloaded 1232 times  
Article Category 
Links 
Award 
Certifications 
MOS 2007
MCAS
MCT
MCPD
MCTS
MCAD.NET
ASP.NET Brainbench
Native Enterprise - IT Training 
Native Enterprise Facebook Group 
My Facebook 
My LinkedIn 
Syndication (RSS 2.0) 
Powered By 
Native Enterprise News