24
Loops Budditha Hettige Department of Computer Science IT 1033: Fundamentals of Programming

Loops - budditha.files.wordpress.com · 2/6/2015  · •Loop is used to repeat a specific block of code an unknown number of times –do while loops (1 –more times) •A do while

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Loops - budditha.files.wordpress.com · 2/6/2015  · •Loop is used to repeat a specific block of code an unknown number of times –do while loops (1 –more times) •A do while

Loops

Budditha Hettige

Department of Computer Science

IT 1033: Fundamentals of Programming

Page 2: Loops - budditha.files.wordpress.com · 2/6/2015  · •Loop is used to repeat a specific block of code an unknown number of times –do while loops (1 –more times) •A do while

Repetitions: Loops

• A loop is a sequence of instruction s that is

continually repeated until a certain condition is

reached.

• The statement may be repeated

– For a specific number of items

– For an indeterminate number of times, depending

on the truth or falsity of some condition.

Budditha Hettige ([email protected])

Page 3: Loops - budditha.files.wordpress.com · 2/6/2015  · •Loop is used to repeat a specific block of code an unknown number of times –do while loops (1 –more times) •A do while

Loops

• C++ provides three types of loops

– for loops (1- n times)

• Repeat a section of code known number of times

– while loops (0 – more times)

• Loop is used to repeat a specific block of code an unknown number of times

– do while loops (1 – more times)

• A do while loop is a control flow statement that executes a block of code at least once, and then repeatedly executes the block

Budditha Hettige ([email protected])

Page 4: Loops - budditha.files.wordpress.com · 2/6/2015  · •Loop is used to repeat a specific block of code an unknown number of times –do while loops (1 –more times) •A do while

Repeat some work

• Do some repeated work

– Initialization (Start number)

– Condition (do repeat action until satisfy some condition)

– Update (Next Value)

• Example

– Initialization Start with 1

– Condition Count up to 50

– Update Count 1 by 1

Budditha Hettige ([email protected])

Page 5: Loops - budditha.files.wordpress.com · 2/6/2015  · •Loop is used to repeat a specific block of code an unknown number of times –do while loops (1 –more times) •A do while

For Loop• Syntax

for (initialization; condition; update)

{

statement(s)

}

• Example

Initialization

Update

Condition

Budditha Hettige ([email protected])

Page 6: Loops - budditha.files.wordpress.com · 2/6/2015  · •Loop is used to repeat a specific block of code an unknown number of times –do while loops (1 –more times) •A do while

Exercise 6.1

Write a C++ program to find

the factorial of a given

number

Example:

Budditha Hettige ([email protected])

Page 7: Loops - budditha.files.wordpress.com · 2/6/2015  · •Loop is used to repeat a specific block of code an unknown number of times –do while loops (1 –more times) •A do while

Answer

Budditha Hettige ([email protected])

Page 8: Loops - budditha.files.wordpress.com · 2/6/2015  · •Loop is used to repeat a specific block of code an unknown number of times –do while loops (1 –more times) •A do while

Nested for Loops

• A loop can be nested inside of another loop. C++ allows at least 256 levels of nesting.

• Syntaxfor ( init; condition; increment )

{

for ( init; condition; increment )

{

statement(s);

}

statement(s);

}

Budditha Hettige ([email protected])

Page 9: Loops - budditha.files.wordpress.com · 2/6/2015  · •Loop is used to repeat a specific block of code an unknown number of times –do while loops (1 –more times) •A do while

Exercise 6.2

• Write a C++ program to display the following

Multiplication table

Budditha Hettige ([email protected])

Page 10: Loops - budditha.files.wordpress.com · 2/6/2015  · •Loop is used to repeat a specific block of code an unknown number of times –do while loops (1 –more times) •A do while

Exercise 6.3

1. Write a c++ program to print the

following figure

a) * b) 1

** 12

*** 123

**** 1234

***** 12345

Budditha Hettige ([email protected])

Page 11: Loops - budditha.files.wordpress.com · 2/6/2015  · •Loop is used to repeat a specific block of code an unknown number of times –do while loops (1 –more times) •A do while

While Loop

• Allows the repetition of a statement based on the

truth value of a condition

• Can run 0 to infinite times

Budditha Hettige ([email protected])

Page 12: Loops - budditha.files.wordpress.com · 2/6/2015  · •Loop is used to repeat a specific block of code an unknown number of times –do while loops (1 –more times) •A do while

While Loop

• Syntax

while (Condition)

{

statement(s)

}

• ExampleInitialization

Update

Condition

Budditha Hettige ([email protected])

Page 13: Loops - budditha.files.wordpress.com · 2/6/2015  · •Loop is used to repeat a specific block of code an unknown number of times –do while loops (1 –more times) •A do while

Exercise 6.4

• Write a C++ program

to accept numbers

until the user enters a

999 and output the

sum of the given

numbers

Budditha Hettige ([email protected])

Page 14: Loops - budditha.files.wordpress.com · 2/6/2015  · •Loop is used to repeat a specific block of code an unknown number of times –do while loops (1 –more times) •A do while

Exercise 6.5

Create a java program to identify the given number is Palindrome number or not.

– Read number as an integer

– Find the number is Palindrome or not

– Print the results

Hint:A palindromic number or numeral palindrome is a number that remains the same when its digits are reversed. Like 16461

Budditha Hettige ([email protected])

Page 15: Loops - budditha.files.wordpress.com · 2/6/2015  · •Loop is used to repeat a specific block of code an unknown number of times –do while loops (1 –more times) •A do while

Answers

Budditha Hettige ([email protected])

Page 16: Loops - budditha.files.wordpress.com · 2/6/2015  · •Loop is used to repeat a specific block of code an unknown number of times –do while loops (1 –more times) •A do while

Do-while Loop

• do…while loop also depends on a condition, but

unlike while loop, its condition is evaluated at the

bottom of the loop, after the body has already

executed.

Budditha Hettige ([email protected])

Page 17: Loops - budditha.files.wordpress.com · 2/6/2015  · •Loop is used to repeat a specific block of code an unknown number of times –do while loops (1 –more times) •A do while

Do-while Loop

• Syntaxdo

{

statement(s)

}

while (condition);

Example Initialization

Update

ConditionBudditha Hettige ([email protected])

Page 18: Loops - budditha.files.wordpress.com · 2/6/2015  · •Loop is used to repeat a specific block of code an unknown number of times –do while loops (1 –more times) •A do while

Loops comparison

For while do-while

Budditha Hettige ([email protected])

Page 19: Loops - budditha.files.wordpress.com · 2/6/2015  · •Loop is used to repeat a specific block of code an unknown number of times –do while loops (1 –more times) •A do while

Loops comparison

Budditha Hettige ([email protected])

Page 20: Loops - budditha.files.wordpress.com · 2/6/2015  · •Loop is used to repeat a specific block of code an unknown number of times –do while loops (1 –more times) •A do while

Exercise 6.6

Accept numbers until the

user enters a 999 and

output the average of the

given numbers

Budditha Hettige ([email protected])

Page 21: Loops - budditha.files.wordpress.com · 2/6/2015  · •Loop is used to repeat a specific block of code an unknown number of times –do while loops (1 –more times) •A do while

Exercise 6.7

• Using do-while loop, create a java program to

display the selected option in the following menu;

[1] Enter data

[2] Print data

[3] Exit the program

do-while

Switch

Budditha Hettige ([email protected])

Page 22: Loops - budditha.files.wordpress.com · 2/6/2015  · •Loop is used to repeat a specific block of code an unknown number of times –do while loops (1 –more times) •A do while

Answer

Budditha Hettige ([email protected])

Page 23: Loops - budditha.files.wordpress.com · 2/6/2015  · •Loop is used to repeat a specific block of code an unknown number of times –do while loops (1 –more times) •A do while

Exercise 6.8

Write a C++ program to read N number of integers and

find the total and average.

• N is an input 1, 2, 3….. N

• Use for, while and do-while loops

• Draw 3 flow chart for the above 3 programs

Budditha Hettige ([email protected])

Page 24: Loops - budditha.files.wordpress.com · 2/6/2015  · •Loop is used to repeat a specific block of code an unknown number of times –do while loops (1 –more times) •A do while

Exercise 6.9

Write a C++ program to compute the gross pay for an

employee. An employee is paid at hourly rate for the

first 40 hours worked in a week. Any hours worked in

excess of 40 hours are paid at the overtime rate of one

and half times that. Your program should print the pay

sheets of all the employees.

Budditha Hettige ([email protected])