Smart BASIC Programming. Lesson 4

Post Reply
User avatar
Mr. Kibernetik
Site Admin
Posts: 4782
Joined: Mon Nov 19, 2012 10:16 pm
My devices: iPhone, iPad, MacBook
Location: Russia
Flag: Russia

Smart BASIC Programming. Lesson 4

Post by Mr. Kibernetik »

Lesson 4 - Logics

Often it is needed to perform an action only if some conditions are fulfilled. And IF THEN ELSE command serves this purpose.

Code: Select all

INPUT N
PRINT "Number";N;
IF EVEN (N) = 1 THEN PRINT "is even" ELSE PRINT "is odd"
IF THEN ELSE command can be written in several lines:

Code: Select all

INPUT A,B
IF A < B THEN
  PRINT A;"is less than";B
ELSE
  IF A = B THEN
    PRINT A;"is equal to";B
  ELSE
    PRINT A;"is greater than";B
  END IF
END IF
It is a bulky example, but it shows that conditions could be of any complexity.

IF THEN ELSE is working like that: after IF there is a condition. If this condition is true then code after THEN is executed. If condition is false then code after ELSE is executed. Also ELSE part can be absent, so it could be written like that:

Code: Select all

INPUT A,B
IF A < B THEN PRINT A;"is less than";B
IF A > B THEN PRINT A;"is greater than";B
IF A = B THEN PRINT A;"is equal to";B
Number instead of condition can be used. If number is zero then condition is considered to be false. If number is not zero then condition is true. So, first example could be written like that:

Code: Select all

INPUT N
PRINT "Number";N;
IF EVEN (N) THEN PRINT "is even" ELSE PRINT "is odd"
Conditions can include logical operations AND, OR and NOT. Operation NOT inverts the result when using number.

Code: Select all

INPUT X,Y
IF X > 0 OR Y > 0 THEN PRINT "At least one number is greater than zero"
IF X > 0 AND Y > 0 THEN PRINT "Both numbers are greater than zero"
EXERCISE 5
Write a program which fills the screen with random circles when device is oriented horizontally and with random lines when device is oriented vertically.

Post Reply