ArticleslgStudy

science

IIf

IIf is a science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand IIf rather than just read about it. In short: In computing, IIf (an abbreviation for Immediate if) is a function in several editions of the Visual Basic programming language and ColdFusion Markup Language (CFML), and on spreadsheets that returns the second or third parameter based on the evaluation of the first parameter. It is an example of a conditional expression, which is similar to a conditional statement.

Key takeaways

  • IIf belongs to science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect IIf to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of IIf from memory before moving on to harder problems.

Reference excerpt

In computing, IIf (an abbreviation for Immediate if) is a function in several editions of the Visual Basic programming language and ColdFusion Markup Language (CFML), and on spreadsheets that returns the second or third parameter based on the evaluation of the first parameter. It is an example of a conditional expression, which is similar to a conditional statement.

Syntax The syntax of the IIf function is as follows:

IIf(expr, truepart, falsepart)

All three parameters are required:

expr is the expression that is to be evaluated. truepart defines what the IIf function returns if the evaluation of expr returns true. falsepart defines what the IIf function returns if the evaluation of expr returns false. Many languages have an operator to accomplish the same purpose, generally referred to as a conditional operator (or, less precisely, as a ternary operator); the best known is ?:, as used in C, C++, and related languages. Some of the problems with the IIf function, as discussed later, do not exist with a conditional operator, because the language is free to examine the type and delay evaluation of the operands, as opposed to simply passing them to a library function.

Examples These examples evaluate mathematical expressions and return one of two strings depending on the outcome.

result = IIf(5 < 10, "Yes it is", "No it isn't") ' Returns "Yes it is"

result = IIf(2 + 2 = 5, "Correct", "Wrong") ' Returns "Wrong"

Criticisms

Efficiency Because IIf is a library function, it will always require the overhead of a function call, whereas a conditional operator will more likely produce inline code. Furthermore, the data type of its arguments is Variant. If the function is called with arguments of other types (variables or literals), there will be additional overhead to convert these to Variant. There may also be additional overhead to check the argument types and convert one of them if they do not have the same type.

Side effects Another issue with IIf arises because it is a library function: unlike the C-derived conditional operator, both truepart and the falsepart will be evaluated regardless of which one is actually returned. In the following code snippet:

value = 10 result = IIf(value = 10, TrueFunction, FalseFunction) although TrueFunction is the function intended to be called, IIf will call both TrueFunction and FalseFunction. Similarly,

While the intent may be to avoid a division by zero, whenever b is zero the error will actually happen. This is because the code in the snippet is executed as if by

This issue makes the IIf() call less useful than the conditional operator. To solve this issue, Microsoft developers had considered converting IIf to an intrinsic function; had this happened, the compiler would have been able to perform type inference and short-circuiting by replacing the function call with inline code.

Alternatives to IIf In Visual Basic, IIf is not the sole way to evaluate and perform actions based on whether an expression is true or false. The following example uses IIf:

result = IIf(x = y, value1, value2)

It could also be written in the following way, using standard conditionals:

The above example would also eliminate the problem of IIf evaluating both its truepart and falsepart parameters. Visual Basic 2008 (VB 9.0) introduced a true conditional operator, called simply "If", which also eliminates this problem. Its syntax is similar to the IIf function's syntax:

IIf in other programming languages $iif() is present in mIRC script, with similar syntax.

alias testiif { %testiif = 0 echo -a $iif(1,$testiif2,$testiif2) %testiif execution(s) unset %testiif } alias testiif2 { inc %testiif | return testing $!iif: }

Calling /testiif will print out "testing $iif: 1 execution(s)". mIRC's $iif acts more like C's ?: than IIf() in VB since it won't pre-evaluate both. IIF() is a function in dBase and xBase (1992 and older). iif() is also a compiler magic function of Oxygene. It is not a real function and is at compile time unrolled to conditional statements.

In this example a new strong type string named "someString" is created (using Type inference) and the iif function will fill it depending on the outcome of the boolean expression. SQL Server 2012 and newer implements the IIF() function (Transact-SQL):

IIf in C (and its variants) and Perl is the ?: conditional operator:

IIf in Python:

IIf (either) in Red and Rebol:

References

External links MSDN Documentation for IIf

Worked examples

Example 1 — a first encounter with IIf

Start with the simplest possible case. Write down what IIf claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to IIf before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about IIf ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of IIf

In research
IIf appears in science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses IIf in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
IIf is common in secondary-school and first-year university syllabi. It links to neighbouring topics BASIC programming language, Conditional constructs, Microsoft Visual Studio, so understanding it makes those chapters shorter.
In everyday life
Look for IIf outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “IIf” →

How to study IIf in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what IIf means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain IIf out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is IIf in simple terms?

In computing, IIf (an abbreviation for Immediate if) is a function in several editions of the Visual Basic programming language and ColdFusion Markup Language (CFML), and on spreadsheets that returns the second or third parameter based on the evaluation of the first parameter. It is an example of a…

Why does IIf matter?

Because it connects several science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study IIf?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on IIf.

Tags

  • BASIC programming language
  • Conditional constructs
  • Microsoft Visual Studio

Keep exploring