Writing the identical utility in a number of languages is an effective way to be taught new methods to program. Most programming languages have sure issues in frequent, corresponding to:
- Variables
- Expressions
- Statements
These ideas are the premise of most programming languages. Once you perceive them, you can begin figuring the remaining out.
Programming languages normally share some similarities. Once you already know one programming language, you may be taught the fundamentals of one other by recognizing its variations.
Practicing with a normal program is an effective approach of studying a brand new language. It permits you to give attention to the language, not this system’s logic. I’m doing that on this article collection utilizing a “guess the number” program, through which the pc picks a quantity between one and 100 and asks you to guess it. The program loops till you guess the quantity accurately.
This program workout routines a number of ideas in programming languages:
- Variables
- Input
- Output
- Conditional analysis
- Loops
It’s an important sensible experiment to be taught a brand new programming language. This article focuses on Basic.
Guess the quantity in (Bywater) Basic
There isn’t any actual commonplace for the Basic programming language. Wikipedia says, “BASIC (Beginners’ All-purpose Symbolic Instruction Code) is a family of general-purpose, high-level programming languages designed for ease of use.” The BWBasic implementation is accessible underneath the GPL.
You can discover Basic by writing a model of the “guess the number” recreation.
Install Basic on Linux
In Debian or Ubuntu, you may set up Basic with the next:
$ apt set up -y bwbasic
Download the most recent launch tarball for Fedora, CentOS, Mageia, and some other Linux distribution. Extract it, make it executable, after which run it from a terminal:
$ tar --extract --file bwbasic*z
$ chmod +x bywater
$ ./bywater
On Windows, download the .exe release.
Basic code
Here is my implementation:
10 worth$ = cint(rnd * 100) + 1
20 enter "enter guess"; guess$
30 guess$ = val(guess$)
40 if guess$ < worth$ then print "Too low"
50 if guess$ > worth$ then print "Too high"
60 if guess$ = worth$ then 80
70 goto 20
80 print "That's right"
Basic packages might be numbered or unnumbered. Usually, it’s higher to write down packages unnumbered, however writing them with numbered strains makes it simpler to consult with particular person strains.
By conference, coders write strains as multiples of 10. This method permits interpolating new strains between current ones for debugging. Here’s a proof of my methodology above:
- Line 10: Computes a random worth between 1 and 100 utilizing the built-in rnd perform, which generates a quantity between 0 and 1, not together with 1.
- Line 20: Asks for a guess and places the worth within the guess$ scalar variable. Line 30 converts the worth to a numeric one.
- Lines 40 and 50: Give the guesser suggestions, relying on the comparability.
- Line 70: Goes to the start of the loop.
- Line 60: Breaks& the loop by transferring management to line 80. Line 80 is the final line, so this system exits after that.
Sample output
The following is an instance of this system after placing it in program.bas
:
$ bwbasic program.bas
Bywater BASIC Interpreter/Shell, model 2.20 patch stage 2
Copyright (c) 1993, Ted A. Campbell
Copyright (c) 1995-1997, Jon B. Volkoff
enter guess? 50
Too low
enter guess? 75
Too low
enter guess? 88
Too excessive
enter guess? 80
Too low
enter guess? 84
Too low
enter guess? 86
Too excessive
enter guess? 85
That's proper
Get began
This “guess the number” recreation is a superb introductory program for studying a brand new programming language as a result of it workout routines a number of frequent programming ideas in a fairly simple approach. By implementing this easy recreation in several programming languages, you may exhibit some core ideas of the languages and evaluate their particulars.
Do you’ve a favourite programming language? How would you write the “guess the number” recreation in it? Follow this text collection to see examples of different programming languages which may curiosity you!