0% found this document useful (0 votes)
2 views13 pages

Conditionals and Loops

The document discusses conditional statements and loops in programming, emphasizing their importance in controlling program flow. It explains the structure of 'if', 'if-else', 'for', and 'while' loops, along with examples of their usage. Additionally, it mentions the 'continue' and 'break' commands in MATLAB for managing loop execution.

Uploaded by

Ginn S. Sales
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views13 pages

Conditionals and Loops

The document discusses conditional statements and loops in programming, emphasizing their importance in controlling program flow. It explains the structure of 'if', 'if-else', 'for', and 'while' loops, along with examples of their usage. Additionally, it mentions the 'continue' and 'break' commands in MATLAB for managing loop execution.

Uploaded by

Ginn S. Sales
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 13

CONDITIONALS

AND LOOPS
AUGUST 21, 2024
CONDITIONALS

Conditional statements can decide


which statements are executed in the
program. They are an important part of
all programming languages.
CONDITIONAL STATEMENT

if …end
if … else … end
IF … END

false if
logical
If the logical expression is true,
then execute some statements,
true
If the logical expression is false,
statements then nothing gets executed.

en
d
IF … ELSE… END

If the logical expression is true,


false if then execute some statements,
logical
els
If the logical expression is false,
e true then executes another another statement.

statements
statements

en
d
CONDITIONAL STATEMENT

Conditional statements affect


the flow of the program.
FOR LOOP
Most mathematical algorithms require
iteration. The workhorses to iterate are the for
loop and the while loop. The for loop usually
iterates the statement a fixed number of
times. For example n = 1:ntimes, inside the
loop, n will take the value of 1,2 , 3 all the way
to n times, and you can use n in expressions
FOR LOOP
Write a for loop to compute the sum of the
squares of all integers from 2 to 20:
2^2 + 3^2 + 4^2 …+ 20^2
WHILE LOOP
The while loop iterates some statements
as long as an expression is true. For
example, while error is greater than
tolerance tol, to iterate until an error falls
below some specified tolerance.
WHILE LOOP
Suppose, starting at his 25th birthday,
Michael deposits $5000 at the beginning
of every year into a retirement annuity
that pays 9% interest per year,
compounded annually He wants to retire
when his annuity first reaches or exceeds
CONTINUE BREAK
MATLAB also provides the command
continue and break to either skip the
remaining statements in a loop, or to
terminate a loop.
CONDITIONAL STATEMENT

Conditional statements affect


the flow of the program.
CONDITIONAL STATEMENT

Conditional statements affect


the flow of the program.

You might also like