I want some PL/SQL code which prints 1 to 100. Additionally for numbers divisible by 3 print 'wel', for numbers divisible by 5 print 'come' and for numbers divisible by 3 and 5 then print 'welcome'. Output like this
1
2
wel
4
come
7
.
.
14
welcome
Here is some code I have written:
begin
for i in 1..100 loop
dbms_output.put_line(i);
if mod(i,3)=0 then
dbms_output.put_line('wel');
elsif mod(i,5)=0 then
dbms_output.put_line('come');
elsif mod(i,3)=0 and mod(i,5)=0 then
dbms_output.put_line('welcome');
end if;
end loop;
end;
/
15is both divisible by3and by5: so it you want to do something specific for multiples of15you need to test for divisible by 15 before testing for either divisible by 3 or divisible by 5. More generally, put start switch evaluations (case,if ... elsif..) with the most specific condition and progress towards the most general (the most general of all of course beingelse).