This document discusses loops in MATLAB. It begins with an introduction to loops, explaining that they are used to repeatedly execute a block of code until a condition is met. It then covers the basic structure of loops and the different types of loops in MATLAB - for loops, while loops, and nested loops. The document provides syntax examples for for and while loops. It also compares while and for loops. Finally, it poses several programming exercises involving loops, such as calculating the Fibonacci series, checking if a number is palindrome, perfect, prime, etc.
3. Introduction
A loop is used for executing a block of
statements repeatedly until a particular
condition is satisfied.
For example, when you are displaying number from
1 to 100 you may want set the value of a variable
to 1 and display it 100 times, increasing its value
by 1 on each loop iteration.
4. Introduction
A loop is used for executing a block of
statements repeatedly until a particular
condition is satisfied.
For example, when you are displaying number from
1 to 100 you may want set the value of a variable
to 1 and display it 100 times, increasing its value
by 1 on each loop iteration.
5. Basic Structure of a Loop Statement
A loop is used for
executing a block
of statements
repeatedly until a
particular
condition is
satisfied.
6. Types of Loops
Loops are of 2 types:
Entry-controlled and exit-controlled.
'C' programming provides us
1) while
2) for loop
3) Nested Loop
For and while loop are also called as entry-
controlled loops.
7. Questions to be Asked ??
In the exam questions can be asked are
1) What do you mean by loop control
statement explain in detail.
Answer: Must include: syntax, example,
flowchart, entry control and exit
control loop difference
2) Write a program ………..
9. for Loop Example
for index = values, statements, end executes a group of statements in a
loop for a specified number of times. values has one of the following
forms:
initVal:endVal — Increment the index variable from initVal to endVal by
1, and repeat execution of statements until index is greater than
endVal.
initVal:step:endVal — Increment index by the value step on each
iteration, or decrements index when step is negative.
valArray — Create a column vector, index, from subsequent columns of
array valArray on each iteration. For example, on the first iteration,
index = valArray(:,1). The loop executes a maximum of n times, where
n is the number of columns of valArray, given by numel(valArray(1,:)).
The input valArray can be of any MATLAB data type, including a
character vector, cell array, or struct.
15. Programming Exercise
Q.3 WAP to Check entered number is magic number
(Condensing a number to single digit, if the yielded single digit is one(1) then it is termed as magic number)