1) Nested for-loops are when you have one (or more) additional loops inside of a
ID: 3726666 • Letter: 1
Question
1) Nested for-loops are when you have one (or more) additional loops inside of a for- loop. Consider the following MATLAB code for i = [12]; for j 112], fpintfCi is: %d, j is: %din,LD; end end Let's examine the first line of code. It is simply afor loop that loops twice, storing the current iteration in the variable i The second for loop is the nested loop. This means that for every one iteration in the outer loop (the i loop), the inner loop (the j loop) will oop until completion, in this case, the j for loop will loop two times for every one i loop. This means that fprintf statements will print the following: i is: 1, j is: 1 is: 1,jis: 2 is: 2, j is:1 i is: 2, j is: 2Explanation / Answer
MATLAB code for 3 nested for loops:
for i = [1:2];
for j = [1:3];
for k = [1:2];
fprintf("i is: %d, j is: %d, k is: %d ",i,j,k);
end
end
end
For each iteration of first loop, second loop runs three times. For each iteration of second loop , third loop runs 2 times.
output will be:
i is: 1, j is: 1, k is: 1
i is: 1, j is: 1, k is: 2
i is: 1, j is: 2, k is: 1
i is: 1, j is: 2, k is: 2
i is: 1, j is: 3, k is: 1
i is: 1, j is: 3, k is: 2
i is: 2, j is: 1, k is: 1
i is: 2, j is: 1, k is: 2
i is: 2, j is: 2, k is: 1
i is: 2, j is: 2, k is: 2
i is: 2, j is: 3, k is: 1
i is: 2, j is: 3, k is: 2
Please Upvote, If you find this helpful.Thank You.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.