Sleep: Difference between revisions

From Rosetta Code
Content added Content deleted
m (→‎{{header|C++}}: Syntax highlighting, put code all in one block)
m (Syntax highlight everything while we're at it)
Line 25: Line 25:
=={{header|BASIC}}==
=={{header|BASIC}}==
{{works with|QuickBasic|4.5}}
{{works with|QuickBasic|4.5}}
INPUT sec 'the SLEEP command takes seconds
<qbasic>INPUT sec 'the SLEEP command takes seconds
PRINT "Sleeping..."
PRINT "Sleeping..."
SLEEP sec
SLEEP sec
PRINT "Awake!"
PRINT "Awake!"</qbasic>
"SLEEP" with no argument will sleep until a button is pressed on the keyboard (including modifier keys such as shift or control). Also, pressing a key while SLEEP is waiting for a specific amount of time (as above) will end the SLEEP.
"SLEEP" with no argument will sleep until a button is pressed on the keyboard (including modifier keys such as shift or control). Also, pressing a key while SLEEP is waiting for a specific amount of time (as above) will end the SLEEP.


Line 49: Line 49:


=={{header|Java}}==
=={{header|Java}}==
import java.util.Scanner;
<java>import java.util.Scanner;

public class Sleep{
public class Sleep{
public static void main(String[] args) throws InterruptedException{
public static void main(String[] args) throws InterruptedException{
Scanner input = new Scanner(System.in);
Scanner input = new Scanner(System.in);
int ms = input.nextInt(); //Java's sleep method accepts milliseconds
int ms = input.nextInt(); //Java's sleep method accepts milliseconds
System.out.println("Sleeping...");
System.out.println("Sleeping...");
Thread.sleep(ms);
Thread.sleep(ms);
System.out.println("Awake!");
System.out.println("Awake!");
}
}
}</java>
}


=={{header|Toka}}==
=={{header|Toka}}==

Revision as of 14:57, 15 April 2008

Task
Sleep
You are encouraged to solve this task according to the task description, using any language you may know.

Write a program that does the following in this order:

  • Input an amount of time to sleep in whatever units are most natural for your language (milliseconds, seconds, ticks, etc.). This unit should be noted in comments or in a description.
  • Print "Sleeping..."
  • Sleep the main thread for the given amount of time.
  • Print "Awake!"
  • End.

Ada

The Ada delay statement takes an argument of type Duration, which is a real number counting the number of seconds to delay. Thus, 2.0 will delay 2.0 seconds, while 0.001 will delay 0.001 seconds.

<ada>with Ada.Text_Io; use Ada.Text_Io; with Ada.Float_Text_Io; use Ada.Float_Text_Io;

procedure Sleep is

  In_Val : Float;

begin

  Get(In_Val);
  Put_Line("Sleeping...");
  delay Duration(In_Val);
  Put_Line("Awake!");

end Sleep;</ada>

BASIC

Works with: QuickBasic version 4.5

<qbasic>INPUT sec 'the SLEEP command takes seconds PRINT "Sleeping..." SLEEP sec PRINT "Awake!"</qbasic> "SLEEP" with no argument will sleep until a button is pressed on the keyboard (including modifier keys such as shift or control). Also, pressing a key while SLEEP is waiting for a specific amount of time (as above) will end the SLEEP.

C++

<cpp>#include <unistd.h>

  1. include <iostream>

using namespace std;

int main(int argc, char* argv[]) {

   useconds_t microseconds;
   cin >> microseconds;
   cout << "Sleeping..." << endl;
   usleep(microseconds);
   cout << "Awake!" << endl;
   return 0;

}</cpp>

Java

<java>import java.util.Scanner;

public class Sleep{ public static void main(String[] args) throws InterruptedException{ Scanner input = new Scanner(System.in); int ms = input.nextInt(); //Java's sleep method accepts milliseconds System.out.println("Sleeping..."); Thread.sleep(ms); System.out.println("Awake!"); } }</java>

Toka

This makes use of the sleep() function from libc which suspends execution for a specified number of seconds.

 1 import sleep as sleep()
 [ ." Sleeping...\n" sleep() drop ." Awake!\n" bye ] is sleep
 
 45 sleep