1
$\begingroup$

I am trying to write a nested for loop that prints the pairs 00 through 55, with each digit running from 0 to 5.

In other words, I want the output to appear as so: 00, 01, 02, 03, 04, 05, 10, 11, ..., 50, 51, 52, 53, 54, 55.

I know I can do this using two variables, one representing the first digit and the other representing the second digit. I'm also aware of how to create a for loop that prints, for example, the digits 0-5 one time, but how do I get Mathematica to repeat this process 5 times so that the pattern of output continues up until 55?

$\endgroup$
  • $\begingroup$ Using Print in loops is the best way to freeze the front end. When you can, try to use Monitor. $\endgroup$ – Henrik Schumacher Feb 26 at 7:16
2
$\begingroup$

From my experience in Wolfram language, not only are explicit loops discouraged, but also explicit Printing is, if one uses the notebook interface. One usually collects data in a List ({...}) construct:

StringJoin @@@ Tuples[CharacterRange["0", "5"], 2]
$\endgroup$
1
$\begingroup$

It is not a good idea to use For loops, if you must use explicit loops, you can use Do or Table

Do[
 Do[
  myVar = ToString[i] <> ToString[j];
  Print[myVar]
  , {j, 0, 5}
  ],
 {i, 0, 5}
 ]

Mathematica graphics

$\endgroup$
1
$\begingroup$

You can also count in 6 digits

data = PadLeft[Table[IntegerDigits[i, 6], {i, 35}]]; Do[Print@StringJoin[ToString[#] & /@ i], {i, data}]

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.