Using an ELSEIF statement under another IF

6 vues (au cours des 30 derniers jours)
sebastian mendoza
sebastian mendoza le 28 Nov 2016
Commenté : Image Analyst le 28 Nov 2016
Hi,
Is it possible to check for a condition, and if that condition is true, check for more? If so, can you please explain? Here's an example of what I mean
if input < 1
if input2 < 1
% then do this
if input2 < 2
% then do this
end
  1 commentaire
bio lim
bio lim le 28 Nov 2016
Yes, it's possible!

Connectez-vous pour commenter.

Réponse acceptée

bio lim
bio lim le 28 Nov 2016
Here is an example:
input = input('Please insert an integer\n');
if input < 10
if input < 5
fprintf('Your input is less than 5\n');
end
if input > 5
fprintf('Your input is greater than 5\n');
end
if input == 5
fprintf('Your input is 5\n');
end
else
fprintf('Your input is greater than 10\n')
end
Make sure to end your if statements with an end.
  2 commentaires
sebastian mendoza
sebastian mendoza le 28 Nov 2016
Thank you so much! I was missing the end after every if. I put them at the very bottom at first.
Image Analyst
Image Analyst le 28 Nov 2016
You can use an elseif if you want, in this case:
input = input('Please insert an integer\n');
if input <= 10
if input < 5
fprintf('Your input is less than 5\n');
elseif input > 5
fprintf('Your input is greater than 5\n');
else
fprintf('Your input is exactly 5\n');
end
else
fprintf('Your input is greater than 10\n')
end

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Simulink 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