For next loop in qbasic?
hey i am taking a beginner's course in programming and i have been asked to tackle this question using the for loop its like i have to write a program that will print the numbers as follows..
1
12
123
1234
12345
123456

how can i do it fellows ?

Dani AI

Generated

asked for an attempt; pointed to a string-based approach and showed a DO/LOOP variant. An alternative that matches the "use a FOR/NEXT" requirement is a simple nested FOR loop: the outer loop picks the row length, the inner loop prints numbers 1..row on the same line, then a blank PRINT moves to the next line.

FOR i = 1 TO 6
  FOR j = 1 TO i
    PRINT j;
  NEXT j
  PRINT
NEXT i

Explanation: the inner loop prints each number with a trailing semicolon so output stays on the same line. The empty PRINT after the inner loop advances to the next line. The upper bound 6 can be replaced by a variable (read from INPUT or set earlier) to make the program flexible.

Troubleshooting notes: if the output shows unexpected spacing, try switching to a text-concatenation approach (as suggested by ) and clear the temporary text each row before appending. If using typed numeric loop counters, there is no need for conversion functions. For students showing homework to helpers (as recommended), paste the small code tried above to get targeted tweaks.

When asking for help with homework, it's a good idea to explain what you've tried.

Here you want to use a for/next loop to print "the numbers". So show us some QBasic code that you wrote to do it (surely the loop itself cannot be all that mysterious).

Then it will be possible for readers to chime in with suggestions on the part that you are having difficulty with (without rehashing the proper syntax for easy things like a for/next loop that would be in your documentation).

You want to treat this exercise as in strings rather than numbers. Take each successive occurrence of your loop variable, as a string, and add (append) it to a string which you have previously set to null. I leave the actual coding to you, but you will have to trim off the leading space which the STR$ function adds.

Here's a version in a DO:Loop for you to convert if you still need it.

DO

  '// If you dont want the spaces us LTRIM$
  NumberString$ = NumberString$ + LTRIM$(STR$(i%))

  '// If you do want the spaces, use this version
  'NumberString$ = NumberString$ + STR$(i%)

  PRINT NumberString$
  i% = i% + 1

LOOP UNTIL i% = 10
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.