12.07.2015 Views

Basics of MATLAB and Beyond

Basics of MATLAB and Beyond

Basics of MATLAB and Beyond

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

SwitchThe basic form <strong>of</strong> a switch statement is:switch testcase result1statementscase result2statements...otherwisestatementsendThe respective statements are executed if the value <strong>of</strong> test is equalto the respective result s. If none <strong>of</strong> the cases are true, the otherwisestatements are done. Only the first matching case is carried out. Ifyou want the same statements to be done for different cases, you canenclose the several result s in curly brackets:switch xcase 1disp(’x is 1’)case {2,3,4}disp(’x is 2, 3 or 4’)case 5disp(’x is 5’)otherwisedisp(’x is not 1, 2, 3, 4 or 5’)endWhileThe basic form <strong>of</strong> a while loop iswhile teststatementsendThe statements are executed repeatedly while the value <strong>of</strong> test isequal to 1. For example, to find the first integer n for which 1+2+···+nis is greater than 1000:n = 1;while sum(1:n)

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!