by admin

Gw Basic Nested Loop Programs

Gw Basic Nested Loop Programs Average ratng: 6,6/10 2402 votes
< Loops
Loops/For
You are encouraged to solve this task according to the task description, using any language you may know.

For” loops are used to make some block of code be iterated a number of times, setting a variable or parameter to a monotonically increasing integer value for each execution of the block of code.

QBasic Nested for loop-1. Krackhead 14 Years Ago. Im taking a qbasic class and am having a very hard time on this program.here is the question. Number pattern programs in C. Basic C programming, Operators, If else, Nested If else, For loop, Nested loop. Basic C programming exercises index. In QBasic an equation has a basic setup a right side and a left side. For instance X=5. In many programs (as above), INKEY$ will appear nested in a loop.

Common extensions of this allow other counting patterns or iterating over abstract structures other than the integers.


Task

Show how two loops may be nested within each other, with the number of iterations performed by the inner for loop being controlled by the outer for loop.

Specifically print out the following pattern by using one for loop nested in another:


Related tasks
  • Loop over multiple arrays simultaneously
  • Loops/Break
  • Loops/Continue
  • Loops/Do-while
  • Loops/Downward for
  • Loops/For
  • Loops/For with a specified step
  • Loops/Foreach
  • Loops/Increment loop index within loop body
  • Loops/Infinite
  • Loops/N plus one half
  • Loops/Nested
  • Loops/While
  • Loops/with multiple ranges
  • Loops/Wrong ranges



Reference
  • For loop Wikipedia.


  • 20BASIC
  • 80Icon and Unicon
  • 112MUMPS
  • 146REXX
  • 167Stata
  • 177UNIX Shell

360 Assembly[edit]

Basic - Algol style

The opcode BXH uses 3 registers, one for index, one for step and one for limit.

Output:
Structured Macros

Structured and without BXH, only one register used by loop.

Same as above

8th[edit]

This illustrates two kinds of 'for' loop. The first kind is 'loop', which iterates from the low to the high value, and passes the current loop index as a parameter to the inner word. The second is 'times', which takes a count and repeats the word that many times.

ActionScript[edit]

Ada[edit]

Agena[edit]

Tested with Agena 2.9.5 Win32

ALGOL 60[edit]

ALGOL 68[edit]

Output:

ALGOL-M[edit]

ALGOL W[edit]

In Algol W, write starts a new line, writeon continues it.

Alore[edit]

AmigaE[edit]

Apex[edit]

AppleScript[edit]

Output:

ARM Assembly[edit]

AutoHotkey[edit]

AWK[edit]

Axe[edit]

In this example, the Axe code is nearly identical to the TI-83 BASIC version. However, note the swapped order of the I and J in the Output() statement. Also, unlike TI-83 BASIC, Axe does not support an increment value other than 1.

Babel[edit]

The key operator here is 'iter' which gives the current iteration of the loop body itresides in. When used with the 'times' operator, it generates a countdown.


bash[edit]


BASIC[edit]

Applesoft BASIC[edit]

BBC BASIC[edit]

Commodore BASIC[edit]

Creative Basic[edit]

GW-BASIC[edit]

FBSL[edit]

Output:

FUZE BASIC[edit]

IS-BASIC[edit]

IWBASIC[edit]

Liberty BASIC[edit]

Unlike some BASICs, Liberty BASIC does not require that the counter variable be specified with 'next'.


Microsoft Small Basic[edit]

PureBasic[edit]

Run BASIC[edit]

smart BASIC[edit]

While some versions of BASIC allow for NEXT without a variable, smart BASIC requires the variable designation.

Visual Basic[edit]

Works with: VB6

Visual Basic .NET[edit]

ZX Spectrum Basic[edit]

On the ZX Spectrum, we need line numbers:

Batch File[edit]

bc[edit]

Befunge[edit]

blz[edit]

Bracmat[edit]

Brainf***[edit]

Brat[edit]

C[edit]

C++[edit]

C#[edit]

Ceylon[edit]

Chapel[edit]

Chef[edit]

Clojure[edit]

Gw Basic Nested Loop Programs

COBOL[edit]

ColdFusion[edit]

Remove the leading space from the line break tag.

With tags:

With script:

Nested Loop Join

Common Lisp[edit]

Coq[edit]

D[edit]

Dao[edit]

Dart[edit]

dc[edit]

[..]sA defines the inner loop A and [..]sB defines the outer loop B. This program nests the entrance to loop A inside loop B.

Delphi[edit]

DMS[edit]

dodo0[edit]

Dragon[edit]

DWScript[edit]

Dyalect[edit]

Output:

E[edit]

This loop is a combination of for .. in .. which iterates over something and a.b which is a range object that is iteratable. (Also, writing a.!b excludes the value b.)

EDSAC order code[edit]

As with many other machine-level languages, there is no inbuilt looping construct; but the equivalent of a FOR or DO loop can easily be synthesized using conditional branching orders and a control variable. Falcon 4 original iso eng.

The EDSAC character set does not include a * character, so + has been used instead.

Characters are encoded in five-bit form, with each code point producing a different character depending on whether the machine is in 'letter' or 'figure' mode: this is why it is necessary to output a 'figure shift' control character at the beginning of the job.

Output:

EGL[edit]

Ela[edit]


Output:

Elena[edit]

ELENA 4.x :

Elixir[edit]

one line (Comprehensions)

Erlang[edit]


ERRE[edit]

Euphoria[edit]

puts() is a function that takes two arguments; an integer and a sequence. Strings are simply sequences; there is no string type.The integer specifies where to put the 'string'. 0 = STDIN, 1 = STDOUT, 2 = STDERR, 3+ = files that are opened with the open() function.puts() prints the sequence out, as a 'string'. Each element in the sequence provided is printed out as the character with that value in the ASCII character chart.

F#[edit]

Factor[edit]

FALSE[edit]

Fantom[edit]

Using for loops:

Using range objects:

FOCAL[edit]

When the program exits the outer loop, the control variable I is set to 4 + 1 = 5; we can therefore permit execution to fall through into the inner loop for one more iteration.

Output:

Forth[edit]

One more:

Fortran[edit]

Fortran 95 (and later) has also a loop structure that can be used only when the result is independent from real order of execution of the loop.

But if one accepts that a do-loop can be expressed without the actual word 'do' (or 'for'), then

That is a complete programme, though a more polite source file would have INTEGER I,J. It uses the old-style DO label etc. style of DO-loop to save on having to specify an END DO. The WRITE statement's output list is generated by an 'implied' DO-loop having much of the form of DO J = 1,I and is indeed a proper loop. The output item is a text literal, which in earlier Fortran was unknown, however the result can still be achieved:

This works because if a value cannot be fitted into its output field, the field is filled with asterisks. Which, is what is wanted! Just allow one digit for output (I1), and present a large integer.

FreeBASIC[edit]

Frink[edit]

Futhark[edit]

Futhark does not have I/O, so this program simply counts in theinner loop.

Gambas[edit]

GAP[edit]

GML[edit]

Go[edit]

Output:

Groovy[edit]

Solution:

GW-BASIC[edit]

Hack[edit]

Haskell[edit]

But it's more Haskellish to do this without loops:

Haxe[edit]

hexiscript[edit]

HicEst[edit]

HolyC[edit]

Icon and Unicon[edit]

Icon[edit]

Unicon[edit]

The Icon solution works in Unicon.

Inform 7[edit]

J[edit]

J is array-oriented, so there is very little need for loops. For example, except for the requirement for loops, one could satisfy this task this way:

J does support loops for those times they can't be avoided (just like many languages support gotos for those time they can't be avoided).

But you would almost never see J code like this.

Java[edit]

JavaScript[edit]


Alternatively, using JavaScript's Array.forEach(), and given an array of indices,or a simple range function which generates a range:

We could write something like:

but it might be more natural in JavaScript, if we are going to use built-in Array functions, to simplify a little with Array.reduce(), writing:

in which the inner n refers to the Array value visited at the next level out, and the triangle is returned as a single expression, rather than as a series of variable mutations.

Finally, in contexts where an expression composes better than a statement, the effect of a loop can often be expressed as a map.

jq[edit]

Example using demo(6)

Output:

Jsish[edit]

Code from Javascript entry.

Julia[edit]

Output:

Klong[edit]

Kotlin[edit]

LabVIEW[edit]

This image is a VI Snippet, an executable image of LabVIEW code. The LabVIEW version is shown on the top-right hand corner. You can download it, then drag-and-drop it onto the LabVIEW block diagram from a file browser, and it will appear as runnable, editable code.

Lang5[edit]


Lasso[edit]

LC3 Assembly[edit]

Output:

Lingo[edit]

Lisaac[edit]

LiveCode[edit]

Logo[edit]

Lua[edit]

M2000 Interpreter[edit]

By default there For loops always perform on execution of block. If end value is smaller than fist value, then step adjust to that direction. When first value is equal to second value then if we declare step negative end value after execution of block became start value minus absolute step, or if step is positive, became start value plus step. We can use a switch for interpreter to change IF's STEP to act as BASIC's, and sign of step always used, and there is situations where block can't executed.

M4[edit]

make[edit]

Maple[edit]


Mathematica[edit]

MATLAB / Octave[edit]

Vectorized version:

Maxima[edit]

MAXScript[edit]

Mercury[edit]

Modula-2[edit]

Modula-3[edit]

MOO[edit]

Morfa[edit]

MUMPS[edit]

Routine[edit]

One line[edit]

The if statement has to follow the write, or else the if statement would control the write (5 lines with one asterisk each).

Nanoquery[edit]

Nemerle[edit]

NetRexx[edit]

NewLISP[edit]

Nim[edit]

Oberon-2[edit]

Works with oo2c Version 2

Objeck[edit]

OCaml[edit]

Octave[edit]

Oforth[edit]

Onyx[edit]

Using repeat inside the for loop instead of nesting another for loop is shorter and more efficient.

Order[edit]

(Order cannot print newlines, so this example just uses a space.)

Oz[edit]

Note: we don't use the inner loop variable, so we prefer not to give it a name.

Panoramic[edit]

PARI/GP[edit]

Pascal[edit]

Perl[edit]

However, if we lift the constraint of two loops the code will be simpler:

or equivalently

Perl 6[edit]

or using only one for loop:

or without using any loops at all:

Phix[edit]

PHP[edit]

or

or

PicoLisp[edit]

Pike[edit]

PILOT[edit]

Core PILOT does not offer any way of printing without a newline, so in the inner loop we concatenate another star onto the string variable $stars each time round and then print it in the outer loop.

PL/I[edit]

Basic version:

Advanced version:

Due to the new line requirement a mono line version is not possible

Pop11[edit]

PowerShell[edit]

Alternatively the same can be achieved with a slightly different way by using the range operator along with the ForEach-Object cmdlet:

while the inner loop wouldn't strictly be necessary and can be replaced with simply '*' * $_.

Processing[edit]

Prolog[edit]

Prolog has a built in iterator, between(Lo,Hi,I) which binds the value of I to successive values from Lo to Hi. This is the closest thing Prolog has to a 'for' loop.

Python[edit]

Note that we have a constraint to use two for loops, which leads to non-idiomatic Python. If that constraint is dropped we can use the following, more idiomatic Python solution:

or

R[edit]

Racket[edit]

REBOL[edit]

Retro[edit]

REXX[edit]

using concatenation[edit]

output:

using abutment[edit]


Ring[edit]

can be done in just one line:

or multiple line

Ruby[edit]

One can write a for loop as for i in 1.5; ..end or as for i in 1.5 do .. end or as (1.5).each do i .. end. All three forms call Range#each to iterate 1.5.

for Range#each

Ruby has other ways to code these loops; Integer#upto is most convenient.

Integer#upto Integer#times Kernel#loop

Or we can use String#* as the inner loop, and Enumerable#map as the outer loop. This shrinks the program to one line.

Rust[edit]

The compiler warns when you create an unused variable; here we use _ to avoid this effect.

Salmon[edit]

or


SAS[edit]

Sather[edit]

Sather allows the definition of new iterators. Here's we define for! so that it resembles the known for in other languages, even though the upto! built-in can be used.

Scala[edit]

Scheme[edit]

Scilab[edit]

Output:

Seed7[edit]

SETL[edit]

Sidef[edit]

for(;;) loop:

for([]) loop:

for-in loop:

Idiomatic:

Simula[edit]

Slate[edit]

Smalltalk[edit]

or:

(only for demonstration of nested for-loops; as the column is not needed, the first solution is probably clearer).

However, streams already have some builtin repetition mechanism, so a programmer might write:

SNOBOL4[edit]

A slightly longer, 'mundane' version

The 'real SNOBOL4' starts here:

one 'loop' only:

.. or just (courtesy of GEP2):

SNUSP[edit]

Sparkling[edit]

Spin[edit]

SPL[edit]

Stata[edit]

Mata[edit]

Suneido[edit]

Swift[edit]

Output:

Tcl[edit]

Note that it would be more normal to produce this output with:

It bears noting that the three parts of the for loop do not have to consist of 'initialize variable', 'test value of variable' and 'increment variable'. This is a common way to think of it as it resembles the 'for' loop in other languages, but many other things make sense. For example this for-loop will read a file line-by-line:

(This is a somewhat awkward example; just to show what is possible)

TI-83 BASIC[edit]

For loops in TI-83 BASIC take at least 3 arguments, with an optional fourth: For(variable,start,end[,step]. Parentheses don't need to be closed in TI-BASIC.

TI-89 BASIC[edit]

TorqueScript[edit]

TransFORTH[edit]

TUSCRIPT[edit]


TypeScript[edit]

UNIX Shell[edit]

A conditional loop, using a while control construct, can have the same effect as a for loop. (The original Bourne Shell has no echo -n '*', so this uses printf '*'.)

The Bourne Shell has a for loop, but it requires a list of words to iterate.The jot(1) command from BSD can output an appropriate list of numbers.

Bash has for loops that act like C. These loops are very good for this task.

C Shell[edit]

UnixPipes[edit]

Ursa[edit]

Vala[edit]

VBA[edit]

VBScript[edit]

Vedit macro language[edit]

Wart[edit]


x86 Assembly[edit]

This subroutine uses only the original 16-bit 8086 instruction set; it is written for DOS, but could be adapted to run under other operating systems.

XLISP[edit]

The equivalent of other languages' FOR or DO loops can be written using DO:

Output:

This construct is not, however, very idiomatic: loops in XLISP are mostly written using recursion.

XPL0[edit]

Z80 Assembly[edit]

For the Amstrad CPC (should work with e.g. the built-in assembler in JavaCPC; use call &4000 to start from BASIC):

zkl[edit]

Retrieved from 'https://rosettacode.org/mw/index.php?title=Loops/For&oldid=282490'
Categories:
< BASIC Programming
Jump to navigationJump to search

The IF..THEN..ELSEIF..ELSE control statement allows identifying if a certain condition is true, and executes a block of code if it is the case.

In some implementations of BASIC (but permitted by most versions), the IF statement may need to be contained in one line. However, ELSEIF may not be available in this case, and there is no need for an explicit END IF:

This carries over into some implementations of BASIC where if the 'IF..THEN' statement is followed by code on the same line then it is fully contained. That is, the compiler assumes the lines ends with 'ENDIF', even if it not stated. This is important when dealing with nested 'IF..THEN' clauses:

The ELSE clause, while following the 'IF 2<3' statement, is associated with the 'IF X<2' statement, because the 'IF 2<3' statement has a PRINT statement on the same line.

Retrieved from 'https://en.wikibooks.org/w/index.php?title=BASIC_Programming/Beginning_BASIC/Control_Structures/IF..THEN..ELSEIF..ELSE&oldid=3383830'
Hidden category: