Small Basic - Mutual Exclusion
A Note on Mutual Exclusion
You could write a square root program like this:
1 TextWindow.Write("Please enter the object's height (meters): ")
2 height = TextWindow.ReadNumber()
3 If (height < 0 ) Then
4 TextWindow.WriteLine("Don't be so negative!")
5 EndIf
6 If ( height >= 0 ) Then
7 time = Math.SquareRoot(10 * height / 49)
8 time = Math.Round( time * 100 ) / 100 ' Rounds to 2 decimal places
9 TextWindow.WriteLine("Fall time = " + ans + " sec. ")
10 EndIf
This program gives the same output as Listing 7-4, but it doesn’t use Else.
If the input number’s less than 0, the program prints the error message on Line 4 and then checks the next condition (height >= 0) on Line 6. If you think about this carefully, you’ll see that the second test in this case isn’t needed because the two conditions, (height < 0) and (height >= 0), are mutually exclusive (only one or the other is true). That means if height is less than 0, it can’t be greater than or equal to 0. The If/Else statement removes that second check! Using If/Else in this case is the way to go!
Learn more about Small Basic: Small Basic: Relational Operators & Strings
Have a Small and Basic day!
- Ninja Ed & Majed Marji
Comments
Anonymous
March 17, 2015
Hi Ed, Thanks for the blog post, And I mailed you regarding Moderator for SharePoint 2013 Here's my link social.msdn.microsoft.com/.../indul%20hassan and Forums link social.technet.microsoft.com/.../homeAnonymous
March 24, 2015
Okay. Thanks, Indul!Anonymous
January 31, 2016
Computers Today (part 1 of 6) blogs.msdn.com/.../computers-today.aspx ..... CS SPOTLIGHT: Girls in computer programming... why it matters!!! blogs.msdn.com/.../cs-spotlight-girls-in-computer-programming-why-it-matters.aspx ... Computational Thinking - Videos & Papers by Jeannette Wing blogs.msdn.com/.../computational-thinking-videos-amp-papers-by-jeannette-wing.aspx