0

In this question, it was mentioned that one can output the concatenation of a string and a variable by using StringForm[]:

var1 = "Tree";
var2 = 10;

StringForm["The `1` is about `2` feet tall...", var1, var2]

(* "The Tree is about 10 feet tall..." *)

This works. However, when I try to use it within a loop, I get no output whatsoever:

For[i = 1, i < 10, i++,
(
   var1 = "Tree";
   var2 = 10;

   StringForm["The `1` is about `2` feet tall...", var1, var2]
)]

I don't understand why there is no output here as I am just repeating the same operation $i$ times. What am I missing?


EDIT: The aim is to perform a task within the For loop, and print some intermediary results every $n$ iterations.

Klangen
  • 1,009
  • 5
  • 14
  • Use Table. For does not return anything. – Kuba Oct 26 '18 at 12:45
  • @Kuba Thank you for your comment. What about the case where a bunch of operations are being executed within the For loop, and some intermediary results must be printed out every $n$ iterations? – Klangen Oct 26 '18 at 12:46
  • Only printed or collected? Use Print or for the latter case Sow/Reap. At the end you can have Table to manage your loop and Table[proc; string, {i, 10}] or something. Assuming string generation is the last step only. – Kuba Oct 26 '18 at 12:48
  • Thank you. I will try to use Table. In the meantime, can you please explain why a perfectly valid output does not output when placed within a For loop? As a software developer, this makes no sense to me. – Klangen Oct 26 '18 at 12:56
  • 2
    For evaluates expression in a loop but does not return anything. Here's more about that: https://mathematica.stackexchange.com/a/124077/5478 – Kuba Oct 26 '18 at 13:00
  • Thank you. I will read that – Klangen Oct 26 '18 at 13:08
  • @Flermat this questions was closed as duplicate and now it has been flagged for reopening. Does the question linked as duplicate answer your question or not? If it doesn't, [edit] your question and explain why so we can reopen it and get the answer you need. – rhermans Oct 26 '18 at 15:04
  • @rhermans Yes it answers my question, thank you. You can close this question. – Klangen Oct 28 '18 at 08:41

0 Answers0