Postingan

Menampilkan postingan dari Maret, 2020

control statement

Gambar
Loop Control Statements With loop control statements, you can repeatedly execute a block of code. There are two types of loops: for  statements loop a specific number of times, and keep track of each iteration with an incrementing index variable. For example, preallocate a 10-element vector, and calculate five values: x = ones(1,10); for n = 2:6 x(n) = 2 * x(n - 1); end while  statements loop as long as a condition remains true. For example, find the first integer  n  for which  factorial(n)  is a 100-digit number: n = 1; nFactorial = 1; while nFactorial < 1e100 n = n + 1; nFactorial = nFactorial * n; end Each loop requires the  end  keyword. It is a good idea to indent the loops for readability, especially when they are nested (that is, when one loop contains another loop): A = zeros(5,100); for m = 1:5 for n = 1:100 A(m, n) = 1/(m + n - 1); end end You can programmatically exit a loo...