Fortran 90 very basic question : else if statement

In summary, the program tried to find the roots of a quadratic binomial, but if a user enters an invalid coefficient (a=0), the program displays a message telling the user that the binomial is not quadratic. If the coefficient is not invalid, but the binomial has no roots in real numbers, the program outputs the negative of the roots in complex numbers, as well as the coordinates of those roots.
  • #1
fluidistic
Gold Member
3,923
261
Hi there,
I've tried all I could in order to complete a very simple and basic program that finds the roots of any quadratic binomial.
You have to enter the coefficients a, b and c and it would find the roots. I could do that, but a problem occurred if I typed a=0. Of course if a=0 the binomial is not quadratic (and not even a binomial), so I wanted to let it know to the user of the program. Here my problems started... I don't know how to insert in my program that if a=0 then a message would appear saying that it isn't a quadratic binomial.
Here it comes :
Program findroot
implicit none

Real(8) :: a,b,c,delta,z,y,g


Write(*,*)'Enter a, b and c'
Read(*,*)a,b,c


if (a=0) then
write(*,*)'This is not a quadratic binomial'
endif


delta=b**2-4*a*c

If (delta>=0) then
z=(-b+sqrt(delta))/(2*a)
y=(-b-sqrt(delta))/(2*a)
Write(*,*)'The roots are',z,y


Elseif (delta<0) then
write(*,*)'The binomial has no root in R but in C its roots are',-b,'+i',sqrt(-delta)/(2*a),'and',-b,'-i',sqrt(-delta)/(2*a)



endif




End program findroot
I'm almost sure that I cannot type endif and then type if...
But I tried many other things like
if (a=0) then
write(*,*)'This is not a quadratic binomial'



delta=b**2-4*a*c

elseIf (delta>=0) then
z=(-b+sqrt(delta))/(2*a)
y=(-b-sqrt(delta))/(2*a)
Write(*,*)'The roots are',z,y


Elseif (delta<0) then
write(*,*)'The binomial has no root in R but in C its roots are',-b,'+i',sqrt(-delta)/(2*a),'and',-b,'-i',sqrt(-delta)/(2*a)



endif
and changing the order of the if statements... I always get errors and the program doesn't compile.
Any help is appreciated.

Note: If I erase the if (a=0) statement the program works well. (it compiles and find roots unless I type a=0).
 
Technology news on Phys.org
  • #2
Ok. It has been a long time (fortran77) but try an else in place of your first endif.
 
  • #3
Thanks for the answer.
It doesn't work, I get an error : Unexpected ELSE statement.
By the way the other error is : Error: Syntax error in IF-expression at (1). (It points out the "a" in "if (a=0) then". I don't see why it is a syntax error... strange.
 
  • #4
Look at this and see what you think. It compiled on a G77 compiler. It needed a == in the
if (a=0) then

there is still a problem with ending the prog after the message on a=0

Program findroot

Write(*,*)'Enter a, b and c'
Read(*,*)a,b,c


if (a == 0) then
write(*,*)'This is not a quadratic binomial'
delta = b**2-4*a*c
endif

If (delta >= 0) then
z = (-b+sqrt(delta))/(2*a)
y = (-b-sqrt(delta))/(2*a)
Write(*,*)'The roots are',z,y


Elseif (delta<0) then
write(*,*)'The binomial has no root in R but in C its roots

are',-b,'+i',sqrt(-delta)/(2*a),'and',-b,'-i',sqrt(-delta)/(2*a)

endif

End program



There are some variable declaration differences between my compiler and your also
so ignore my lack of declaration. I am still messing with it so hang with me.
 
  • #5
how about this?

Program findroot

Write(*,*)'Enter a, b and c'
Read(*,*)a,b,cif (a == 0) then
write(*,*)'This is not a quadratic binomial'
goto 90
endif

delta = b**2-4*a*c
If (delta >= 0) then
z = (-b+sqrt(delta))/(2*a)
y = (-b-sqrt(delta))/(2*a)
Write(*,*)'The roots are',z,yElseif (delta<0) then
write(*,*)'The binomial has no root in R but in C its roots

are',-b,'+i',sqrt(-delta)/(2*a),'and',-b,'-i',sqrt(-delta)/(2*a)

endif
90 write(*,*)'Goodbye'
End program
 
  • #6
Thanks a lot montoyas!
It works great... I forgot that 2 signs "=" were needed. That's nice.
What about this :
100 Write(*,*)'Enter a, b and c'
Read(*,*)a,b,cif ( a==0 ) then
write(*,*)'This is not a quadratic binomial, try again'
goto 100
endif
:)
 
Last edited:
  • #7
That's even better. And you are welcome.

That was a fun trip in the way back machine.
 

1. What is an "else if" statement in Fortran 90?

An "else if" statement is a conditional statement in Fortran 90 that allows for additional conditions to be evaluated if the first condition in an "if" statement is not met. It allows for more complex decision-making in a program.

2. How do you use an "else if" statement in Fortran 90?

To use an "else if" statement in Fortran 90, you first need to write an "if" statement with a condition. Then, after the "if" statement, you can add one or more "else if" statements with their own conditions. Finally, you can add an optional "else" statement at the end for any remaining cases that do not meet the conditions in the "if" or "else if" statements.

3. Can you have multiple "else if" statements in Fortran 90?

Yes, you can have multiple "else if" statements in Fortran 90. This allows for more complex decision-making in a program, as you can have multiple conditions that need to be evaluated.

4. What happens if none of the conditions in an "else if" statement are met?

If none of the conditions in an "else if" statement are met, then the code within the "else" statement, if one is included, will be executed. If there is no "else" statement, then the program will continue to the next line of code outside of the "if" statement.

5. Are "else if" statements necessary in Fortran 90?

No, "else if" statements are not necessary in Fortran 90. They are used to add additional conditions to an "if" statement, but if your program does not require this level of complexity, you can simply use an "if" statement without any "else if" or "else" statements.

Similar threads

  • Programming and Computer Science
Replies
4
Views
588
  • Programming and Computer Science
Replies
12
Views
953
  • Programming and Computer Science
Replies
5
Views
2K
  • Calculus and Beyond Homework Help
Replies
1
Views
254
  • Programming and Computer Science
Replies
2
Views
1K
Replies
3
Views
572
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
12
Views
1K
  • Set Theory, Logic, Probability, Statistics
Replies
1
Views
1K
  • Programming and Computer Science
Replies
4
Views
2K
Back
Top