Main Content

Loops and Conditional Statements

Control flow and branching using keywords, such as if, for, and while

Within any program, you can define sections of code that either repeat in a loop or conditionally execute. Loops use a for or while keyword, and conditional statements use if or switch. Additional keywords provide finer control over the program flow.

Sintassi del linguaggio MATLAB

if, elseif, elseExecute statements if condition is true
switch, case, otherwiseExecute one of several groups of statements
forfor loop to repeat specified number of times
whilewhile loop to repeat when condition is true
try, catchExecute statements and catch resulting errors
breakTerminate execution of for or while loop
returnReturn control to invoking script or function
continuePass control to next iteration of for or while loop
pauseStop MATLAB execution temporarily
parforParallel for loop
endTerminate block of code or indicate last array index


Conditional Statements

To determine which block of code to execute at run time, use if or switch conditional statements.

Loop Control Statements

To repeatedly execute a block of code, use for and while loops.