Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Your very own "for" instruction

Assuming you have the following input : a, b, c, d

Input can be in one-line using any format "a/b/c/d" or "a,b,c,d" etc..

You can also have 4 inputs.

You must code the following behaviour (pseudo-code here) :

var i = <a>
while (i <b> <c>)
    print i
    i = i + <d>
    print "\n"

Here are some tests cases :

input : 1,<,10,1
output :
1
2
3
4
5
6
7
8
9

One more :

input : 20,>,10,1
output :
20
21
22
23
24
25
26
27
...
infinite loop / program crashes
  • a is an integer, the initial value of i.

  • b is a string or a char, it can't be something else, the comparator used in the ending condition of the for loop.

    b can and must be one of the following strings :

    - ">"
    - "<"
    
  • c is an integer, the number used in the ending condition of the for loop.

  • d is an integer that is added to i at every loop.

This is code-golf, the shortest answer wins !

share|improve this question
1  
Can the numbers be returned from a function as a list/sequence, rather than printed to stdout? – smls 2 days ago
    
@smls Nope sorry, the output must be like the examples ! – Sygmei 2 days ago
1  
It says my code should follow the pseudo-code and there's a print "\n", but I am using javascript's alert for each line. Would that be acceptable, or would I have to use console.log instead making my answer longer? – Masterzagh yesterday
1  
You can use the alert function as a way to ouput but you can't use multiple alerts. Something like alert("23\n24\n25"); would work whereas alert("23"); alert("24"); alert(25); wouldn't – Sygmei yesterday

36 Answers 36

JavaScript (ES6),  44  43 56 bytes

Saved 1 byte thanks to ETHproductions
Edit: fixed to comply with output requirements

(a,b,c,d)=>{for(s='';eval(a+b+c);a+=d)s+=a+`
`;alert(s)}

Test

let f =

(a,b,c,d)=>{for(s='';eval(a+b+c);a+=d)s+=a+`
`;alert(s)}

f(1,'<',7,2)

share|improve this answer
    
Nice use of scope! – ETHproductions 2 days ago
    
I think you can rearrange the eval to save a byte: (a,b,c,d)=>{for(;eval(a+b+c);a+=d)alert(a)} – ETHproductions 2 days ago
    
@ETHproductions Ah, yes. Nice one! – Arnauld 2 days ago
3  
That's a 44 with a tutu! – aross 2 days ago
    
This doesn't follow the specification where output is line-by-line with U+000A after every line. – Joey yesterday

Javascript (ES6), 47 42 48 Bytes

Wanted to make the for version but someone was faster, so here's the recursive version.

(b,c,d)=>F=a=>eval(a+b+c)&&console.log(a)|F(a+d)

You need to add f= before and call it like f(b,c,d)(a).

Many thanks to Arnauld for the awesome golf.

alert changed to console.log because of output specification

share|improve this answer
    
@Arnauld Thanks, that's a pretty cool golf. I just asked him, so let's see if he accepts it. – Masterzagh 2 days ago
    
Glad to see it accepted. ;) – Arnauld 2 days ago
    
This doesn't follow the specification where output is line-by-line with U+000A after every line. – Joey yesterday
    
@Joey That's just pseudo-code, but I'll ask OP about this. – Masterzagh yesterday
    
@Masterzagh: There was a question about alternative output formats already which was denied. – Joey yesterday

Python 2, 50 bytes

a,b,c,d=input()
while eval(`a`+b+`c`):print a;a+=d

Try it online!

share|improve this answer

Jelly, 12 bytes

Ṅ+⁶µ⁴;⁵¹vµ¿t

Try it online!

Jelly has a lot of ways to tersely do iteration, create ranges, etc.. However, mirroring C++'s behaviour exactly is fairly hard, due to special cases like the increment being 0, the loop ending before it starts (due to the inequality being backwards), and the increment going in the wrong direction (thus meaning the exit condition of the loop can't be met naturally). As such, this solution is basically a direct translation of the C++, even though that makes it rather more low-level than a Jelly program normally is. Luckily, C++ has undefined behaviour on signed integer overflow (the question uses int), meaning that a program can do anything in that case, and thus there's no need to try to mimic the overflow behaviour.

Explanation

Ṅ+⁶µ⁴;⁵¹vµ¿t
   µ     µ¿   While loop; while ((⁴;⁵¹v) counter) do (counter = (Ṅ+⁶)counter).
    ⁴;⁵       Second input (b) appended to third input (c), e.g. "<10"
        v     Evaluate, e.g. if the counter is 5, "<10" of the counter is true
       ¹      No-op, resolves a parser ambiguity
Ṅ             Output the counter, plus a newline
 +⁶           Add the fourth input (d)
           t  Crashes the program (because the counter is not a list)

Crashing the program is the tersest way to turn off Jelly's implicit output (otherwise, it would output the final value of the counter); it generates a bunch of error messges on stderr, but we normally consider that to be allowed.

Incidentally, the loop counter is initialised with the current value before the loop starts. As the loop appears at the start of the program, that'll be the first input.

share|improve this answer
    
You could change t to to have no crash. The dequeue results in an empty list for which Jelly's implicit print yields nothing. – Jonathan Allan 2 days ago
    
@JonathanAllan: It doesn't, what it actually does is to create a range from 2 to the given value, which is definitely visible on an implicit print. – ais523 2 days ago
    
Ah, I must've tested that theory with a loop ending in negative territory; indeed a range is implicitly created. – Jonathan Allan 2 days ago
    
Uhm, this is 12 characters, but it's not 12 bytes right? – Cruncher yesterday
    
@Cruncher: Jelly uses its own encoding in which each character used by the language is represented by a single byte (it only uses 256 different characters). The reason it doesn't use something better-known like code page 437 is to make it easier to type (I mean, it's not that easy to type, but it's easier than a language like gs2 would be). A hexdump of this program would be 12 bytes long. – ais523 yesterday

R, 63 bytes

function(a,b,c,d)while(do.call(b,list(a,c))){cat(a,"\n");a=a+d}
share|improve this answer

Pure bash, 35

I assume its OK just to plug the parameters into the standard for loop:

for((i=$1;i$2$3;i+=$4));{ echo $i;}

Try it online.

share|improve this answer
    
Haha, bash makes it really easy – Sygmei 2 days ago

Java, 67

(a,b,c,d)->{for(int i=a;b=='>'?i>c:i<c;i+=d)System.out.println(i);}
share|improve this answer
10  
Is there a reason to create i? Could you skip the initialization part and just use a? Also, using the ASCII value of '>' (62) saves a byte. – Riley 2 days ago
4  
Following Riley's comment, you can do b>61 – Kritixi Lithos 2 days ago
    
I don't believe this compiles. – ChiefTwoPencils 11 hours ago

05AB1E, 22 20 bytes

[D²`'>Q"‹›"è.V_#D,³+

Try it online!

Explanation

[                       # start loop
 D                      # copy top of stack (current value of a)
  ²`                    # push b,c to stack
    '>Q                 # compare b to ">" for equality
       "‹›"             # push this string
           è            # index into the string with this result of the equality check
            .V          # execute this command comparing a with c
              _#        # if the condition is false, exit loop (and program)
                D,      # print a copy of the top of the stack (current value of a)
                  ³+    # increment top of stack (a) by d
share|improve this answer
1  
Any input format is accepted so the second version is okay :) – Sygmei 2 days ago

SmileBASIC, 53 bytes

INPUT A,B$,C,D
S=ASC(B$)-61WHILE S*A>S*C?A
A=A+D
WEND

Explanation:

INPUT A,B$,C,D
IF B$=="<" THEN S=-1 ELSE S=1 'get comparison direction
I=A
WHILE S*I>S*C 'loop while I is less than/greater than the end
 PRINT I
 INC I,D
WEND

This uses the fact that X<Y is the same as -X>-Y

share|improve this answer
    
I'll trust you for this one, I don't have a 3DS to test :) – Sygmei 2 days ago
    
I have Petit Computer, so cool idea! I will try something like this sometime... – python-b5 2 days ago
    
You could use a READ statement, saving 1 byte. – ckjbgames 2 days ago
    
@ckjbgames how? – 12Me21 2 days ago
    
@12Me21 Check the SmileBASIC manuals. It should be in the list of instructions for SmileBASIC. – ckjbgames 2 days ago

Stacked, 34 bytes

@d@c@b[show d+][:c b tofunc!]while

Try it online! (Testing included.) This is a function that expects the stack to look like:

a b c d

For example:

1 '<' 10 2
@d@c@b[show d+][:c b tofunc!]while

Explanation

@d@c@b[show d+][:c b tofunc!]while
@d@c@b                               assign variables
               [............]while   while:
                :c                   duplicate "i" and push c
                   b tofunc!         convert b to a function and execute it
      [.......]                      do:
       show                          output "i" without popping
            d+                       and add the step to it
share|improve this answer

C++, 80

Whoops, this is C++ not C. Was a bit confused by the question.

void f(int a,char b,int c,int d){for(;b==62?a>c:a<c;a+=d)cout<<a<<endl;}
share|improve this answer
    
Is this C or C++? – betseg 2 days ago
7  
Which implementation of C++? (I'm curious how you're getting something akin to using namespace std for free). – H Walters 2 days ago
    
Doesn't i have to start at a, not 0? You can just use a and skip i altogether and use the ASCII value of '>'. for(;b==62?a>c:a<c;a+=d) – Riley 2 days ago
    
Doesn't work for f(1,'<'3,1); – Roman Gräf 2 days ago
    
Ack... yeah, requires the math on both sides; for(b-=61;b*a>b*c;a+=d) works for a single byte; but so does for(;b-62?a<c:a>c;a+=d). – H Walters 2 days ago

Python 3, 52 bytes

def f(a,b,c,d):
 while[a>c,a<c][b<'>']:print(a);a+=d

repl.it

share|improve this answer
    
Clever use of lists ! – Sygmei 2 days ago

Pip, 14 bytes

W Va.b.ca:d+Pa

Takes four command-line arguments. Supports negative & floating point numbers and comparison operators < > = <= >= !=. Try it online!

                a,b,c,d are cmdline args
W               While loop with the following condition:
  Va.b.c          Concatenate a,b,c and eval
            Pa  Print a with newline (expression also returns value of a)
        a:d+    Add d to that and assign back to a
share|improve this answer

Jelly, 8 bytes

ḢṄ+⁹;µV¿

This is a dyadic link that takes a,b,c as its left argument and d as its right one. Output may be infinite and goes to STDOUT.

Try it online!

How it works

ḢṄ+⁹;µV¿  Dyadic link.
          Left argument:  a,b,c (integer, character, integer)
          Right argument: d     (integer)

       ¿  While...
      V     the eval atom applied to a,b,c returns 1:
     µ       Combine the links to the left into a chain and apply it to a,b,c.
Ḣ              Head; pop and yield a from a,b,c.
 Ṅ             Print a, followed by a linefeed.
  +⁹           Add a and the right argument (d) of the dyadic link.
    ;          Concatenate the result and the popped argument of the chain,
               yielding a+d,b,c.
share|improve this answer
    
Command-line arguments use Python syntax and cannot distinguish between a character and a singleton string. If you want to use CLAs, you have to insert an F to flatten the array. – Dennis 2 days ago
1  
Now I want to delete half my comment as it's obsolete, whilst keeping the other half. I guess I'll just repeat the relevant half and delete the rest: "Oh, bleh, you defined it as a function so you could disregard the implicit output under PPCG rules. I should have thought of that." – ais523 2 days ago

Python 3, 57

Recursive solution.

f=lambda a,b,c,d:eval('a%sc'%b)and[print(a),f(a+d,b,c,d)]

We can use f-strings in Python 3.6 and newer: f'a{b}c' (same number of bytes).

share|improve this answer

C, 52 51 bytes

-1 byte thanks to H Walters

f(a,b,c,d){for(;b&2?a>c:a<c;a+=d)printf("%d\n",a);}

Try it online!

share|improve this answer
1  
Sorry for the mistake in the pseudo-code, i increments after each print :) – Sygmei 2 days ago
1  
Use b&2 instead of b^60 for another byte. – H Walters 2 days ago

Haskell, 66 64 bytes

f a b c d|last$(a<c):[a>c|b>"<"]=print a>>f(a+d)b c d|1<3=pure()

Try it online! Usage:

Prelude> f 0 "<" 9 2
0
2
4
6
8
share|improve this answer

Python 2, 45 bytes

exec"i=%d\nwhile i%c%d:print i;i+=%d"%input()

Try it online!

A very literal implementation of the spec. Takes the code template, substitutes in the inputs via string formatting, and executes it.

share|improve this answer

Bash (+Unix Tools), 29 bytes

Golfed

bc<<<"for(x=$1;x$2$3;x+=$4)x"

Test

./forloop 1 '<' 10 1
1
2
3
4
5
6
7
8
9
share|improve this answer
1  
Ha. I was just about to post the exact same thing! +1 – Digital Trauma 2 days ago

Plain TeX, 88 bytes

\newcount\i\def\for#1 #2 #3 #4 {\i#1\loop\the\i\endgraf\advance\i#4\ifnum\i#2#3\repeat} 

The command \for provides the requested function. Save this as for.tex and then run it and enter the variable values at the command line: pdftex '\input for \for 1 < 5 1 \bye' The variable values must be separated by spaces.

share|improve this answer

Common Lisp, 82 80 79 73 bytes

(lambda(a b c d)(do((i a(+ i d)))((not(funcall(intern b)i c)))(print i)))

Test

(funcall * 1 "<" 10 1)

1 
2 
3 
4 
5 
6 
7 
8 
9 
NIL
CL-USER> 

Ungolfed (pretty-printed)

(LAMBDA (A B C D)
  (DO ((I A (+ I D))) ((NOT (FUNCALL (INTERN B) I C)))
    (PRINT I)))

  • "loop for" can be written "loop as", 1 byte!
  • "do" shorter than "loop"
share|improve this answer

Perl 6, 44 bytes

{.say for $^a,*+$^d...^*cmp$^c!= $^b.ord-61}

How it works

{                                          }  # A lambda.
          $^a                                 # Argument a.
             ,*+$^d                           # Iteratively add d,
                   ...^                       # until (but not including the endpoint)
                       *cmp$^c                # the current value compared to c
                                              # (less=-1, same=0, more=1)
                              != $^b.ord-61.  # isn't the codepoint of the b minus 61.
 .say for                                     # Print each number followed by a newline.

If it's okay to return a (potentially infinite) sequence of numbers as a value of type Seq, instead of printing the numbers to stdout, the .say for part could be removed, bringing it down to 35 bytes.

share|improve this answer

Clojure, 66 63 bytes

#(when((if(= %2"<")< >)% %3)(println %)(recur(+ % %4)%2 %3 %4))

-3 bytes by factoring out the loop. I'm "abusing" the init parameter to act as the running accumulator.

Recursive solution (with TCO). See comments in pregolfed code. I tried a non-TCO recursive solution, and it ended up being 67 bytes.

I'd love to see this beat in Clojure! I think this is the smallest I can get it.

(defn my-for [init-num com-str com-num inc-num]
  (let [op (if (= com-str "<") < >)] ; Figure out which operator to use
    (when (op init-num com-num) ; When the condition is true, print and recur
      (println init-num)
      (recur (+ init-num inc-num) com-str com-num inc-num))))
    ; Else, terminate (implicit) 
share|improve this answer
    
Oh I didn't notice this answer. #(when(({">">"<"<}%2)% %3)(println %)(recur(+ % %4)%2 %3 %4)) would be 61 bytes, combining your when with my ({">">"<"<}%2). – NikoNyrh 2 days ago

Groovy, 51 bytes

{a,b,c,d->while(Eval.me("$a$b$c")){println a;a+=d}}

This is an unnamed closure. Try it Online!

Caution - If you want to test this with groovy console, make sure you kill the entire process when the input causes an infinite loop. I noticed this after it consumed ~5 gigs of RAM.

share|improve this answer

QBIC, 51 40 bytes

:;::{?a┘a=a+c~A=@<`|~a>=b|_X]\~a<=b|_X

And three minutes after posting I realised I could simplify the terminator logic...

:;::      Consecutively read a, A$, b and c from the command line
{?a┘      Start an infinite loop; print a, add a newline to the source
a=a+c     increment a
~A=@<`|   If we are in LESS THAN mode
  ~a>=b   and IF we are no longer LESS
    |_X]  THEN QUIT, end if.
  \       ELSE (we're in GREATER THAN mode)
    ~a<=b IF we are no longer GREATER
    |_X   THEN QUIT
          The last IF and the loop are auto-closed
share|improve this answer

Batch, 94 bytes

@set i=%1
@set o=gtr
@if "%~2"=="<" set o=lss
:g
@if %i% %o% %3 echo %i%&set/ai+=%4&goto g

If it wasn't for the second parameter behaviour, it could be done in 53 bytes:

@for /l %%i in (%1,%4,%n%)do @if not %%i==%3 echo %%i

This simply does nothing if the step has the wrong sign. The extra test is because Batch's for loop allows the loop variable to equal the end value.

share|improve this answer

Clojure, 66 bytes

#(loop[i %](if(({">">"<"<}%2)i %3)(do(println i)(recur(+ i %4)))))

This could have been 55 bytes as< and > are functions in Clojure:

(def f #(loop[i %](if(%2 i %3)(do(println i)(recur(+ i %4))))))
(f 1 < 10 1)
share|improve this answer
    
I like the use of the map here. I would have never thought that that would have beaten my way. Also interesting that both of our initial counts were the same, despite slightly different approaches. – Carcigenicate 2 days ago
    
Allowing b to be a function would give an unfair advantage to some languages :) – Sygmei 2 days ago
    
True, but I think most languages I know of wouldn't benefit much from allowing < instead of "<", except Clojure. – NikoNyrh 2 days ago
    
@Sygmei True. It would be freakin sweet though. Can't blame you making that call. – Carcigenicate 2 days ago
    
OP said characters are fine instead of strings for the comparison operators btw. That should save a couple bytes. – Carcigenicate 2 days ago

PHP, 69 bytes

for($i=($a=$argv)[1];$a[2]<"="?$i<$a[3]:$i>$a[3];$i+=$a[4])echo"$i
";

Run with '-r'; provide command line arguments as input.

For just one byte more, I can take every operator:

for($i=($a=$argv)[1];eval("return $i$a[2]$a[3];");$i+=$a[4])echo"$i
";

Yeah, evil eval. Did you know that it can return something?

share|improve this answer
    
Neat ! I wasn't sure when creating the challenge if I should include every common operators, then I remembered that they aren't all the same (~= for != in Lua for example) – Sygmei 2 days ago
    
Woah, eval IS evil. – cyberbit yesterday

TI-Basic, 41 34 bytes

Prompt A,Str2,Str3,D
While expr("A"+Str2+Str3
Disp A
A+D->A
End
share|improve this answer
1  
The way that a TI calculator works, many symbols are stored as a single byte. Prompt , Str2, Str3, While , expr(, Disp , ->, and End are all single-byte symbols. I count 29 bytes. – Pavel 2 days ago
    
@Pavel Thanks for your interest! Although it's true that TI-Basic is tokenized, not all tokens are one byte. For example, Str2, Str3, and expr( are all two-byte tokens. To see a list of one-byte tokens, check out tibasicdev.wikidot.com/one-byte-tokens – Timtech 2 days ago

Ruby, 43 41 bytes

->a,*i,d{a=d+p(a)while eval"%s"*3%[a,*i]}

If b can be taken in as a Ruby symbol instead of a string, you get 38 bytes:

->a,b,c,d{a=d+p(a)while[a,c].reduce b}

Try either solution online!

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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