oracle

Tuesday, 2 May 2017

PL/SQL: IF ELSEIF OR IF THEN ELSEIF STATEMENT

If Then else if statement is used with the multiple conditions in Selection structure.

Syntax:
IF condition then
statement(s);
elseif conditon  then
statement(s);
elseif  condition then
statement(s);
else
statement(s);
end if;
statement(s);
end;


IF THEN ELSE Statement is responsible for the execution of first statement if the condition is true. and if conditin is true then it will never evaluate the rest conditions .In case non of this condition is true, it evaluate the ELSE statement.

Example:

declare
place varchar2(30):= '&Enter_City_name';
begin
if place = 'Metropolis' then
dbms_output.put_line('City Is protected by Salman Khan');
elsif place='Gotham' then
dbms_output.put_line('City is protected by Amir khan');
elsif place='Amazon' then
dbms_output.put_line('City is protected by Shahrukh Khan');
else
dbms_output.put_line('Call to the Police');
end if;
dbms_output.put_line('Thank you For Contacting us');

end;

Result:
Enter_City_name = Metropolis

City Is protected by Salman Khan
Thank you For Contacting us


Now check it with using city name that is not in else statements.
Try it again.

declare
place varchar2(30):= '&Enter_City_name';
begin
if place = 'Metropolis' then
dbms_output.put_line('City Is protected by Salman Khan');
elsif place='Gotham' then
dbms_output.put_line('City is protected by Amir khan');
elsif place='Amazon' then
dbms_output.put_line('City is protected by Shahrukh Khan');
else
dbms_output.put_line('Call to the Police');
end if;
dbms_output.put_line('Thank you For Contacting us');
end;

Result:
Enter_City_name = Faisalabad

Call to the Police

Thank you For Contacting us

No comments:

Post a Comment