0

i was trying to make something like this when input : 5

it will print A B C D E

input : 10

print A B C D E J I H G F

input : 15

print A B C D E J I H G F K L M N O

input : 20 A B C D E J I H G F K L M N O T S R Q P

and so on... here is my code i create

    declare
    angka number := '&Angka';
    i number := trunc(angka/5);
    p number := 65;
    a number := 1;
    b number := 1;
begin
    while a <= b loop
    if mod(i,2) = 1 then
        a := 5;
        for b in 1..5 loop
            p := p + a
            dbms_output.put( chr(p) || ' ' );
            a := a - 1;
        end loop;
        p := p + 5;
    else
        a := 1;
        for b in 1..5 loop
            p := p + a
            dbms_output.put( chr(p) || ' ' );
            a := a + 1;
        end loop;
    end loop;
    dbms_output.put_line(' ');
end;
/

but i was still confused it's still didn't work and about dbms_output.put_line vs dbms_output.put can someone explain this ? because i was trying print using dbms_output.put it's didn't show.. i don't know why

Thanks

Lalit Kumar B
  • 47,486
  • 13
  • 97
  • 124

2 Answers2

1

Firstly, the line p := p + a has not been terminated by semi-colon. Ideally, the PL/SQL anonymous block shouldn't compile at first place.

Secondly, with PUT procedure, you haven't completed the line yet. It needs GET_LINES to retrieve an array of lines from the buffer.

There was a similar question, Is dbms_output.put() being buffered differently from dbms_output.put_line()?

Community
  • 1
  • 1
Lalit Kumar B
  • 47,486
  • 13
  • 97
  • 124
0

You have some problems in your code. I don't believe that you can execute exactly this code. Propably, you forgot to copy some parts of it.

First of all, syntax errors:

declare
    angka number := '&Angka';
    i number := trunc(angka/5);
    p number := 65;
    a number := 1;
    b number := 1;
begin
    while a <= b loop
    if mod(i,2) = 1 then
        a := 5;
        for b in 1..5 loop
            p := p + a  -- ";" missed
            dbms_output.put( chr(p) || ' ' );
            a := a - 1;
        end loop;
        p := p + 5;
    else
        a := 1;
        for b in 1..5 loop
            p := p + a  -- ";" missed
            dbms_output.put( chr(p) || ' ' );
            a := a + 1;
        end loop;
   -- here you missed "end if" 
    end loop;
dbms_output.put_line(' ');
end;
/

Also you don't need your outer loop ("while a <= b loop"), because its condition always is true and code execution will never ends. And last - when you declare

for b in 1..5 loop

oracle creates here new variable with name "b", and inside the loop previously declared b is not visible. Try to execute this:

declare
  b number := 111;
begin
  for b in 1..5 loop
    dbms_output.put_line(b);
  end loop;
  dbms_output.put_line(b);
end;
/

You will get:

1
2
3
4
5
111

If you correct these errors, your code will work as you want.

Dmitriy
  • 5,525
  • 12
  • 25
  • 38