can you use a while loop in a while loop in matlab

Here is an example of how to use a while loop in a while loop in MATLAB:

main.m
%initialize variables
outer_count = 1;
inner_count = 1;

%outer while loop
while outer_count <= 3
    fprintf("Outer count = %d\n", outer_count);
    
    %inner while loop
    while inner_count <= 3
        fprintf("\tInner count = %d\n", inner_count);
        inner_count = inner_count + 1;
    end
    
    %reset inner_count
    inner_count = 1;
    outer_count = outer_count + 1;
end
389 chars
19 lines

This program will print the following output:

main.m
Outer count = 1
        Inner count = 1
        Inner count = 2
        Inner count = 3
Outer count = 2
        Inner count = 1
        Inner count = 2
        Inner count = 3
Outer count = 3
        Inner count = 1
        Inner count = 2
        Inner count = 3
264 chars
13 lines

In this example, the outer while loop runs 3 times and the inner while loop runs 3 times for each outer loop iteration.

related categories

gistlibby LogSnag