Visual Basic Code Snippets

1K Blog Marathon: Day 9

Hi folks!

Today I’m not feeling well,🤒😷🤧🤮💊💉 and I’m sorry for my blog post today. I am very ill, and at the moment my head is hurting. But I don’t want to skip a day in my 1K Blogging Marathon, so here is one of my treasured article that I wrote last year. This topic revolves around my first programming language, Visual Basic.

IF block without the need of END IF closing

Did you know that you can evaluate an IF condition with only one-line and without the need for END IF? This is an IF block (we also have IF-END IF, IF-ELSE-END IF, IF-ELSEIF- END IF, and IF-ELSEIF-ELSE-END IF)

Here is the format: IF <condition> then <action>

Ex.

strColor = “blue”
strResponse = “”
IF strColor = “red” then strResponse = “The color is red!”

‘This will set strResponse to “” (empty) since the condition is FALSE.

Ternary Operator

A ternary operator is an inline IF block that evaluates a condition then return either of the two output separated by comma in ONE SINGLE LINE!

Format: IF(<condition>,<value if TRUE>, <value if FALSE>)

Ex.

strAnimal = “dog”
strResponse = IF (strAnimal=”dog”,”The animal is a dog!”,”The animal is not a dog.”)

‘This will set strResponse to “The animal is dog!”, since the condition (IF strAnimal=“dog”) is TRUE

This is very useful if you only have 2 exact action from a True or False condition. It would not only save a line of code, it will also make your prof or superior yell “Nayswan!”

DIVISION Hacks!

When dealing with numbers, the division process gives us different output depending on the symbol we use. You are familiar with the SLASH “/” symbol. This is the very basic division symbol used. But did you know that there are 2 other symbol that you can use aside from this so-called forward slash? I’ll list it down for you.

1. Forward Slash (“/”) – the basic division symbol. This will give you a quotient with a whole number and the decimal places (if applicable)

Ex.

intQuotient = 100/3

This will give you 33.3333333……

2. Back Slash (“\”) – this process outputs the Whole Number of the quotient – that is, the decimal places (if applicable) is removed.

Ex.

intQuotient = 100\3

This will give you 33

3. Modulo (“Mod”) – this is the magic word when you want to get the remainder of the dividend and divisor. This is helpful when you are coding Roman Numerals or Currency Denomination (Programming Competition’s favorite problem!)

Ex.

intRemainder = 100 mod 3

This will give you 1

So, next time you deal with Divison, you have 3 options to chose from: getting the RAW, the ROUNDED OFF, or the REMAINING! ^_^

“And that’s one blog, stay hungry!”

“Any fool can write code that a computer can understand. Good programmers write code that humans can understand.”

― Martin Fowler

Published by Christian Foster

Code-blooded, coffee-lover, tall, dark and chubby. I love to draw, has motion-sickness and a sleepy-head. BTW, graduate of BS Computer Science, Associate in Computer Science and certified UiPath RPA Developer. Loyal to my partner and a father of a cute bouncing baby daughter!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Create your website at WordPress.com
Get started
%d bloggers like this: