循环逻辑中的问题,使用for循环而不是while循环

时间:2017-12-04 09:00:53

标签: matlab if-statement

简单循环直至满足条件

我有Desired value = 10并且我创建一个执行操作的循环意味着它进行计算并运行循环,除非或直到接收的​​值在可接受的范围内。

value_desired = 10
来自代码

received Value = 5 ;

除非或直到收到的值在可接受的范围内(10 + 1 = 11; 10-1 = 9),否则我应该运行循环并执行操作。

问题:

我在else部分

中遇到了问题

如果我的逻辑进入else部分,那么我希望我的程序执行其他部分的任务并转到

J = out  + 4;
received_value_J = J;

然后计算新值并执行任务,除非它满足

if (ii == 1)
value_finalized =  received_value_J;

我想使用for loop。有可能吗?

value_desired = 10 
acceptable_limit_1 = value_desired + 1
acceptable_limit_2 = value_desired - 1
acceptable_limit_upper_range = [value_desired :0.01: acceptable_limit_1];
acceptable_limit_lower_range = [acceptable_limit_2 : 0.01 : value_desired ];

delta = 0.1;
out =  1 

J = out  + 4;
received_value_J = J;

ii =  (received_value_J >= acceptable_limit_lower_range(1) & ... 
       received_value_J <= acceptable_limit_upper_range(end) );

while ((ii ~= 1 ))   
    % Right now using this with while loop but i have commented it to explain the question
    % J = out  + 4;
    % received_value_J = J; 
    %  Range Limit    
    ii =  (received_value_J >= acceptable_limit_lower_range(1) & ...
           received_value_J <= acceptable_limit_upper_range(end) );

    difference_signal = (value_desired -  received_value_J );

    if (ii == 1)
        value_finalized =  received_value_J;    
    else (ii ~= 1)
        Add = delta*difference_signal;
        out =  received_value_J + Add ;    
    end
end

升级说明:

我做过一些事后已经完成了任务并获得了价值。

我们假设received value5

现在,我将使用received value (5) desired value检查此10

如果5limits范围内,则表示5 is greater than 9 and less than 11,然后执行If condition下的任务

否则执行Else condition下的任务并更新值。在我的示例中,更新out

Go to

   J = out + 4;
 received_value_J = J;

找出新的received value

在我的示例中,我将首先获得received_value_J = 5。如果range我会检查9—11 not run else是否在out并升级9.500,现在它将变为9.500

range of 9---11位于<key>NSExtension</key> <dict> <key>NSExtensionAttributes</key> <dict> <key>NSExtensionActivationRule</key> <dict> <key>NSExtensionActivationSupportsFileWithMaxCount</key> <integer>1</integer> <key>NSExtensionActivationSupportsText</key> <true/> </dict> </dict> <key>NSExtensionMainStoryboard</key> <string>MainInterface</string> <key>NSExtensionPointIdentifier</key> <string>com.apple.share-services</string> </dict> ,因此我将输出此值。

我正在使用while循环,这是有效但我的原始代码的问题是,经过很多操作后,我将获得我的接收值,之后我想应用循环但在while循环中我执行所有操作和比较。

0 个答案:

没有答案