June 2018 Update
This commit is contained in:
parent
ba8067c3b7
commit
22f33d4004
5278 changed files with 84726 additions and 14379 deletions
|
|
@ -6,4 +6,20 @@ Start an integer value at '''1024'''.
|
|||
Loop while it is greater than zero.
|
||||
|
||||
Print the value (with a newline) and divide it by two each time through the loop.
|
||||
|
||||
|
||||
;Related tasks:
|
||||
* [[Loop over multiple arrays simultaneously]]
|
||||
* [[Loops/Break]]
|
||||
* [[Loops/Continue]]
|
||||
* [[Loops/Do-while]]
|
||||
* [[Loops/Downward for]]
|
||||
* [[Loops/For]]
|
||||
* [[Loops/For with a specified step]]
|
||||
* [[Loops/Foreach]]
|
||||
* [[Loops/Increment loop index within loop body]]
|
||||
* [[Loops/Infinite]]
|
||||
* [[Loops/N plus one half]]
|
||||
* [[Loops/Nested]]
|
||||
* [[Loops/While]]
|
||||
<br><br>
|
||||
|
|
|
|||
6
Task/Loops-While/BASIC/loops-while-3.basic
Normal file
6
Task/Loops-While/BASIC/loops-while-3.basic
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
10 N% = 1024
|
||||
20 IF N% = 0 THEN 60
|
||||
30 PRINT N%
|
||||
40 N% = N%/2
|
||||
50 GOTO 20
|
||||
60 END
|
||||
|
|
@ -1,5 +1,5 @@
|
|||
int i = 1024;
|
||||
while(i > 0) {
|
||||
while(i > 0){
|
||||
std::cout << i << std::endl;
|
||||
i /= 2;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,2 +1,2 @@
|
|||
for (int i = 1024; i>0; i /= 2)
|
||||
for(int i = 1024; i > 0; i /= 2)
|
||||
std::cout << i << std::endl;
|
||||
|
|
|
|||
|
|
@ -1,2 +1,3 @@
|
|||
for (init; cond; update)
|
||||
for(init; cond; update){
|
||||
statement;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,7 +1,6 @@
|
|||
{
|
||||
init;
|
||||
while (cond)
|
||||
{
|
||||
while(cond){
|
||||
statement;
|
||||
update;
|
||||
}
|
||||
|
|
|
|||
9
Task/Loops-While/Fortress/loops-while.fortress
Normal file
9
Task/Loops-While/Fortress/loops-while.fortress
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
component loops_while
|
||||
export Executable
|
||||
|
||||
var i:ZZ32 = 1024
|
||||
run() = while i > 0 do
|
||||
println(i)
|
||||
i := i DIV 2
|
||||
end
|
||||
end
|
||||
|
|
@ -1,4 +1,5 @@
|
|||
import Control.Monad (when)
|
||||
|
||||
main = loop 1024
|
||||
where loop n = when (n > 0)
|
||||
(do print n
|
||||
|
|
|
|||
|
|
@ -1,5 +1,10 @@
|
|||
import Control.Monad (when)
|
||||
import Data.IORef
|
||||
import Control.Monad.Loops
|
||||
|
||||
whileM :: (Monad m) => m Bool -> m a -> m ()
|
||||
whileM cond body = do c <- cond
|
||||
when c (body >> whileM cond body)
|
||||
main :: IO ()
|
||||
main = do r <- newIORef 1024
|
||||
whileM_ (do n <- readIORef r
|
||||
return (n > 0))
|
||||
(do n <- readIORef r
|
||||
print n
|
||||
modifyIORef r (`div` 2))
|
||||
|
|
|
|||
|
|
@ -1,9 +1,11 @@
|
|||
{-# LANGUAGE MonadComprehensions #-}
|
||||
import Data.IORef
|
||||
import Control.Monad.Loops
|
||||
|
||||
main :: IO ()
|
||||
main = do r <- newIORef 1024
|
||||
whileM (do n <- readIORef r
|
||||
return (n > 0))
|
||||
(do n <- readIORef r
|
||||
print n
|
||||
modifyIORef r (`div` 2))
|
||||
main = do
|
||||
r <- newIORef 1024
|
||||
whileM_ [n > 0 | n <- readIORef r] $ do
|
||||
n <- readIORef r
|
||||
print n
|
||||
modifyIORef r (`div` 2)
|
||||
|
|
|
|||
10
Task/Loops-While/PL-SQL/loops-while.sql
Normal file
10
Task/Loops-While/PL-SQL/loops-while.sql
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
set serveroutput on
|
||||
declare
|
||||
n number := 1024;
|
||||
begin
|
||||
while n > 0 loop
|
||||
dbms_output.put_line(n);
|
||||
n := trunc(n / 2);
|
||||
end loop;
|
||||
end;
|
||||
/
|
||||
|
|
@ -1,5 +1,5 @@
|
|||
my $n = 1024;
|
||||
while ($n) {
|
||||
while($n){
|
||||
print "$n\n";
|
||||
$n = int $n / 2;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,3 @@
|
|||
my $n = 1024;
|
||||
until ($n <= 0) {
|
||||
for(my $n = 1024; $n > 0; $n >>= 1){
|
||||
print "$n\n";
|
||||
$n = int $n / 2;
|
||||
}
|
||||
|
|
|
|||
5
Task/Loops-While/Perl/loops-while-3.pl
Normal file
5
Task/Loops-While/Perl/loops-while-3.pl
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
my $n = 1024;
|
||||
until($n == 0){
|
||||
print "$n\n";
|
||||
$n = int $n / 2;
|
||||
}
|
||||
40
Task/Loops-While/X86-Assembly/loops-while.x86
Normal file
40
Task/Loops-While/X86-Assembly/loops-while.x86
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
; NASM 64 bit X86-64 assembly on Linux
|
||||
|
||||
global main
|
||||
extern printf
|
||||
|
||||
segment .data
|
||||
|
||||
printffmt db `%ld\n`,0
|
||||
|
||||
segment .text
|
||||
|
||||
main:
|
||||
push rbp
|
||||
mov rbp,rsp
|
||||
|
||||
; used rbx and r12 because printf preserves these values
|
||||
|
||||
mov rbx,1024 ; start with 1024
|
||||
mov r12,2 ; load 2 as divisor
|
||||
|
||||
.toploop ; top of while loop
|
||||
cmp rbx,0 ; compare to 0
|
||||
jle .done ; exit 0 or less
|
||||
|
||||
lea rdi,[printffmt] ; print number in rsi
|
||||
mov rsi,rbx ; mov to rsi as argument
|
||||
call printf
|
||||
|
||||
; calculate n/2 and save
|
||||
xor rdx,rdx ; clear rdx for division
|
||||
mov rax,rbx ; mov number to rax for division
|
||||
idiv r12 ; divide by 2
|
||||
mov rbx,rax ; save n/2
|
||||
|
||||
jmp .toploop ; next loop
|
||||
|
||||
.done
|
||||
xor rax,rax ; return code 0
|
||||
leave ; fix stack
|
||||
ret ; return
|
||||
Loading…
Add table
Add a link
Reference in a new issue