At runtime, keep prompting for a line of input until the user inputs something (other than an empty newline), i.e. does not just press Enter or OK. Output or result is neither required nor prohibited.

Pseudo-code 1

myform = new form("GUI")
myform.mytxt = new editfield("")
myform.ok = new button("OK")
repeat
  waitfor(myform.ok,"click")
until myform.mytxt.content <> ""

Pseudo-code 2

LET TEXT = ""
WHILE TEXT = "" DO
  TEXT = PROMPT("")
ENDWHILE

Example 1

Program runs and immediately pops up a form with a single text field and an OK button.
User clicks the OK button.
Nothing happens.
User pastes "hello world" into the text field and clicks the OK button.
Program terminates.

Example 2

Function is called and immediately displays a blank line and a blinking cursor.
User presses Enter.
Cursor moves down one line.
User presses Enter.
Cursor moves down one line.
User presses PPCGEnter
Function returns.

share|improve this question
    
is it allowed to count space-only lines as empty? – 12431234123412341234123 6 mins ago
    
@12431234123412341234123 No. – Adám 2 mins ago

23 Answers 23

sed, 4

/./q

Waits for a line of input that has 1 or more characters, then quits.

Try it online. But it works better on a live shell:

sed '/./q'
share|improve this answer

JavaScript, 37 22 17 bytes

while(!prompt``);

while(!prompt``);

Explanation

The while keyword starts the while loop. In the condition of the loop, !prompt`` asks for input and checks whether it is given or not. If it isn't given, the body of the loop is executed, which in our case is empty, then the interpreter goes back to the loop condition. The same process happens over and over again until the user gives the input.

share|improve this answer
    
while(""==prompt("")); – Adám 4 hours ago
    
@Adám Wow. I was just thinking about how to make the solution smaller, and you came up with an answer thanks! – Arjun 4 hours ago
    
It seemed obvious. However, my browser console refuses to execute without a trailing ;, while your initial expression worked fine without ;. Any idea why? – Adám 4 hours ago
    
No, Chrome. So if any JavaScript engine can handle omission of the current expression's ;, you can save that byte :-) – Adám 4 hours ago
1  
@Adám Just figured out the real cause of the error produced after the removal of semicolon from the source code. The reason why that happens is that while loop expects a statement (or a group of statements) as the loop body after the loop condition. Adding the semicolon makes it think that the loop body is over. However, removing the semicolon causes it to expect the next statement as the loop body. But in our case, no new statements comes after the loop condition. So, it throws an error. I hope it is clear now. I am terrible at explaining stuff!! :p – Arjun 3 hours ago

Python 3, 18 bytes

while''==input():0

Try it online!

share|improve this answer
4  
So, you'll keep adding another language every 9 minutes? – Adám 4 hours ago
    
My solution beats it by the tiniest possible margin! – Arjun 3 hours ago
    
@Arjun You can't outgolf Dennis: He submitted the first (3-byte) solution. – Adám 1 hour ago

Perl 5, 8+1 (-p or -n flag) bytes

/./&&die

Takes input from stdin and dies as soon as the regex matches anything except a newline.

share|improve this answer

C# (.NET Core), 72 bytes

using System;class C{static void Main(){for(;Console.ReadLine()!="";);}}

Try it online!

share|improve this answer
1  
You can save 6 bytes by writing System.Console.ReadLIne directly and drop the using-Statement. – raznagul 2 hours ago

brainfuck, 26 bytes

+[>,----------[[-]<[-]>]<]

Try it online!

share|improve this answer
    
That TIO link is all nice and well, but it is a bit hard to tell what happens when run… ;-) – Adám 3 hours ago
1  
@Adám you can append ,. to the end to verify. – Leaky Nun 3 hours ago

Java, 55 bytes

void f()throws Exception{while(System.in.read()==10);}}

If I remember correctly (it's been a while since I've been active on PPCG), my program can just be a function.

This is system specific; it only works on systems where the end-of-line character is a single newline. If the end-of-line character is instead a carriage return, replace the 10 with 13. On Windows, this doesn't work, as end-of-line on windows is \r\n.

This makes use of the fact that I can read directly from System.in.

share|improve this answer

Jelly, 3 bytes

ɠṆ¿

Not much to look at on TIO, I'm afraid.

Try it online!

How it works

This is a niladic program, meaning that it takes no input arguments. The implit argument and return value are both 0 in this case.

¿ (while) is a quick that pops two links from the link stack: a condition () and a body.

is a monadic atom: flat logical NOT. If the previous return value is falsy (here, 0 or an empty string), it returns 1 and the body is called.

ɠ is a niladic atom; it reads a raw line from STDIN and returns the result.

Once ɠ reads a non-empty line, returns 0 and we break out of the loop.

share|improve this answer
    
That fast, eh? Ah, well. – Adám 4 hours ago
    
@Adám Fast? That's horrendously obvious! I'm surprised it took so long to post. – Erik the Outgolfer 1 hour ago

Pyth, 3 bytes

W!w

Try it online!

Translated to Python:

while Pnot(input()):
 pass
share|improve this answer

Braingolf, 8 bytes [non-competing]

1[{?:+|]

Non competing because {, which reads input from STDIN, was added to braingolf after this challenge's post date.

Explanation:

1         Pushes 1 to the stack
 [        Begins while loop
  {       Reads from STDIN, returns -1 if no input given
   ?      Checks if last item in stack is > 0
    :     Else
     +    Adds the last 2 items together, however there is only 1 item, so add it to itself
      |   Endif
       ]  End While, decrement first item in stack, or exit if first item is 0
share|improve this answer
    
Haha, you implemented input an hour after the challenge was posted – ASCII-only 2 hours ago
    
@ASCII-only had to do it eventually, haha – Mayube 1 hour ago

HTML 5, 33 bytes

<form action=y.p><input required>

<form action=y.p><input required>

Explanation

The required attribute on the <input> causes the browser to inform the user "this field is required"-sort-of-message, if they do not enter a value. However, when they enter a value, the value is sent to the URL of the action attribute of the <form> (which in our case is "y.p", an imaginary file). So, the browser is redirected to "y.p" file, causing our HTML program to terminate.


Note: It doesn't matter whether "y.p" is available in the same directory. If it isn't, a 404 error is output by the browser. If it is, then it is displayed. Both of them cause our HTML program to terminate.

Another Note: This should work in any HTML5 compatible browser.

share|improve this answer
    
action=/ might work on some browsers. – Neil 1 hour ago

Charcoal, 4 bytes

W¬Sω

Explanation

W      While
  ¬S   Logical not of string input
     ω  Print ""
share|improve this answer

Java, 128 126 bytes

2 bytes thanks to Kevin Cruijssen.

import java.util.*;class a{public static void main(String[]a){for(Scanner s=new Scanner(System.in);s.nextLine().isEmpty(););}}

Try it online!

share|improve this answer
    
You can golf it by two bytes: Change the while to a for and put the Scanner s=new Scanner(System.in); inside it. And change the .equals("") to .isEmpty(). – Kevin Cruijssen 2 hours ago

05AB1E, 5 bytes

[IõÊ#

Explanation:

[     Start infinite loop
 I    One line of input
   Ê  Not equal to
  õ   Empty string
    # Break if true

Try it online!

share|improve this answer

C, 52 bytes, 33 bytes, 29 bytes

-19 bytes thanks to Justin

-4 bytes thanks to Christoph

main(){while(getchar()==10);}

10 is equal to '\n' - In case this isn't obvious.

share|improve this answer
1  
You can golf it by 1 char by using a ; instead of the {} for the while loop – Justin 2 hours ago
1  
Although not standards compliant, you can remove the #include<stdio.h> if you switch to while(getchar()==10): int main(){while(getchar()==10);}. – Justin 2 hours ago
2  
@Justin main(){while(getchar()==10);} is enough no need for default int. – Christoph 1 hour ago

QBasic, 34 bytes

INPUT A$
WHILE A$=""
INPUT A$
WEND

I think nothing can be more self-explanatory than that. To test, go to this website and copy-paste the following code in their text-editor :

10 INPUT A$
20 WHILE A$=""
30 INPUT A$
40 WEND

The reason why line numbers are required is that their website only supports unstructured BASIC.

share|improve this answer
    
Why the final END? – Adám 4 hours ago
    
@Adám The final END is required to terminate the program when the interpreter comes out of the while loop (WEND is to terminate While Loop) – Arjun 4 hours ago
    
So what happens if there is no more program to execute, even though END hasn't been reached? – Adám 4 hours ago
    
@Adám According to the standard, a program must have an END statement as the last statement. However, many interpreters don't require it. But I will keep it just for the sake of compatibility. – Arjun 4 hours ago
    
This has been discussed on Meta, concluding that a language is defined by its implementation, not its documentation. – Adám 4 hours ago

PHP, 18 bytes

while(!readline())
share|improve this answer
    
My solution beats this by the tiniest possible margin! – Arjun 3 hours ago

C#, 36 bytes

_=>while(System.Console.Read()==13);

Compiles to a Action<int>.

Or if a full program is required, 62 bytes.

class P{static void Main(){while(System.Console.Read()==13);}}
share|improve this answer

R, 27 bytes

while(!length(scan(,''))){}

Takes input from stdin, and repeat as long as input is of length 0.

share|improve this answer

Batch, 35 bytes

@set s=
@set/ps=
@if "%s%"=="" %0

Rather unfortunately, Batch doesn't clear the variable if you don't enter anything.

share|improve this answer

Bash, 51 bytes

It's my first time participating in PPCG, so dont be to rude ;D

#!/bin/bash
read a
while [ -z "$a" ]
do
read a
done
share|improve this answer
    
Welcome to PPCG. – Adám 1 hour ago
1  
Welcome to PPCG!, if you're planning on using Bash as a golfing language you might find This Post Interesting, it has a bunch of tips for making bash programs as small as humanly possible, there's also a More General Version you might want to skim through too. – ConnorLSW 1 hour ago
    
@Adám Thanks :D – Nicolas Fischer 18 mins ago
    
@ConnorLSW Wow, thanks, that Post will help me a lot. I'll try to edit my Answer with this new tips ASAP. – Nicolas Fischer 17 mins ago

PowerShell, 20 Bytes

for(;!(read-host)){}

runs a for loop,

read-host prompts for input

if read-host returns nothing it evals to false, so we invert that !(...) and use that as the loop end check.

much shorter than any do{$a=read-host}while($a-eq"") type solution involving variables.

share|improve this answer

Perl, 15 bytes (14 + -n flag)

1/0if$_ ne"\n"

Explanation:

The -n flag places a read loop around the program, turning it into:

LINE:
while (<>) {
    1/0 if $_ ne "\n"
}

Where "<>" reads a line from stdin to the variable $_. If the line contained more than the ending newline (ne is string inequality), a division by zero is performed which causes the program to output an error message and terminate.

share|improve this answer
3  
Welcome to PPCG. Can you explain your code? – Adám 2 hours ago
    
As 3aw5TZetdf suggests in his tip in Tips for golfing in Perl?, “Use $/ instead of "\n"”. And if you reverse the compered terms, you will not need the space. – manatwork 1 hour ago
3  
@user68838 Are you the OP? Did you create a new account to edit this? – Adám 1 hour ago

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.