## converting if to math

## converting if to math

(OP)

Hi guys! How to convert a expression with IF to only math?

I mean, how to transform the "if" in this following expression in a mathematical expression?

Thank you in advanced!

PROGRAM if_in_math

IMPLICIT none

INTEGER :: num

PRINT *, "Type a number"

READ *, num

IF(num.GE.0)THEN

PRINT *, num

END IF

END PROGRAM

I mean, how to transform the "if" in this following expression in a mathematical expression?

Thank you in advanced!

PROGRAM if_in_math

IMPLICIT none

INTEGER :: num

PRINT *, "Type a number"

READ *, num

IF(num.GE.0)THEN

PRINT *, num

END IF

END PROGRAM

## RE: converting if to math

François Jacq

## RE: converting if to math

I think that this functional style is not supported by Fortran, because it can retrun in boolean concept only True or False.

But, for example in Perl you can do this

## CODE

## CODE

The same output could be done in Python

## CODE

## RE: converting if to math

## CODE

## RE: converting if to math

http://en.wikipedia.org/wiki/Short-circuit_evaluat...

## RE: converting if to math

## CODE

## RE: converting if to math

Link

## RE: converting if to math

## RE: converting if to math

## RE: converting if to math

Another way is to use WHERE constructs which replaces in the same time DO loops and IF statements.

But for your example, the IF instruction is obviously the easiest way ! All the funny attempts to avoid it, as demonstrated by mikrom and xwb, have drawbacks like for instance an empty WRITE statement when num is <= 0 !

François Jacq

## RE: converting if to math

If in your opinion a program contains too many IFs, it is very naive or stupid to try to replace these IFs even with mathematical or logical constructs.

Even though you should be able to do this, it would be very likely that in the end your program will be much more incomprehensible than before.

So if you want to avoid too many IFs then change your programming style.

## RE: converting if to math

I am trying to do some analysis about some struture as a way to improve my programming style.

For example: I was thinking about how to chance it mathematics.

IF(lim_1>lim_2)THEN

interval = lim_1 - lim_2 + 1

lim_in = lim_2

ELSE

lim_in = lim_1

interval = lim_2 - lim_1 + 1

END IF

## RE: converting if to math

## CODE

François Jacq

## RE: converting if to math

Although in my opinion the original code with IF is better understandable then the mathematical expressions with ABS and MIN. But it's philosophical question what of the both is better - my opinion is that explicit is better than implicit

With changing programming style I thought about dividing your long programming task into short subtasks. Then for every subtask you can write a subroutine or a function. Keep your subroutines and functions slim and you shall not have to bother with too much code on one place and optionally with too many nested IFs.

For example your code given above could be placed into a short subroutines, which takes on input lim_1 and lim_2 and returns interval and lim_in :

## CODE

The IF is now hidden in the subroutine. In the main program you only have to CALL the subroutine, so the code containing in the subroutine will not be irritating for you.

## RE: converting if to math

mikrom I going to try from today use subroutines. It become the program pretty organized. Thank you again for your tips!