Re: Basic programming Question?

From: Edward G. Nilges (spinoza1111_at_yahoo.com)
Date: 04/11/04


Date: 10 Apr 2004 19:57:17 -0700


"C-man" <iamcleaver@shaw.ca> wrote in message news:<rXZdc.32684$J56.28153@edtnps89>...
> So say in a program. any program you have an if, else if, else statement.
> And if say the first if evaluates to true, does the program check the
> conditions of the else if or does it just skip past it. Basically I am
> thinking in terms of efficiency, as in is it wise to put the first if as one
> that you will think will be evaluated to true more often than the others..
> Will this give you any speed advantage?
>
> Thanks

Ben's of course right. The ElseIf will NOT be evaluated if the If is
true. Indeed I am not aware of any programming language that would
evaluate multiple Ifs unnecessarily.

But be aware of two related Gotcha's.

In Visual Basic 6 and VB .Net's a And b, when a is False, b is still
evaluated. Also, in both languages, and in a Or b, when a is True, b
is still evaluated.

There is no need but Basic follows the lead of Niklaus Wirth's Pascal
which was designed less for "efficiency" and more to support provably
correct code.

The semantics make no difference UNLESS b is a function call. But when
b is a function call, it can have unlimited side-effects and slow
things down.

The solution in VB 6 is to replace the use of Boolean operators in
these scenarios with nested Ifs and Boolean flags.

The solution in .Net is to use the new operators AndAlso and OrElse
which are guaranteed to work just like the operators && and || in C.
They will never evaluate right side unnecessarily and are both more
efficient and safer. In VB .Net, one should NEVER use And and Or.

In the first VB.Net Beta, Microsoft changed the rules for And and Or
but there was a Hue and Cry from the user community about old code
being "hard to port". As a result, Microsoft caved in March 2001 and
changed And and Or back to the old style and, to its enormous credit,
had the clever devils on its compiler and runtime team add the new
operators.

IMO Microsoft should have stood its ground because the objection "I
can't port the code" is without merit. VB 6 and VB .Net are different
languages and for this reason one should never "port" code without
careful review, and this review could catch the And and Or.

A related issue: Iif(a, b, c), a curiously useful expression that
yields b when a is True and c when a is False. It roughly corresponds
to the curious and gnomic expression of C, a?b:c, a most amusing
operator, but the critical difference, again, is that in the case of
Iif (in both VB 6 and .Net) c is evaluated whether or not a is True.
In a?b:c, c is ignored when a is True.

Iif is handy when you need to stuff logic in a business rule, for
example, for storage in a data base or when using Excel and Access at
the macro level. But for power programming its downside is that it
always evaluates c.

The unnecessary evaluation in all these cases derives, I think, from
the structure of a simple stack machine, such as is used to interpret
high-level languages: for in a And b it is natural, when compiling
code to a stack machine, to stack a, then stack b, then apply the
operator.

This is on the model of ordinary arithmetic and the fact that it is
unnecessary brings up a related issue in ordinary math: compilers can
and do eliminate sequences such as a*0 and a*1 for at base the same
reason C eliminates the unnecessary test: it's not necessary.

(gswork: here I go again...)

Of course, the whole issue is evidence for a transition from a
Platonic view of mathematics as timeless truths, and the correct
dialectical-materialist view in which mathematical expressions are
expressive of a labor process, a struggle as it were, that takes place
in history. For the Truth of a in a And b should liquidate the ability
of b to be expressed.

Proletarians are only interested in the praxis of every day life and
their time, and the time of the people's computers, should not be
wasted evaluating expressions unnecessarily in the running-dog ghost
ox devil Mandarin spirit.

Bourgeois running dog logicians are forever drawing our attention to
degenerate cases such as a+0 and True Or b because as decadent
representatives of the possessor classes, they have the leisure time
to waste. Henceforth, then, the Party directs that all computers shall
be programmed with Lazy and and Lazy or, but renamed Socialist and and
Socialist or.

(That sucked. It shows the real limitations and even the dangers of
such language because the next thing you know, b in a And b is a
"class enemy". However, it is true that computers bring out the fact
that math takes place in time inescapably and in Kant's sense. The
only computer scientist who realized that was Dijkstra.)

(Also I should note that I have heard mathematically isomorphic
language about "practical business needs" in the corporation, where
the stockholders occupy the favored position occupied by the
proletariat. The reductionist language is not unique to Marxism.
Prolix details as always available upon application.)



Relevant Pages

  • Re: Does Python really follow its philosophy of "Readability counts"?
    ... Because human are smarter than computers. ... Why can optimizing C compilers make more efficient code than the best human assembly language programmers? ... That's even better than Python ... protection module from their Python installations. ...
    (comp.lang.python)
  • Re: Why is OO Popular?
    ... In most domains there are many abstractions, ... > that computers behave like other computers. ... surely just as solid a basis for design, ... since this implies that language has no impact on ...
    (comp.object)
  • Thai Instructor $31K to $45 K, Must be US Citizen; job in Monterey, Califonia
    ... Defense Language Institute Foreign Language Center, Monterey, ... an evaluation of degreeobtained outside the U.S. must be mailed to ... Applicants whose self-nominations are received by the cut-off date will ...
    (soc.culture.thai)
  • Re: How much intelligence?
    ... Imagine a native English speaker who knows no Chinese locked in a room ... In what language are the instructions written? ... trying to emulate AI directly by means of TvN mechanics. ... saying we can't model actual intelligence artificially with computers ...
    (comp.ai.philosophy)
  • Sr Instructor (thai), $38K to 58K, California, US citizenship required
    ... Army Training and Doctrine Command ... Defense Language Institute Foreign Language Center, Monterey, ... Applicants with a Master's degree and less than one year of teaching ... an evaluation of degreeobtained outside the U.S. must be mailed to ...
    (soc.culture.thai)