0 views

Uploaded by sike1977

save

You are on page 1of 12

**15-110 Summer 2010
**

Margaret Reid-Miller

The for Loop

• Another loop statement, for, is best for when you

can determine in advance how many times you need

to execute the loop (counting loop).

**• The for statement includes the three parts needed
**

for loops: initialize, test, and update.

• All this information is conveniently placed at the

beginning of the loop.

**• All three loop statements (while, do, and for) are
**

functionally equivalent.

**Summer 2010 15-110 (Reid-Miller) 2
**

The for statement

• The form of the for statement is

for (<initialize>; <boolean_expression>; <update>)

<statement>

**• First, the initialize statement is executed.
**

• If boolean_expression evaluates to true, then

statement (body of loop) is executed, followed by the

update statement.

• The loop repeats until the boolean_expression

evaluates to false.

**Summer 2010 15-110 (Reid-Miller) 3
**

The for statement

• The form of the for statement is

for (<initialize>; <boolean_expression>; <update>)

<statement>

• It is equivalent to

<initialize>;

while (<boolean_expression>) {

<statement>

<update>; executed after

} statement

(body of loop)

Summer 2010 15-110 (Reid-Miller) 4

The for Flowchart

initialize

false

boolean_expression

update true

statement

(body of loop)

**Summer 2010 15-110 (Reid-Miller) 5
**

A for Loop Example

n=4

!int sum = 0;! sum i

!for (int i = 1; i <= n; i++) {! 0

! !sum += i*i;! 1

!}! i <= n ?

1

!System.out.println(sum);!

2

i <= n ?

5

Which variable is the loop

3

control variable? i <= n ?

14

4

i <= n ?

30

5

i <= n ?

Summer 2010 15-110 (Reid-Miller) 6

Another for Loop Example

n = 11

sum i

!int sum = 0;! 0

!for (int i = 1; i <= n; i+=3) {! 1

! !sum += i;! i <= n ?

1

!}!

4

!System.out.println(sum);! i <= n ?

5

7

i <= n ?

12

10

i <= n ?

22

13

i <= n ?

Summer 2010 15-110 (Reid-Miller) 7

Scope

**• The scope of a variable is the area within a program that
**

can reference the variable.

• The scope depends on where the variable is declared.!

!int sum = 0;!

!for (int i = 1; i <= n; i++) {!

Scope of

! !sum += i*i;!

!}!

variable i

!System.out.println(sum);!

**Summer 2010 15-110 (Reid-Miller) 8
**

Scope

!int sum = 0;!

!int i;!

!for (i = 1; i <= n; i++) {!

! !sum += i*i;! Scope of

!}! variable i

!System.out.println(“Sum of first “ + (i-1)!

! + “ integers squared is “ + sum);!

**Summer 2010 15-110 (Reid-Miller) 9
**

Nested Loops

• A loop can have another loop inside of it.

• For each iteration of the outside loop, the inside loop

runs completely.

• Often it is easiest to read from the inside out.

• Example:

How many lines are printed?

for (int i = 1; i <= 5; i++) {!

for (int j = 1; j <= 3; j++) {!

System.out.println(i + " " + j);!

}!

} What happens if we

write println(i + j)?

Summer 2010 15-110 (Reid-Miller) 10

Palindromes

• A palindrome is word, phrase, or sequence that reads

the same backwards as forwards.

**• Example: Bob by Weird Al Yankovic
**

(A parody of Bob Dylan's Subterranean Homesick

Blues)

http://www.youtube.com/watch?v=Nej4xJe4Tdg

How would you test whether a string is a palindrome?

**Summer 2010 15-110 (Reid-Miller) 11
**

Which Loops?

• for loops are more natural when we know how many

iterations we need (definite or counting loops).

Examples:

• Print "*" 10 times

• Print the even numbers between 10 and the value of n

• while and do loops are more natural when we want

to keep looping until some outcome (indefinite or

result controlled loops).

Examples:

• Prompt the user until the user inputs the data in the correct

form.

• Continue looping until we reached a million dollars.

Summer 2010 15-110 (Reid-Miller) 12

- AngularJS-Day-#3 & #4Uploaded bygeorge_soosai
- ReactiveUploaded byGetsuga Tenshou
- Errors in CPR (9017) ManualUploaded byapi-3728136
- ListsLoopsAndPrintingUploaded byCeline Tanglao
- Php BasicUploaded bySwapnil Sonar
- Semantic ReportUploaded byKram Alegre Soliva
- Javascript Notes NewUploaded byC Fox
- Fortran 77 Chap 5Uploaded bySaikat Payra
- c Coding StandardsUploaded bykamal.vlsi7997
- Single Page Applications in DepthUploaded bycivanus
- Keywords — Use-packageUploaded bya9d0f8t7
- Eee 241 Lecture 05Uploaded byŞemsettin karakuş
- Scope Language IntroductionUploaded byshikhar
- BPC Currency ConversionUploaded bysuryapadhi_661715971
- csharpUploaded byapi-296188075
- 044262 VHDL SummaryUploaded byNir Chako
- nullUploaded bytech4a
- Appendix b Keywords JavabookUploaded byVeeru Ram
- apisara1999 pythonUploaded byapi-295783327
- Lab4Uploaded byradians042
- exprs_ch5Uploaded byapi-3706602
- An+Easy+Guide+to+Programming+in+-+Gentile%2C+MartinUploaded byhp
- A Thorough Introduction to PythonUploaded byKishor Paul
- 4DGL_progmanual_R_5_3Uploaded byplazitas
- Typescript DeepDiveUploaded bykiles
- x071 05Uploaded byapi-298216893
- Project 1Uploaded bymohsan
- Object Oriented ProgrammingUploaded bySidra Khalid
- Nc02Uploaded bykiran

- 58223main_Is.There.Water.on.Mars.pdfUploaded bysike1977
- takvim_kalendarski_dio_2018.pdfUploaded bysike1977
- Treem NawUploaded bysike1977
- 5.40.3App2EvaluationClassificationPositionUploaded bysike1977
- MSDS-SINO-GARNET-2014XNG-Aug-2016.pdfUploaded bysike1977
- Msds Sino Garnet 2014xng Aug 2016Uploaded bysike1977
- MAXITAXIUploaded bysike1977
- GarnetUploaded bysike1977
- takvim_kalendarski_dio_2018.pdfUploaded bysike1977
- D-D-R.pdfUploaded bysike1977
- bpb_312 DDR_gesamt_ES_20130110.pdfUploaded bysike1977
- Takvim 2018Uploaded bysike1977
- TeerUploaded bysike1977
- TREEMNAW.pdfUploaded bysike1977
- TweepUploaded bysike1977
- TttUploaded bysike1977
- sc-22Uploaded bysike1977
- QwrUploaded bysike1977
- A2Uploaded bysike1977
- DDR_retrospective.pdfUploaded bysike1977
- En Twa EsserUploaded bysike1977
- DDR MUploaded bysike1977
- AZUTOUploaded bysike1977
- JASINUploaded bysike1977
- XUploaded bysike1977
- UNDDRUploaded bysike1977
- DDRFinal3Uploaded bysike1977
- klokanUploaded bysike1977
- terexUploaded bysike1977

- LaplaceUploaded byFrancisco Bajo García
- Conversiones de Radianes a GradosUploaded byRicardo Melchor Santa Fe
- Data Structure and AlgorithmsUploaded byየዓለምዘውድ መኮንን
- Quine's Platonism and Antiplatonism (Srecko Kovac)Uploaded byGramo S World
- Ppts Fisica Aplicada FINALUploaded bypera38
- Lecture 12Uploaded bynaymyothwin
- Ejercicios en R3Uploaded bymiguel tarazona
- Nigerian company model_Product mix.pdfUploaded byHarsh Saxena
- Lecture Notes on Discrete MathematicsUploaded byMythili Duraisami
- Radicales I -3° secUploaded byNarda Marcela Piminchumo Grau
- The Mathematics Any Physicist Should KnowUploaded byVetPhoenix Stk
- Basics of Approximation AlgorithmsUploaded byFakee Zimbo
- Lagrange (2).pdfUploaded bycarolina
- Elements of Mathematical LogicUploaded byGtk X Hunter
- fasciculo_03Uploaded bymatiati
- Proyeccion de Poblacion Para Formulacion de ProyectosUploaded byJhuber Franco Cardenas Choque
- Normas de La UpelUploaded byMayuriJGomezR
- Actividad 1 Curso Uso Eficiente de La Biblioteca Virtual Ing Juan Ignacio Arreola Tecmilenio Campus DurangoUploaded bymhm_025
- De Rham-Hodge Theory Lecture NotesUploaded bySteven Scott
- Aljebraic FractionsUploaded byFuad Quazi
- ÁNGULO TRIGONOMETRICOUploaded byManuel Campos Tesen
- EE105LectureNotes(9)Uploaded bythinkberry22
- Unidad didáctica: La elipseUploaded byjosemoralesreyes93
- Bab 5.1Uploaded byEdward Prananta Barus
- Chap5 SlidesUploaded bysrinivas
- METODOLOGÍA DE ANÁLISIS series de tiempo.docxUploaded byNathan Hernandez
- Soma Subtracao BinariosUploaded bydebora_chagas
- Nother CurrentUploaded byNachiketa Sarkar
- Calculus : Differentiation RulesUploaded byAoSd' Tikman
- Eixo 2 - Tema 1Uploaded bySoulyoga