BBC BASIC for Windows
Programming >> BBC BASIC language >> color print
http://bb4w.conforums.com/index.cgi?board=language&action=display&num=1394932141

color print
Post by Wendell on Mar 16th, 2014, 01:09am

Why does this program print the color twice
should print either red 0r green ?

REM HAVING COLOR PROBLEM
INPUT A$
IF A$="C" THEN (RED) ELSE (GREEN)
CLS
(RED)
COLOUR 1
PRINT TAB(16,29)A$
(GREEN)
COLOUR 2
PRINT TAB(16,29)A$
END

Re: color print
Post by Edja on Mar 16th, 2014, 01:43am

Quote:
Why does this program print the color twice
should print either red 0r green ?


When you input "C" you only see a green C on the screen because you first display a red C and immediately replace it with a green C

Change your second PRINT statement to
PRINT TAB(20,29)A$

and you'll see a red and a green C

Eddy
Re: color print
Post by Wendell on Mar 16th, 2014, 02:00am

i am sorry not making myself clear. i only want red or green to print not both each time run prog. which ever is true. sorry i am a beginner. if "C" print it red . and that it
Re: color print
Post by Malvern on Mar 16th, 2014, 03:08am

Hi,
I think you imagine that it is going to jump to the labels. It doesn't but even if it did the program would complete the Green part last and show green. It might if you used some GOTO's but we don't really recommend that as you may have noticed.

You are making it more difficult than it really is. What changes the colour is the COLOUR statement so that is all that needs to be in the IF statement. Something like:

Code:
      INPUT A$
      CLS
      IF A$="C" THEN COLOUR 1 ELSE COLOUR 2
      PRINT TAB(16,29)A$

      END
 


I have my own colour problem: I can't tell if it prints red or green as I have a red-green colour deficiency! But it should work.


;)
Re: color print
Post by Wendell on Mar 16th, 2014, 05:45am

thank you i was making it harder than it was. but i am making progress. thank again
Re: color print
Post by Malvern on Mar 16th, 2014, 4:14pm

Before we leave this I think we need to show how what you were trying to write could be written.
Labels are used as places to jump to but are not the best way of controlling the program flow. Procedures are much better. At the end of a procedure the program returns to the next statement after the calling statement. It is a return ticket in other words: out, do the work and then return back to the next statement.

Your initial program could have been written like this:
Code:
 
     INPUT A$
      CLS
      IF A$="C" THEN PROCred ELSE PROCgreen

      END

      DEF PROCred
      COLOUR 1
      PRINT TAB(16,29)A$
      ENDPROC

      DEF PROCgreen
      COLOUR 2
      PRINT TAB(16,29)A$
      ENDPROC
 


And you would want to write it in this way if you wanted to do more things rather than just print.

Before going too much further I would suggest that you take a look at the Tutor that is linked to the BB4W Help menu.
It may seem like going back to school but it is very good and will help you progress faster than by trial and error. By all means write little programs to test your understanding. Better still is to find a programming buddy. I expect there are people here that would offer to help. We all had to learn and tend to forget how difficult it was to begin with.




Re: color print
Post by rtr on Mar 16th, 2014, 4:50pm

on Mar 16th, 2014, 4:14pm, Malvern wrote:
Your initial program could have been written like this:

Indeed it could, but it's a style that has its origins in the original BBC Microcomputer BASIC which, critically, had no multi-line IF...ELSE...ENDIF statement. This meant that the only way to avoid GOTOs was to put the conditional clauses in PROCedures, as you illustrate.

With the availability of a multi-line IF...ENDIF (in ARM BASIC and BB4W) I would consider this preferable:

Code:
      INPUT A$
      CLS
      IF A$="C" THEN
        COLOUR 1
        PRINT TAB(16,29)A$
      ELSE
        COLOUR 2
        PRINT TAB(16,29)A$
      ENDIF 

As a general rule I would move code into a PROC only if it is called from multiple places, or if it's sufficiently lengthy that including it 'inline' within an IF...ENDIF statement causes the structure to be unclear (for example if you can't seen the entire conditional clause on one screen), or if it uses LOCAL variables.

Richard.