How to use multiple break statments

3 Ansichten (letzte 30 Tage)
Muhammad Qaisar Fahim
Muhammad Qaisar Fahim am 14 Jul. 2021
In the below code if any of the error goes below the limit the loop break. But I want to break the loop if all of the conditions errors are below the defined limits. How can I modify this piece of code.
for i = 1:100
if(sln2.Error<=1)
break
else
% Update weights
Ext.W22=beta*Ext.W22;
Ext.W22List(i) = Ext.W22;
end
%% Convergance Check sub 2
sln3.Error=max(abs(Ext.P_gen_scaling-Ext.P_gen_scaling_3));
SLN3(i)=sln3;
if(sln3.Error<=0.1)
break
else
end
  4 Kommentare
Muhammad Qaisar Fahim
Muhammad Qaisar Fahim am 14 Jul. 2021
Yeah end is there . so there is no error with that . My question is that you can see that there are 2 break statments within the for loop. my for loop keeps on repiting when both of the if conditions are false but as only the single condition satisfies then the for loop breaks. But i want that even single condition is false the for loop shoulkd keep on repeteting.
Muhammad Qaisar Fahim
Muhammad Qaisar Fahim am 14 Jul. 2021
Please consider this below code
for i=1:100
if(sln2.Error<=1)
break
else
% Update weights
Ext.W22=beta*Ext.W22;
Ext.W22List(i) = Ext.W22;
end
%% Convergance Check sub 2
sln3.Error=max(abs(Ext.P_gen_scaling-Ext.P_gen_scaling_3));
SLN3(i)=sln3;
if(sln3.Error<=0.1)
break
else
% Update weights
Ext.W23=beta*Ext.W23;
Ext.W23List(i) = Ext.W23
end
end

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

David Hill
David Hill am 14 Jul. 2021
for i = 1:100
if ~(sln2.Error<=1)
Ext.W22=beta*Ext.W22;
Ext.W22List(i) = Ext.W22;
end
sln3.Error=max(abs(Ext.P_gen_scaling-Ext.P_gen_scaling_3));
SLN3(i)=sln3;
if (sln3.Error<=0.1)&&(sln2.Error<=1)
break;
end
end
  1 Kommentar
Muhammad Qaisar Fahim
Muhammad Qaisar Fahim am 14 Jul. 2021
Thanks alot this worked . But later on if I have more than 2 statments then I need to add 2nd &&?

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Jan
Jan am 14 Jul. 2021
for i = 1:100
Ext.W22 = beta * Ext.W22;
Ext.W22List(i) = Ext.W22;
%% Convergance Check sub 2
sln3.Error = max(abs(Ext.P_gen_scaling - Ext.P_gen_scaling_3));
SLN3(i) = sln3;
if (sln3.Error <= 0.1) && (sln2.Error <= 1)
break
end
end
  1 Kommentar
Muhammad Qaisar Fahim
Muhammad Qaisar Fahim am 14 Jul. 2021
Bearbeitet: Muhammad Qaisar Fahim am 14 Jul. 2021
Thanks Jan. Your recommendation worked for me

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Tags

Produkte


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by