devxlogo

Compact Your Code Using IIF and Switch

Compact Your Code Using IIF and Switch

You can often replace an If…Then…Else block with a more compact IIf function:

 ' returns the max of two valuesmaxValue = IIf(first >= second, _	first, second)

Switch is a rarely used function, yet in many cases it proves rather useful as a substitute for a lengthy If…ElseIf block:

 ' is "x" negative, positive or null?Print Switch(x  0, _	"positive", True, "Null")

Note the last test is True, because the three conditions are mutually exclusive and exhaustive.

devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist