183

Commodore 64 BASIC 40 print "Line 1" 30 print "Line 2" 20 print "Line 3" 10 print "Line 4"


109

PHP Abusing precedence... :-) !print "Line1\n". !print "Line2\n". !print "Line3\n". !print "Line4\n";


76

C Undefined behavior is the most exciting kind of behavior! f(){} main() { f(printf("Line 1\n"), printf("Line 2\n"), printf("Line 3\n"), printf("Line 4\n")); } Actual output may vary depending on your compiler, linker, operating system, and processor :)


74

Java Using reflection public class ReversePrint { public static void main(String[]a) { System.out.println("Line1"); System.out.println("Line2"); System.out.println("Line3"); System.out.println("Line4"); } static { try{ Field f=String.class.getDeclaredField("value"); f.setAccessible(...


70

C (and sort-of Python) New version, using a macro to fit the question format perfectly. Following Quincunx's comment, I added return to make it nicer. It also works in Python, but it prints in correct order. #define print"\n",printf( #define return"\n"))));} #define def main(){0? def main(): print "Line 1" print "Line 2" print "Line 3" ...


61

ES6 (using backwards mode ;) Wow, it looks like the designers of ECMAScript had some incredible foresight when they made backwards mode part of the spec: // activate backwards mode: 'use backwardsˈ; \* mode backwards in now *\ code of lines some ⧵\ \*code*\ "Line1" print \*code*\ \*code*\ "Line2" print \*code*\ \*code*\ "Line3" print \*code*\ \*code*\ "...


59

C main() { int i = 0; for(; i == 0; printf("Line 1\n")) for(; i == 0; printf("Line 2\n")) for(; i == 0; printf("Line 3\n")) for(; i == 0; printf("Line 4\n")) i = 1; }


56

Ruby print 'Line1' unless print 'Line2' unless print 'Line3' unless print 'Line4' Edit: Alternatively, def method_missing(meth,*) puts meth.to_s.sub('print'){} end printLine1( printLine2( printLine3( printLine4)))


49

PHP I know, this is madness... goto d; a: print "Line1\n"; goto end; b: print "Line2\n"; goto a; c: print "Line3\n"; goto b; d: print "Line4\n"; goto c; end: exit;


41

Haskell This is almost idiomatic Haskell, as the program now looks like a right-to-left function composition. If the function wasn't print, but something that would return a (useful) value the operator declaration would be unnecessary and the code would be something you'd see in libraries. a << b = (const a =<< b) main = putStrLn "Line1" &...


40

Perl use threads; $a=threads->create(sub {sleep(5); print("Line1\n");}); $b=threads->create(sub {sleep(4); print("Line2\n");}); $c=threads->create(sub {sleep(3); print("Line3\n");}); $d=threads->create(sub {sleep(2); print("Line4\n");}); $a->join(); $b->join(); $c->join(); $d->join();


37

HTML + CSS <p>Line 1</p> <p>Line 2</p> <p>Line 3</p> <p>Line 4</p> CSS: body {margin-top:7em} p + p {margin-top:-4em} See jsFiddle. Edit: To conform to the rules better, here is a variant in XML, that actually uses print. <?xml version="1.0" encoding="UTF-8"?> <?xml-stylesheet href="style.css"?&...


33

We can get rid of a=1 by moving it into another scope: #include <iostream> main() { int a=0;if(0) const int a=1; const int b=2; const float c=0.5; std::cout << a/b-a*c; } This is I think 13 characters. Or better yet get a new a that also results in 0: #include <iostream> int main() { const int a=1; const int b=2; ...


31

Here, another try: class Foo { //static Foo foo = new Foo(); // you are not allowed using this approach //static readonly Foo foo = new Foo(); // you are not allowed using this approach Foo foo = new Foo(); MWAHAHAHA, THIS LINE GIVES A COMPILE ERROR! NO STACKOVERFLOW EXCEPTION ANYMORE! LOL } class Program { static void Main(string[] args)...


30

C++ This is trivial using the right tools. #include <iostream> using namespace std; class Obj { public: int state; Obj& operator= (Obj& foo) { foo.state++; this->state = foo.state - 2; return *this; } }; int main() { Obj a, b, c, d; a.state = 3; b.state = 4; cout << a.state << " " <...


28

Yay, found it! public class Animal { public class Giraffe { } // 1 } public class Giraffe : Animal // 2 { public bool Test() { return this is Giraffe; } } Since Giraffe 1 is a member of Animal, and Giraffe 2 is one level further out, the name Giraffe in the is test refers to the former (section 7.6.2 in the C# 5 spec). Visual ...


25

How about this? class Foo { //static Foo foo = new Foo(); // you are not allowed using this approach //static readonly Foo foo = new Foo(); // you are not allowed using this approach String str = @" Foo foo = new Foo(); "; } class Program { static void Main(string[] args) { new Foo(); } }


24

#include <iostream> main() { const int a=1; #define a 0 const int b=2; const float c=0.5; std::cout << a/b-a*c; } 1 new line, 12 new chars


24

Another Solution This is, in my opinion, one of the most interesting problems on the site. I need to thank deadcode for bumping it back up to the top. ^((^|xx)(^|\3\4\4)(^|\4x{12})(^x|\1))*$ 39 bytes, without any conditionals or assertions... sort of. The alternations, as they're being used (^|), are a type of conditional in a way, to select between "...


24

C#, 46 (88 including boilerplate) using System;class P{static void Main(){ try { while(true) try { System.Threading.Thread.CurrentThread.Abort(); } catch(Exception ex2) { } } catch(Exception ex1) { // your goal ...


23

So, #define a 0, Done. I saw that was posted - unsurprisingly. Surprisingly, this wasn't posted: #include <iostream> main() { const int a=1; const int b=2; const float c=0.5; std::cout<<0|| std::cout << a/b-a*c; } 14 chars That should do, right?


23

C++ #include <iostream> #define Q(x,y) x ## y #define P(x,y) Q(x, y) #define print S P(s, __LINE__) = struct S { const char *s; S(const char *s): s(s) {} ~S() { std::cout << s << std::endl; } }; int main() { print "Line1"; print "Line2"; print "Line3"; print "Line4"; } (Local variables are destroyed in reverse order of ...


22

This (ir)regular expression seems to work. ^((?(1)((?(2)\2((?(3)\3((?(4)\4x{24}|x{60}))|x{50}))|x{15}))|x))*$ This regex is compatible with PCRE, Perl, .NET flavors. This basically follows a "difference tree" (not sure if there's a proper name for it), which tells the regex how many more x's to match for the next fourth power: 1 16 81 256 625 ...


21

#include <iostream> main() { const int a=0;//\ const int a=1; const int b=2; const float c=0.5; std::cout << a/b-a*c; } 17 chars. By the way, the original program doesn't compile under MSVC, which complains that main doesn't have a return type.


21

Haskell main = sequence_ $ reverse [ putStr "Line1", putStr "Line2", putStr "Line3", putStr "Line4"]


21

Javascript setTimeout(function(){console.log("Line 1");},900); setTimeout(function(){console.log("Line 2");},800); setTimeout(function(){console.log("Line 3");},700); setTimeout(function(){console.log("Line 4");},600);


20

C Trying to make defiance of the tips in the question as creative as possible: #include <stdio.h> #define print if (i == __LINE__) puts static unsigned i; int main(void) { while (--i) { print("Line 1"); print("Line 2"); print("Line 3"); print("Line 4"); } return 0; }


20

C# 24 Characters terminates the inner try block before intended, allowing me to cause an exception outside of the try block. }finally{int a=1/0;}try{


20

static void Main(string[] args) { bool a, b; unsafe { int* pa = (int*)&a; int* pb = (int*)&b; *pa = 1; *pb = 2; } Console.Write(Test(a, b)); } This prints True for me with the C# implementation that comes with Visual Studio 2015. I actually don't know any C#, but I figured I'd try to write ...


17

52 characters }static Program(){System.Console.Write(0<1);for(;;); so the whole thing becomes: class Program { static void Main() { System.Console.Write( "False" ); } static Program() { System.Console.Write( 0 < 1 ); for ( ; ; ) ; } }


Only top voted, non community-wiki answers of a minimum length are eligible