Студопедия

Главная страница Случайная страница

КАТЕГОРИИ:

АвтомобилиАстрономияБиологияГеографияДом и садДругие языкиДругоеИнформатикаИсторияКультураЛитератураЛогикаМатематикаМедицинаМеталлургияМеханикаОбразованиеОхрана трудаПедагогикаПолитикаПравоПсихологияРелигияРиторикаСоциологияСпортСтроительствоТехнологияТуризмФизикаФилософияФинансыХимияЧерчениеЭкологияЭкономикаЭлектроника






White Space and Indentation






RFO Basic does not require line terminators at the ends of code lines, and you can insert empty white space (tabs, spaces, etc.) as desired into code. It's standard practice in virtually all programming languages to indent code blocks which perform a group of related activities. For example, you could put multiple if-then conditions all on one line:

! Set some numeric variables (exp1 and exp2 now both equal the number 1): exp1 = 1exp2 = 1! Perform 2 separate if evaluations using the variables, all on one line: if (exp1 = 1) then (if (exp2 = 1) then (print " Yes"))

But it's much easier to understand the logic of each related code block above, if those conditional blocks are each indented. The following code does the exact same thing as the code above. It checks the second conditional evaluation only if the first evaluation (if the variable exp1 = 1) is true. So, everything indented inside that outside surrounding if-endif block only occurs if that first condition evaluates to true. It's much easier to follow the logic in this indented format:

exp1 = 1exp2 = 1 if (exp1 = 1) if (exp2 = 1) % These 3 lines only print " yes" % run if the above endif % evaluation is trueendif

NOTE: if you write an entire if-then condition on 1 line, then the word " then" is required. If you separate the " if" evaluation onto its own line, then the word " then" is not required. Notice that there are no " then" keywords in the code above.

5.3 If/Else

If/Else chooses between two blocks of code to evaluate, based on whether the given condition is true or false. Its syntax is:

if (condition) perform this code if the condition is trueelse perform this code if the condition is falseendif

For example:

time y$, m$, d$, h$, n$, s$if val(h$) > 8 print " It's time to get up! " else print " You can keep on sleeping." endif

These types of evaluations are found in just about every type of computer program:

input " Username: ", user$ if user$ = " validuser" print " Welcome back! " else print " You are not a registered user." endif

5.4 If/ElseIf/Else

You can choose between multiple evaluations of any complexity using the " If/ElseIf/Else" structure. If none of the cases evaluate to true, you can use " Else" to run some default code:

input " What is your name? ", name$ if is_in (" a", name$) < > 0 print " Your name contains the letter 'a'" elseif is_in (" e", name$) < > 0 print " Your name contains the letter 'e'" elseif is_in (" i", name$) < > 0 print " Your name contains the letter 'i'" elseif is_in (" o", name$) < > 0 print " Your name contains the letter 'o'" elseif is_in (" u", name$) < > 0 print " Your name contains the letter 'u'" else print " Your name doesn't contain any vowels! " endif

5.5 Multiple Conditions: " and", " or"

You can check for more than one condition to be true, using the " and" and " or" evaluations. Here's an example that gets a username and password from the user, tests that data using an " if" evaluation, and alerts the user if both responses are correct:

input " Username: ", username$input " Password: ", password$ if ((username$ = " validuser") AND (password$ = " validpass")) print " Welcome back! " else print " You are not registered! " endif

This example responds favorably if the user chooses any one of three favorite colors:

input " What is your favorite color? ", color$ if ((color$ = " blue") or (color$ = " red") or (color$ = " orange") print " That's one of my favorite colors too! " else print " We don't like any of the same colors: (" endif

Switch

This evaluation allows you to compare as many values as you want against a main value, and run a selected block of code for any matching value, with an optional default block of code to be run if no matches are found:

input " What's your favorite day of the week? ", favoriteDay$ sw. begin favoriteDay$sw.case " Monday" print " Monday is the worst! The work week begins..." sw.breaksw.case " Tuesday" sw.case " Thursday" print " Tuesdays and Thursdays are both ok, I guess..." sw.breaksw.case " Wednesday" print " The hump day - the week is halfway over! " sw.breaksw.case " Friday" print " Yay! TGIF! " sw.breaksw.case " Saturday" sw.case " Sunday" print " Of course, the weekend! " sw.breaksw.default print " You didn't type in the name of a day! " sw.end! note that you can perform the same action for 2 or more conditions! by placing those conditions consecutively before an sw.break! (as in the Tuesday/Thursday and Saturday/Sunday conditions above)

The conditional structures you've seen here will help your programs perform appropriate actions in RFO Basic, based upon user input, data content, and other potentially changing situations.


Поделиться с друзьями:

mylektsii.su - Мои Лекции - 2015-2024 год. (0.006 сек.)Все материалы представленные на сайте исключительно с целью ознакомления читателями и не преследуют коммерческих целей или нарушение авторских прав Пожаловаться на материал