Difference between revisions of "Blitz:Shell Input/Output"

From Amiga Coding
Jump to: navigation, search
m
m
 
Line 5: Line 5:
 
The Print statement is used to print one or more items directly to the shell window. Each argument will be shown directly after the previous argument with no space in between so you need to separate your items yourself if needed. For example:
 
The Print statement is used to print one or more items directly to the shell window. Each argument will be shown directly after the previous argument with no space in between so you need to separate your items yourself if needed. For example:
  
<code>a = 6
+
<pre>a = 6
Print "The value of a is", a</code>
+
Print "The value of a is", a</pre>
 
Will show the following in the shell window:
 
Will show the following in the shell window:
<code>The value of a is6</code>
+
<pre>The value of a is6</pre>
 
Adding spaces to the messages fixes the output:
 
Adding spaces to the messages fixes the output:
<code>a = 6
+
<pre>a = 6
Print "The value of a is ", a</code>
+
Print "The value of a is ", a</pre>
 
Any combination of arguments is possible: string constants (which are surrounded by quote marks), string variables, constants, numerical variables, numerical expressions, or returned values of a function.
 
Any combination of arguments is possible: string constants (which are surrounded by quote marks), string variables, constants, numerical variables, numerical expressions, or returned values of a function.
  
 
Print will leave the cursor exactly at the end of what ever is output to the console, so
 
Print will leave the cursor exactly at the end of what ever is output to the console, so
<code>Print "Message 1"
+
<pre>Print "Message 1"
Print "Message 2"</code>
+
Print "Message 2"</pre>
 
will output
 
will output
<code>Message 1Message 2</code>
+
<pre>Message 1Message 2</pre>
  
 
===NPrint===
 
===NPrint===
 
NPrint is almost identical to Print, except that it adds a newline at the end of its output. So
 
NPrint is almost identical to Print, except that it adds a newline at the end of its output. So
<code>NPrint "Message 1"
+
<pre>NPrint "Message 1"
NPrint "Message 2"</code>
+
NPrint "Message 2"</pre>
 
will output
 
will output
<code>Message 1
+
<pre>Message 1
Message 2</code>
+
Message 2</pre>
  
 
'''Note:''' At least one argument is required for Print/NPrint statements. So, unlike other BASICs, to skip a line you need to provide something to print. In this case, a null string can be used. For example:
 
'''Note:''' At least one argument is required for Print/NPrint statements. So, unlike other BASICs, to skip a line you need to provide something to print. In this case, a null string can be used. For example:
<code>NPrint "Message 1"
+
<pre>NPrint "Message 1"
 
NPrint ""
 
NPrint ""
NPrint "Message 2"</code>
+
NPrint "Message 2"</pre>
 
will output
 
will output
<code>Message 1
+
<pre>Message 1
  
Message 2</code>
+
Message 2</pre>
  
 
AmiBlitz added the ability to include escape characters in strings. See [[Blitz:String Handling|String Handling]] for more information.
 
AmiBlitz added the ability to include escape characters in strings. See [[Blitz:String Handling|String Handling]] for more information.
Line 44: Line 44:
  
 
Edit is a function, so returns the value input by the user. It halts program flow and waits for the user to enter a number, finishing their input with the Return or Enter key. Unlike other BASICs, it does not display a "?" character while waiting on input, and a prompt cannot be added as an argument so needs to be provided separately. For example:
 
Edit is a function, so returns the value input by the user. It halts program flow and waits for the user to enter a number, finishing their input with the Return or Enter key. Unlike other BASICs, it does not display a "?" character while waiting on input, and a prompt cannot be added as an argument so needs to be provided separately. For example:
<code>Print "What number did you choose? "
+
<pre>Print "What number did you choose? "
 
a = Edit(5)
 
a = Edit(5)
NPrint "Thank you. You chose ", a</code>
+
NPrint "Thank you. You chose ", a</pre>
 
In this example, the numbers typed by the user will appear after the end of the question, since the Print command doesn't add a newline.
 
In this example, the numbers typed by the user will appear after the end of the question, since the Print command doesn't add a newline.
  
 
===Edit$===
 
===Edit$===
 
Edit$ is almost identical to Edit, except that it accepts text entered by the user and returns a string instead. For example:
 
Edit$ is almost identical to Edit, except that it accepts text entered by the user and returns a string instead. For example:
<code>Print "What is your name? "
+
<pre>Print "What is your name? "
 
a$ = Edit$(30)
 
a$ = Edit$(30)
NPrint "Nice to meet you, ", a$</code>
+
NPrint "Nice to meet you, ", a$</pre>
 
Inputting numbers using Edit$ will require the returned string to be converted to a number type before it can be used as a value.
 
Inputting numbers using Edit$ will require the returned string to be converted to a number type before it can be used as a value.
  

Latest revision as of 13:55, 25 January 2018

Input and output using the shell is quite simple in Blitz Basic / AmiBlitz, and generally similar to most other BASIC dialects.

Shell Output

Print

The Print statement is used to print one or more items directly to the shell window. Each argument will be shown directly after the previous argument with no space in between so you need to separate your items yourself if needed. For example:

a = 6
Print "The value of a is", a

Will show the following in the shell window:

The value of a is6

Adding spaces to the messages fixes the output:

a = 6
Print "The value of a is ", a

Any combination of arguments is possible: string constants (which are surrounded by quote marks), string variables, constants, numerical variables, numerical expressions, or returned values of a function.

Print will leave the cursor exactly at the end of what ever is output to the console, so

Print "Message 1"
Print "Message 2"

will output

Message 1Message 2

NPrint

NPrint is almost identical to Print, except that it adds a newline at the end of its output. So

NPrint "Message 1"
NPrint "Message 2"

will output

Message 1
Message 2

Note: At least one argument is required for Print/NPrint statements. So, unlike other BASICs, to skip a line you need to provide something to print. In this case, a null string can be used. For example:

NPrint "Message 1"
NPrint ""
NPrint "Message 2"

will output

Message 1

Message 2

AmiBlitz added the ability to include escape characters in strings. See String Handling for more information.

Shell Input

Edit

Edit is used for inputting numbers via the shell window, and is similar to the Input command used in other BASIC dialects. Inputting strings using Edit will return a 0 (zero), read more about variable types.

Edit is a function, so returns the value input by the user. It halts program flow and waits for the user to enter a number, finishing their input with the Return or Enter key. Unlike other BASICs, it does not display a "?" character while waiting on input, and a prompt cannot be added as an argument so needs to be provided separately. For example:

Print "What number did you choose? "
a = Edit(5)
NPrint "Thank you. You chose ", a

In this example, the numbers typed by the user will appear after the end of the question, since the Print command doesn't add a newline.

Edit$

Edit$ is almost identical to Edit, except that it accepts text entered by the user and returns a string instead. For example:

Print "What is your name? "
a$ = Edit$(30)
NPrint "Nice to meet you, ", a$

Inputting numbers using Edit$ will require the returned string to be converted to a number type before it can be used as a value.

Note: Both Edit and Edit$ require one argument: the maximum number of characters to accept. If the user enters more characters than this number (5 in the example), the result will only contain the first 5 characters typed.