Eliminating the If Statement Wherever Possible

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:
XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes

ransomware cyber attack

Why Is Ransomware Such a Major Threat?

One of the most significant cyber threats faced by modern organizations is a ransomware attack. Ransomware attacks have grown in both sophistication and frequency over the past few years, forcing

data dictionary

Tools You Need to Make a Data Dictionary

Data dictionaries are crucial for organizations of all sizes that deal with large amounts of data. they are centralized repositories of all the data in organizations, including metadata such as