In: Electrical Engineering
FOR THE FUNCTION CALLS OR OTHER DEFINED VARIABLES USING THE EXECUTIONS SUCH AS WHILE(), FOR(), IF(), DO() AND ETC LOOPS OR CLASS INHERETANCES
WHAT ARE DIFFERENCES BETWEEN?
ARE THERE DIFFERENCES BETWEEN THE TYPES?
WHAT ARE THE TAKE AWAY POINTS OF THESE MENTIONED?
In while loop a condition is checked first, if the condition is true then a group of statement in the loop is executed while the condition is true. A condition is true when its result is non empty otherwise the condition is false. Here is an exmple of while loop calculating factorial of 10 in Matlab.
n = 10; f = n; while n > 1 % condition check n = n-1; % executing group of statemnt in while loop f = f*n; end % end of loop, condition is checked at the end of loop everytime. disp(['n! = ' num2str(f)])
for loops are used to repeat a specified number of times, for loop consists of a condition check, number of times it must iterate, in while loop the the program would iterate asa long as the condition check results true but in for loop a fixed number is assingned to the number of iterations.
Step by increments of -0.2, and display the values.
for v = 1.0:-0.2:0.0 disp(v) end
1 0.8000 0.6000 0.4000 0.2000 0
if, elseif, else loops execute statement if condition is true. Here is the syntax below:
if expression statement1 elseif expression statement2 else statement3 end
if the first expression is true then statement 1 would be execute, if the first expression is false then else expression would be checked. This loop allows for multiple condition checking.
In a do while loop first the block is executed, and then the condition is checked. If the condition is true the code within the block is executed again. This repeats until the condition becomes false. Whereas the while loop, tests the condition before the code within the block is executed. Here is the syntax for do while loop:
do { do_work(); } while (condition); % condition check at the end.