Lizzie
Lizzie

Reputation: 179

PL/SQL Will this if then statement work?

If p_CreditHour is between 0 and 30 (including), the system prints "This student is a Freshmen" on the screen; if between 31 and 60 credits, print "This student is a Sophomore", between 61-90 credits, print "This student is a Junior"; for more than 91 credits, print "This student is a Senior."

Does the following program reflect the logic of the previous problem?

IF credit <= 30 THEN
     dbms_output.putline ('This student is a Freshmen'.);
END IF;
IF credit <= 60 THEN
     dbms_output.putline ('This student is a
Sophomore.');
END IF;
IF credit <= 90 THEN
     dbms_output.putline ('This student is a Junior.');
END IF;
IF credit > 90 THEN
     dbms_output.putline ('This student is a Senior.');
END IF;

Upvotes: 0

Views: 59

Answers (3)

Priom
Priom

Reputation: 53

Or you can use ADD statement.

IF credit <= 30 THEN
 dbms_output.putline ('This student is a Freshmen'.);
ElSIF credit <= 60 AND credit > 30 THEN
 dbms_output.putline ('This student is a Sophomore.');
ELSIF credit <= 90 AND credit > 60 THEN
 dbms_output.putline ('This student is a Junior.');
ELSE
 dbms_output.putline ('This student is a Senior.');
END IF;

Upvotes: 0

Belayer
Belayer

Reputation: 14861

Alternatively, there is the CASE statement.

begin 
  for s in ( select 1 student, 25 credits from dual union all 
             select 2 student, 35 credits from dual union all 
             select 3 student, 65 credits from dual union all 
             select 4 student, 85 credits from dual union all 
             select 5 student, 95 credits from dual 
           ) 
    loop 
      dbms_output.put( 'Student ' || to_char(s.student) || ' is a ');
      case  when s.credits <= 30 then dbms_output.put_line(' Freshman.');
            when s.credits <= 60 then dbms_output.put_line(' Sophomore.');
            when s.credits <= 90 then dbms_output.put_line(' Junior.');
            else  dbms_output.put_line(' Senior.');
      end case;
    end loop; 
end;

Upvotes: 1

Turo
Turo

Reputation: 4914

It wouldn't, since 15 is smaller than 30, 60 and 90, so the student would be a Frechman, a Sophomore and a Junior. But Oracle has an ELSIF that you should use:

IF credit <= 30 THEN
     dbms_output.putline ('This student is a Freshmen'.);
ELSIF credit <= 60 THEN
     dbms_output.putline ('This student is a Sophomore.');
ELSIF credit <= 90 THEN
     dbms_output.putline ('This student is a Junior.');
ELSE
     dbms_output.putline ('This student is a Senior.');
END IF;

Upvotes: 1

Related Questions