June 24, 2024, 01:16:19 AM

News:

IonicWind Snippit Manager 2.xx Released!  Install it on a memory stick and take it with you!  With or without IWBasic!


Creative Basic is Easy

Started by GWS, July 29, 2011, 03:49:57 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

GWS

Hi folks,

I've read comments on another site about how difficult the examples given in some languages are for beginners.  They get put off and seek a 'simpler' language.

It struck me that a simple example might be in order, to demonstrate that languages don't come much simpler than Creative.

So any complexity in this example is down to the logic of the problem only, which seems simple, but has a few pitfalls.

The task is to display the time followed by AM or PM .. as appropriate.

First, we use a simple console window:


' A simple Creative Basic program to determine AM or PM time in a Console window
' using string functions and conditional logic tests ..

openconsole :' open a console window

def a$,b$,c$:string
def ColonPosition,hours,mins,secs:int

a$ = time$ :' get the current time in the format hh:mm:ss
ColonPosition = instr(a$,":") :' find the position of the first colon in the string

hours = val(left$(a$, ColonPosition))
mins = val(mid$(a$,ColonPosition+1,2))
secs = val(right$(a$,2))

select 1 :' test for a logical true condition (1 = True, 0 = False)
case (Hours > 12)
hours = hours - 12
b$ = " PM"
case (hours = 12) & (mins > 0) | (secs > 0)
b$ = " PM"
case (hours = 12) & (mins = 0) & (secs = 0)
b$ = " Noon"
default
b$ = " AM"
endselect

c$ = str$(hours) + mid$(a$,ColonPosition) + b$
print c$

do:until inkey$<>"" :' hold the screen until any key is pressed
closeconsole
end


This does the job, but like all console windows, it has a poor visual style for the user.

So a beginner might say .. Ah! a console window is easy .. but using real windows is much more difficult.

Well, it's only a little bit more difficult because we have to open a window and handle the window's messages. But we get a much nicer display for our effort.

Here's the same problem done using a window - and this time, we have a continually updating display.  Much more impressive.


' A simple Creative Basic program to determine AM or PM time in a normal window
' using string functions, conditional logic tests, and a timer for continuous updates ..
' Also shows setting the font, colour, and position of the text in the window.

def w:window
def textW,textH,run,style:int
def ColonPosition,hours,mins,secs:int
def a$,b$,c$:string

style = @SIZE|@MINBOX|@MAXBOX
window w,0,0,400,300,style,0,"AM or PM Example Window",main
setwindowcolor w,rgb(0,70,130)
centerwindow w

frontpen w, 0xffff00
setfont w,"Arial",24,600

starttimer w,500
run = 1

waituntil run = 0
stoptimer w
closewindow w

END

SUB main
select @class
case @IDCloseWindow
run = 0
case @IDTimer
a$ = time$ :' get the current time in the format hh:mm:ss
ColonPosition = instr(a$,":") :' find the position of the first colon in the string

hours = val(left$(a$, ColonPosition))
mins = val(mid$(a$,ColonPosition+1,2))
secs = val(right$(a$,2))

select 1 :' test for a logical true condition (1 = True, 0 = False)
case (Hours > 12)
hours = hours - 12
b$ = " PM"
case (hours = 12) & (mins > 0) | (secs > 0)
b$ = " PM"
case (hours = 12) & (mins = 0) & (secs = 0)
b$ = " Noon"
default
b$ = " AM"
endselect

c$ = str$(hours) + mid$(a$,ColonPosition) + b$

gettextsize w,c$,textW,textH
move w,(400-textW)/2,100
print w,c$

endselect

RETURN


I hope that demonstrates both the strength and simplicity of Creative Basic.

And one other thing for a beginner to consider.  Suppose you are so pleased with some program you've written, you wish to pass a copy to a friend - on a memory stick for instance.

Creative makes it easy to transfer a single .exe executable file.  That's all you need.

Some other languages require several (often large) files to be included as well.  Otherwise the program won't work - a real pain.

This is true of Visual Basic, Delphi and some other independent Basics.

Clearly, this is a big advantage of using Creative Basic (or IWB for that matter).

all the best, :)

Graham

Tomorrow may be too late ..