What is "Short Circuiting"?

Posted at : May/31/2007
8467 Views

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...

 

ABOUT ME

Rully Yulian MF
Rully Yulian Muhammad Firmansyah | Co-Founder & IT Trainer at Native Enterprise | Microsoft Azure Data Scientist | IBM RAG & Agentic AI | IBM Data Science & Data Analyst | Python Certified (PCEP, PCAP) | MOS, MTA, Xamarin Certified, ex MCT | ex MVP

CERTIFICATIONS

Microsoft Certified Associate
IBM RAG and Agentic AI Professional
IBM Data Science Professional IBM Data Analyst Professional
PCAP Associate Python Programmer Certified PCEP Entry Level Python Programmer Certified
Xamarin Certified
MOS 2007
MCPD MCTS
MCAD.NET

NATIVE ENTERPRISE

Native Enterprise - IT Training

FOLLOW ME

Youtube  X Twitter Facebook  Instagram  LinkedIn

RSS


NATIVE ENTERPRISE NEWS

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