Fortran 90 very basic question : else if statement

  • Context: Fortran 
  • Thread starter Thread starter fluidistic
  • Start date Start date
  • Tags Tags
    Fortran If statement
Click For Summary

Discussion Overview

The discussion revolves around a Fortran 90 programming issue related to implementing an "else if" statement in a program designed to find the roots of a quadratic binomial. Participants explore how to handle the case when the coefficient 'a' is zero, which invalidates the quadratic nature of the equation.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant describes their attempt to write a program that calculates the roots of a quadratic equation and encounters issues when 'a' is set to zero.
  • Another participant suggests using an "else" statement instead of an "endif" to resolve the issue.
  • A later reply points out that the syntax for the "if" statement should use "==" instead of "=" to avoid compilation errors.
  • One participant proposes using a "goto" statement to loop back for user input if 'a' equals zero, allowing the user to try again.
  • Another participant expresses satisfaction with the solution that includes the correct use of "==", indicating that it resolved their issue.
  • There are mentions of differences in variable declaration requirements between compilers, suggesting variability in implementation.
  • Areas of Agreement / Disagreement

    Participants generally agree on the need for correct syntax in the "if" statement and the use of "goto" for user input handling. However, there is no consensus on the best approach to structure the program, as different solutions are proposed and discussed.

    Contextual Notes

    Limitations include potential differences in compiler behavior and variable declaration requirements that may affect program functionality.

    Who May Find This Useful

    Individuals learning Fortran programming, particularly those interested in handling conditional statements and user input in their programs.

fluidistic
Gold Member
Messages
3,932
Reaction score
283
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
Ok. It has been a long time (fortran77) but try an else in place of your first endif.
 
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.
 
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.
 
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
 
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:
That's even better. And you are welcome.

That was a fun trip in the way back machine.
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 25 ·
Replies
25
Views
4K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 19 ·
Replies
19
Views
7K
  • · Replies 54 ·
2
Replies
54
Views
5K
  • · Replies 23 ·
Replies
23
Views
3K