Eliminating the If Statement Wherever Possible

If you have to assign True or False to a variable after testinga certain condition, then the If statement may be done away withaltogether.

For example, if you have something like this:

 If (age < 13 and sex = "M") or (age > _        14 and sex = "F") then FetchRecord _        = true

it can be replaced by:

 FetchRecord = (age < 13 and sex = "M") _        or (age > 14 and sex = "F")

This performs the same function as the If statement above. TheFetchRecord variable is correctly assigned True or False. Theadvantage is that it allows the splitting condition-part of theIf statement (see example below). Further, if you like replacingyour If statements by IIf, then you know that IIf requires youto add msainfx.dll to your projects in VB3. The above versiondoesn’t.

If the condition-part of if statement is very lengthy, then theabove technique allows you to split it. Thus, if you have somethinglike this in VB3:

 If (Age > 25 and Category = "M1") or _        (Age > 35 and Category <> "C1") or _        (Age > 45 and Category = "M7") or _        (Age > 45 and Category = "P1") thenProcessRecordEndif

you can do this:

 Dim Result as integerResult = (Age > 25 and Category = "M") _        or (Age > 35 and Category <> "C1")Result = Result or (Age > 45 and _        Category = "M7") or (Age > 45 and _        Category = "P1")if Result then ProcessRecord

The advantage is that it allows limited splitting of lines inVB3, thus making the code more readable.

By the way, the technique:

 x = (y op z) 

to assign True or False to x works in C, Pascal and FoxPro aswell. Remember to use = for assignment in Pascal and == for testingin C.

Share the Post:
Share on facebook
Share on twitter
Share on linkedin


The Latest

homes in the real estate industry

Exploring the Latest Tech Trends Impacting the Real Estate Industry

The real estate industry is changing thanks to the newest technological advancements. These new developments — from blockchain and AI to virtual reality and 3D printing — are poised to change how we buy and sell homes. Real estate brokers, buyers, sellers, wholesale real estate professionals, fix and flippers, and beyond may

man on floor with data

DevX Quick Guide to Data Ingestion

One of the biggest trends of the 21st century is the massive surge in internet usage. With major innovations such as smart technology, social media, and online shopping sites, the internet has become an essential part of everyday life for a large portion of the population. Due to this internet

payment via phone

7 Ways Technology Has Changed Traditional Payments

In today’s digital world, technology has changed how we make payments. From contactless cards to mobile wallets, it’s now easier to pay for goods and services without carrying cash or using a checkbook. This article will look at seven of the most significant ways technology has transformed traditional payment methods.