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.

2
  • 1
    What did you try so far? Have a look at minval and maxval. Commented Mar 16, 2018 at 13:46
  • Welcome, take the tour and see How to 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 stackoverflow.com/questions/8340592/… Commented Mar 16, 2018 at 14:59

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

but how can I ask if exactly/more/less elements then x (variable defined before in the program) are a logical statement?
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.