Loops - Programming for Engineers, Lecture notes of Programming for Engineers

All the topics that the Loops will cover. Unlock it and see it yourself; I will ensure it will help you.

Typology: Lecture notes

2021/2022

Available from 08/29/2022

ggegegege
ggegegege 🇵🇭

45 documents

1 / 10

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Chapter 4: Loops
CS10-8
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Loops - Programming for Engineers and more Lecture notes Programming for Engineers in PDF only on Docsity!

Chapter 4: Loops

CS10-

Introduction to Loops

A basic building block of all programs is to be able to

repeat some code over and over again. Whether it is

updating the bank balances of millions of customers each

night, or sending email messages to thousands of people

programming involves instructing the computer to do

many repetitive actions. In computing, we refer to this

repetitive execution as iteration. In this section, we will

explore some mechanisms for basic iteration.

The While Loop

i = 1; while i < 6 disp(i); i = i + 1; end

The break statement

With the break statement we can stop the loop even if the while condition is true:

For Loop

A for loop is used for iterating over a

sequence

For Loop

fruits = ["apple", "pear", "apricot", "cherry",
"peach"];
for n = 1:
fprintf(fruits(n) + " ");
end

For Loop

Assume you have a list of numbers 12, 10, 32, 3, 66, 17, 42, 99, 20

  1. Write a loop that prints each of the numbers on a new line.
  2. Write a loop that prints each number and its square on a new line.
  3. Write code that uses iteration to print out the length of each element of the list stored in str_list.