IF statements program

100 views Asked by At

I'm having an issue. I have to write a program for my calculator but I'm not sure how to do it. It uses a form of QBASIC language.

My statement is:

IF (y>0 and x>0) then it should calculate n:=ATAN(y/x);
IF (y<0 and x<0) then it should calculate n:=ATAN(y/x)+180;
IF (y>0 and x<0) then it should calculate n:=ATAN(y/x)+180;
IF (y<0 and x>0) then it should calculate n:=ATAN(y/x)+360;

I think I could only use (IF, ELSE, THEN)

2

There are 2 answers

0
Monodeep Saha On

your code is

 cls
input x 
input y
if y>0 and x>0 then 
n=ATAN(y/x) 
else if y<0 and x<0 then 
n= ATAN(y/x)+ 180 
else if y>0 and x<0 then 
n=ATAN(y/x)+180 
else if y<0 and x>0 then 
n= ATAN(y/x)+360
endif
end
1
Sep Roland On

If IF, THEN, and ELSE are all you have then the following applies:

The rule for a statement containing several IFs and ELSEs is that the first ELSE is associated with the closest preceding IF, and each subsequent ELSE with the closest unassigned preceding IF.

                                         first ELSE            subsequent ELSE                   first ELSE
                                         |                     |                                 |
                                         v                     v                                 v
IF y<0 THEN IF x<0 THEN n:=ATAN(y/x)+180 ELSE n:=ATAN(y/x)+360 ELSE IF x<0 THEN n:=ATAN(y/x)+180 ELSE n:=ATAN(y/x)
^           ^                            |                     |    ^                            |
|           \--- closest preceding IF ---/                     |    \--- closest preceding IF ---/
\---unassigned closest preceding IF ---------------------------/

If possible, use the somewhat less complex:

n:=ATAN(y/x): IF y<0 THEN IF x<0 THEN n:=n+180 ELSE n:=n+360 ELSE IF x<0 THEN n:=n+180