1//<-- CLI SHELL MODE --> 2// ============================================================================= 3// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab 4// Copyright (C) ????-2008 - INRIA Michael Baudin 5// 6// This file is distributed under the same license as the Scilab package. 7// ============================================================================= 8// 9// for.tst -- 10// Test "for" for several data types : integer, string, vector, matrix, 11// polynomials, and complex polynomials. 12// 13// Loop over an integer range 14computed=0; 15for i=1:5 16 computed=computed+i; 17end 18assert_checkequal(computed, 1+2+3+4+5); 19// Loop over a vector of integers 20computed=0; 21values=[1 3 5 7 9]; 22for i=values 23 computed=computed+i; 24end 25assert_checkequal(computed, 1+3+5+7+9); 26// Loop over an integer with n1:step:n2 syntax 27computed=0; 28step=2; 29for i=1:step:10 30 computed=computed+i; 31end 32assert_checkequal(computed, 1+3+5+7+9); 33// Loop over a vector of strings (test also concatenation of strings) 34computed=""; 35values=["this+" "is+" "my+" "string"]; 36for i=values 37 computed=computed+i; 38end 39assert_checkequal(computed, "this+is+my+string"); 40// Loop over a row vector of real values 41computed=0.; 42values=[1. 2. 3. 4. 5.]; 43for x=values 44 computed=computed+x; 45end 46assert_checkequal(computed, 1.+2.+3.+4.+5.); 47// Loop over a list of real vectors 48computed=[0. 0. 0.]; 49mylist=list([1. 2. 3.],[4. 5. 6.],[7. 8. 9.]); 50for v=mylist 51 computed=computed+v; 52end 53assert_checkequal(computed, [(1.+4.+7.) (2.+5.+8.) (3.+6.+9.)]); 54// Loop over a vector of real polynomials 55computed=0.; 56myvector=[(1.+2*%s) (3.+4.*%s)]; 57for v=myvector 58 computed=computed+v; 59end 60assert_checkequal(computed, 4.+6.*%s); 61// Loop over a vector of complex polynomials 62computed=0.; 63p1=1.+2.*%i+3*%s; 64p2=4.+5.*%i+6*%s; 65myvector=[p1 p2]; 66for p=myvector 67 computed=computed+p; 68end 69assert_checkequal(computed, 5.+7*%i+9*%s); 70// Loop over a column vector of real values 71// Note : there is only one loop here 72computed=[0;0;0;0;0]; 73values=[1.;2.;3.;4.;5.]; 74for x=values 75 computed=computed+x; 76end 77assert_checkequal(computed, [1.;2.;3.;4.;5.]); 78// Loop over a matrix of real values 79// Note : the loop is over the columns 80computed=[0;0]; 81values=[1. 2.;3. 4.]; 82for x=values 83 computed=computed+x; 84end 85assert_checkequal(computed, [3.;7.]); 86