Effacer les filtres
Effacer les filtres

How can I skip the rest of an if statement?

50 vues (au cours des 30 derniers jours)
Jing Ci Neo
Jing Ci Neo le 20 Fév 2024
Commenté : Jing Ci Neo le 20 Fév 2024
Is there a way to skip the rest of an "if" statement if a condition is fulfilled? I want to having many if-else statements. I can't use break because it is only for "for" and "while" loops, is there something similar for "if" statements?
Here is a simple example of my code.
if n > 1
if a == 1
disp('Haha')
break % (Here is where I want to skip the rest of if statement)
end
disp('Nooooo')
end
disp('Done!')

Réponse acceptée

Walter Roberson
Walter Roberson le 20 Fév 2024
if n > 1
while true
if a == 1
disp('Haha')
break % (Here is where I want to skip the rest of if statement)
end
disp('Nooooo')
break;
end
end
disp('Done!')
  1 commentaire
Jing Ci Neo
Jing Ci Neo le 20 Fév 2024
I think this would work the best for my code, thank you!

Connectez-vous pour commenter.

Plus de réponses (2)

Hassaan
Hassaan le 20 Fév 2024
Modifié(e) : Hassaan le 20 Fév 2024
if n > 1
if a == 1
disp('Haha')
% No additional commands here - skips directly to the end of the outer if block
else
% This part will only execute if a ~= 1
disp('Nooooo')
end
end
disp('Done!')
In this structure, if a == 1, MATLAB displays "Haha" and then skips to the end of the if block, because there are no other commands to execute in the if a == 1 case. If a is not equal to 1, MATLAB does not enter the first if block and instead goes to the else part, executing whatever commands are there.
Effectively uses the logical structuring of your conditions to control the flow of your code without needing a specific statement to exit an if block.
-----------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
It's important to note that the advice and code are based on limited information and meant for educational purposes. Users should verify and adapt the code to their specific needs, ensuring compatibility and adherence to ethical standards.
Professional Interests
  • Technical Services and Consulting
  • Embedded Systems | Firmware Developement | Simulations
  • Electrical and Electronics Engineering
Feel free to contact me.

Torsten
Torsten le 20 Fév 2024
Modifié(e) : Torsten le 20 Fév 2024
Also in for-loops, "break" only exits the inner structure, but the outer loop runs until the end (see below).
Thus you cannot expect that "break" in your nested if structure will exit completely.
for i = 1:10
for j = 1:10
if j==2
break
end
end
i
end
i = 1
i = 2
i = 3
i = 4
i = 5
i = 6
i = 7
i = 8
i = 9
i = 10

Catégories

En savoir plus sur Startup and Shutdown dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by