if else nested loops

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

回答 (1 件)

A. Sawas
A. Sawas 2019 年 4 月 8 日

0 投票

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 件のコメント

A. Sawas
A. Sawas 2019 年 4 月 8 日
I guess this is what you are trying to do in a cleaner code:
clc;
clear;
a=input('a');
b=input('b');
x=0;
y=0;
if a>b
x = x-1;
y = y-1;
else if a<b
x = x+1;
y = y+2;
else % a==b this is the remaining case
y = 1;
end
disp(x);
disp(y);
Arouj
Arouj 2019 年 4 月 8 日
actually this isnt my real code.. i got this and what u r saying is correct but in my real code its giving me zero except for the first case. this is the output
WI =
1.1830
W2 =
0.2196
W1 =
0
W2 =
0.2196
W1=0; W2=0;
if strcmp(RF,'b')
W1=((abs(Vab))*Iam*cos((angle(Vab)-Iaa)))/1000
W2=((abs(Vccomp-Vbcomp))*Icm*cos((angle(Vccomp-Vbcomp)-Ica)))/1000
else
if strcmp(RF,'a')
WI=((abs(Vbcomp-Vacomp))*Ibm*cos((angle(Vbcomp-Vacomp)-Iba)))/1000
W2=((abs(Vca))*Icm*cos((angle(Vca)-Ica)))/1000
else
if strcmp(RF,'c')
WI=((abs(Vacomp-Vccomp))*Iam*cos((angle(Vacomp-Vccomp)-Iaa)))/1000
W2=((abs(Vbc))*Ibm*cos((angle(Vbc)-Iba)))/1000
end
end
end
W1
W2
Walter Roberson
Walter Roberson 2019 年 4 月 8 日
You would need another end on that code, unless you change the else if to elseif
A. Sawas
A. Sawas 2019 年 4 月 8 日
First, to find the issue in the result try to display the value of the variables and intermediate calculations. One easy and quick way is to highlight part of the equation and press F9 to evaluate and see the result.
Second, I suggest you use switch statement which is nicer to follow and code:
switch (RF)
case 'a'
% case 'a' code
case 'b'
% case 'b' code
case 'c'
% case 'c' code
otherwise
fprintf('No such case');
end
Arouj
Arouj 2019 年 4 月 8 日
i tried this..and got the following output
WI =
1.1830
W2 =
0.2196
W1 =
0
W2 =
0.2196
W1=0;
W2=0;
switch (RF)
case 'a'
WI=((abs(-Vab))*Ibm*cos((angle(-Vab)-Iba)))/1000
W2=((abs(Vca))*Icm*cos((angle(Vca)-Ica)))/1000
case 'b'
W1=((abs(Vab))*Iam*cos((angle(Vab)-Iaa)))/1000
W2=((abs(Vccomp-Vbcomp))*Icm*cos((angle(Vccomp-Vbcomp)-Ica)))/1000
case 'c'
WI=((abs(Vacomp-Vccomp))*Iam*cos((angle(Vacomp-Vccomp)-Iaa)))/1000
W2=(abs(Vbc))*Ibm*cos((angle(Vbc)-Iba))/1000
otherwise
end
W1
W2
Image Analyst
Image Analyst 2019 年 4 月 8 日
編集済み: Image Analyst 2019 年 4 月 8 日
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 2019 年 4 月 9 日
Image Analyst: Thanks for the note ... I know that very well ;)

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeProgramming についてさらに検索

質問済み:

2019 年 4 月 8 日

コメント済み:

2019 年 4 月 9 日

Community Treasure Hunt

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

Start Hunting!

Translated by