OR statement in a while Loop

22 ビュー (過去 30 日間)
Brian Clyde
Brian Clyde 2020 年 4 月 2 日
編集済み: James Tursa 2020 年 4 月 2 日
I am trying to get a while loop with an OR statement in it. I want it to end when either the player or the enemy hp reaches 0, but it won't end for the enemy hp hitting zero or for the player hp hitting zero. here is a sample that should simulate the code.
I end up just getting infinite loops. How do I get it to read the OR statment?
enemyhp=32;
playerhp=32;
while ((playerhp > 0) || (enemyhp > 0))
playerhp = playerhp - 4
%enemyhp = enemyhp - 4
end

採用された回答

James Tursa
James Tursa 2020 年 4 月 2 日
編集済み: James Tursa 2020 年 4 月 2 日
Change to AND:
while ((playerhp > 0) && (enemyhp > 0))
I.e., you only do the while loop when both hp are positive. It either one of them is not, then the while loop exits.

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by