if else nested loops

1 vue (au cours des 30 derniers jours)
Arouj
Arouj le 8 Avr 2019
Commenté : A. Sawas le 9 Avr 2019
This is my program... i want the ouput of x and y to be the new values not the 0 and 0. this always returns me 0 and 0 why?
clc
clear
a=input('a')
b=input('b')
x=0;
y=0;
if a>b
x-1
y-1
else if a<b
x+1
x+2
else if a==b
y=1
x
end
end
end
x
y

Réponses (1)

A. Sawas
A. Sawas le 8 Avr 2019
The problem is not with the if-else statments (although they are not correctly used). You need to assign the new values to x and y like this:
x - 1; % this does not change the values of x
x = x - 1; % the new value (x-1) is assigned to x
y = y - 1;
  7 commentaires
Image Analyst
Image Analyst le 8 Avr 2019
Modifié(e) : Image Analyst le 8 Avr 2019
Sawas:
else if a<b
is much, much different than
elseif a<b
If you don't know why, just ask.
A. Sawas
A. Sawas le 9 Avr 2019
Image Analyst: Thanks for the note ... I know that very well ;)

Connectez-vous pour commenter.

Catégories

En savoir plus sur Loops and Conditional Statements 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