📜  Octave 中的循环(For 和 While)和控制语句

📅  最后修改于: 2022-05-13 01:55:19.823000             🧑  作者: Mango

Octave 中的循环(For 和 While)和控制语句

控制语句是用于根据语句中提供的条件控制程序的执行和流程的表达式。这些结构用于在评估变量后做出决定。在本文中,我们将通过示例讨论 if 语句、for 和 while 循环等控制语句。

如果条件

此控制结构检查括号中提供的表达式是否为真。如果为真,则继续执行语句。
句法 :

if (condition)
    statements
    ...
    ...
end 
(endif can also be used)

例子 :

% initializing variables variable1 and variable2 
variable1 = 20;
variable2 = 20;
   
% check the if condition
if variable1 == variable2,
   
% if the condition is true this statement will execute
disp('The variables are Equal'); 
  
% end the if statement
endif;

输出:

The variables are Equal

if-else 条件

它类似于if条件,但是当if条件中的测试表达式失败时,将执行else条件中的语句。
句法 :

if (condition)
    statements
    ...
    ...
else
    statements
    ...
    ...
end 
(endif can also be used)

例子 :

% initializing variables variable1 and variable2 
variable1 = 20;
variable2 = 40;
  
% check the if condition
if variable1 == variable2,
  
% if the condition is true this statement will execute
disp('The variables are Equal'); 
  
% if the condition is false else statement will execute
else
disp('The variables are Not Equal');
   
%end the if-else statement
end; 

输出:

The variables are Not Equal

if-elseif 条件

当第一个if条件失败时,我们可以使用elseif提供另一个if条件。
句法 :

if (condition)
    statements
    ...
    ...
elseif (condition)
    statements
    ...
    ...
else
    statements
    ...
    ...
end
(endif can also be used)

例子 :

% initializing the variable var
var = 50;
  
% check the if condition
if var < 50,
disp('The variable is less than 50');
  
% check the elseif condition
elseif var > 50,
disp('The variable is greater than 50');
  
% if both the above condition is false else
% statement will execute
else
disp('The variable is 50');
  
% end the if..elseif.. statements
end;

输出:

The variable is 50

for 循环

它是一种循环或重复执行的语句序列,直到达到退出条件。
句法 :

for var = expression
    body
end
(endfor can also be used)

示例 1:打印从 1 到 5 的数字:

% the value of i will move from 1 to 5
% with an increment of 1
for i = 1:5,
  
% displays value of i
disp(i);
  
% end the for loop
end;

输出 :

1
 2
 3
 4
 5

示例 2:带有向量for循环:

% making a column vector from 1 to 10
v = [1; 2; 3; 4; 5; 6; 7; 8; 9; 10];
  
% the value of i will move from 1 to 10 
% with an increment of 1
for i = 1:10,
   
% modifying the value of ith element 
% in the column vector as v(i) * 10 
v(i) = v(i) * 10;
   
% end the for loop
end; 
   
% displays the v vector with modified values
disp(v)

输出 :

10
    20
    30
    40
    50
    60
    70
    80
    90
   100

示例 4:使用for循环打印最多 10 个元素的斐波那契数列的程序:

% create a row vector of 10 elements all as '1' 
fibonacci = ones(1, 10);
  
% the value of i will move from 3 to 10 over the 
% increment of 1
for i = 3:10
  
% the ith term of fibonacci will computed 
% as the sum of its previous 2 terms
    fibonacci(i) = fibonacci(i - 1) + fibonacci(i - 2);
  
% end the for loop
endfor
  
% print the fibonacci series
disp(fibonacci)

输出 :

1    1    2    3    5    8   13   21   34   55

while 循环

while循环是另一种循环,直到满足条件为止。在执行循环体之前首先检查测试表达式。

句法 :

while (condition)
    body
end
(endwhile can also be used)

示例:显示从 1 到 10 的数字:

% initializing the variable i with 1
i = 1;
  
% while condition
while i <= 10
  
% displaying the value of i
disp(i);
  
% make an increment of 1 in the value of i
i = i + 1;
  
% end the while loop
endwhile

输出 :

1
 2
 3
 4
 5
 6
 7
 8
 9
 10

中断语句

它用于退出循环。

示例 1:我们将创建一个行向量,并且只使用break语句修改前 6 个值。

% making a row vector of 1x10, starting from 1 
% and the next value is +10 of it's previous value
v = [1:10:100];
  
% the value of i will move from 1 to 10 
% with an increment of 1
for i = 1:10,
  
% making the ith element in vector to 0
v(i) = 0;
  
% if the condition is true the break statement 
% will execute and the loop will terminate
if i == 6,
break;
  
% end the if condition
end;
  
% end the for loop
end;
  
% displays the modified vector v
disp(v)

输出 :

0    0    0    0    0    0   61   71   81   91

示例 2:带有while循环的break语句:

% initializing the variable i with 1
i = 1;
  
% the while condition is always true
while true
  
% display the value of i
disp(i);
  
% display the below content
disp(" is less then 5");
  
% make an increment of 1 in value of i
i = i + 1;
  
% if the if condition is true loop will break 
if i == 5,
break;
  
% end the if statement
end;
  
% end the while
end;

输出 :

1
 is less then 5
 2
 is less then 5
 3
 is less then 5
 4
 is less then 5