0

I am programming in Fortran and if all single elements of my array are positive I want to execute statement 1, if they are partly positive execute statement 2 and if all are negative execute statement 3.

I know I will probably need a 'do' loop and a 'if' construct but could not figure out how to do it best.

MDR
  • 1
  • 1
    What did you try so far? Have a look at minval and maxval. – albert Mar 16 '18 at 13:46
  • Welcome, take the [tour] and see [ask]. How are you stuck? Did you encounter any errors? Which errors? Whit which exact code? You probably just need to use `ALL()`, see very similar https://stackoverflow.com/questions/8340592/how-to-check-if-fortran-array-contains-value – Vladimir F Героям слава Mar 16 '18 at 14:59

1 Answers1

5

There is no need to use loop for a simple condition

 if (ALL(A>0)) then
    statement1
 else if (ALL(A<0)) then
    statement3
 else
    statement2
 end if

Explanation: A>0 is an array of logical values based on evaluating the condition for each element of the original array A. Function ALL() then reduces this logical array and returns true if all elements are true and false otherwise.

You request a do loop in the title. If you really need to fix a particular error with that, you must show us the code from your efforts, your errors and all other important details.

  • but how can I ask if exactly/more/less elements then x (variable defined before in the program) are a logical statement? – MDR Mar 20 '18 at 12:40
  • There is another function, `COUNT()` if I understand you correctly. You can ask a new question if you think it is necessary, first search and if it hasn't been asked before, ask a new question. – Vladimir F Героям слава Mar 20 '18 at 12:49