basicqb64

Why is there a colon in some Programs between 2 statements


I was going through a QB64 tutorial and saw the following program structure for printing numbers from 1 to 10 →

FOR x = 1 to 10: PRINT x: NEXT x

I have never seen such a kind of QB64 program. What do those colons : mean? Are they anything different?


Solution

  • I the QB64 IDE, you do not need to terminate statements using a special character like in other languages. This also means that you CANNOT expand a statement into multiple lines. Consider the following IF...THEN...ELSE... code block.

    IF 
    x = 1 
    THEN
    'Do something
    ...
    

    This wouldn't be interpreted, as each new line terminates the statement. The above code would be parsed as:

    Statement 1: IF [Incomplete Statement]
    Statement 2: x = 1 [assign value 1 to x]
    Statement 3: THEN [No such statement]
    ...
    

    This means that you must constrain a single statement over a single line.

    However, on the contrary, you are allowed to use multiple statements on a single line. In this case, since statements cannot be terminated with new lines, you must terminate them with a colon :. In your case,

    FOR x = 1 to 10: PRINT x: NEXT x
    

    This would be parsed as:

    Statement 1: FOR x = 1 to 10 [Initialize a value and set a condition for a FOR...NEXT loop]
    Statement 2: PRINT x [Print the value]
    Statement 3: NEXT x [Close the FOR...NEXT code block, and iterate the variable]
    

    There is no significant difference between using a colon or a new line for termination, but personally, I would recommend using new lines as they make the code considerably cleaner to look and easier to read. However, at times when there are multiple short and less important statements one after the other, you can combine them on a single line with colons to make your code shorter and concise to look.