The Math Forum

Search All of the Math Forum:

Views expressed in these public forums are not endorsed by NCTM or The Math Forum.

Math Forum » Discussions » Software » comp.soft-sys.matlab

Notice: We are no longer accepting new posts, but the forums will continue to be readable.

Topic: Very strange thing when using if else structure
Replies: 1   Last Post: Feb 27, 2013 11:59 AM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View  
Steven Lord

Posts: 18,038
Registered: 12/7/04
Re: Very strange thing when using if else structure
Posted: Feb 27, 2013 11:59 AM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

"Manalap " <> wrote in message
> I encountered a very strange problem when using if else structure.
> Simply put, this problem is like the following.
> If I code
> if x >3
> ....
> elseif x<=3
> ....
> end
> then I get a wrong result, but where as if I code
> if x>3
> ...
> else
> ...
> end
> then I get the correct result that I expected through symbolic reasoning.
> Aren't these two the same thing?

Not if x is nonscalar or x is NaN.

> Can these two be different?


x = [1 2 3 4 5];
x > 3
x <= 3

"An evaluated expression is true when the result is nonempty and contains
***all*** nonzero elements (logical or real numeric). Otherwise, the
expression is false." [Emphasis added.]

In the first case, neither the IF condition nor the ELSEIF condition are
satisfied for the x I gave above, since some elements are greater than 3 and
some are less than or equal to 3. In the second case, the IF condition is
not true but the ELSE is unconditional; it WILL execute if the IF does not.

Steve Lord
To contact Technical Support use the Contact Us link on

Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© The Math Forum at NCTM 1994-2018. All Rights Reserved.