pl/sql - equation [message #38476] |
Sat, 20 April 2002 12:16 |
Mark
Messages: 284 Registered: July 1998
|
Senior Member |
|
|
Hello, I'm trying to sum all the even numbers from 1 to 1000. i.e. 2+4+6+8+10 =30..I accumulate but when the loop goes to the top, the variable is cleared and the last loop value is displayed. The code below is what I'm using. Can I get a variable to hold the accumulated value so that at the end of the loop it displays the total sum of all the looped values? Thanks for the help.
set serveroutput on
DECLARE
TOTAL NUMBER;
TTOTAL NUMBER;
BEGIN
FOR I IN 1..1000 LOOP
IF mod(I,2)=0 then
total:=I + I;
ttotal:=total + I;
END IF;
END LOOP;
DBMS_OUTPUT.PUT_LINE('Total sum of even numbers: '||ttotal);
END;
/
|
|
|
Re: pl/sql - equation [message #38477 is a reply to message #38476] |
Sat, 20 April 2002 22:22 |
Todd Barry
Messages: 4819 Registered: August 2001
|
Senior Member |
|
|
Wouldn't you just want something like:
DECLARE
TOTAL NUMBER := 0;
BEGIN
FOR I IN 1..1000 LOOP
IF mod(I,2) = 0 then
total := total + I;
END IF;
END LOOP;
DBMS_OUTPUT.PUT_LINE('Total sum of even numbers: ' || total);
END;
/
|
|
|