IT 1033: Fundamentals of Programming Loops Budditha Hettige Department of Computer Science
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.
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
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
For Loop Syntax for (initialization; condition; update) { statement(s) } Example Initialization Condition Update
Exercise 6.1 Write a C++ program to find the factorial of a given number Example:
Answer
Nested for Loops A loop can be nested inside of another loop. C++ allows at least 256 levels of nesting. Syntax for ( init; condition; increment ) { for ( init; condition; increment ) { statement(s); } statement(s); }
Exercise 6.2 Write a C++ program to display the following Multiplication table
Exercise 6.3 1. Write a c++ program to print the following figure a) * b) 1 ** 12 *** 123 **** 1234 ***** 12345
While Loop Allows the repetition of a statement based on the truth value of a condition Can run 0 to infinite times
While Loop Syntax while (Condition) { statement(s) } Example Initialization Condition Update
Exercise 6.4 Write a C++ program to accept numbers until the user enters a 999 and output the sum of the given numbers
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
Answers
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.
Do-while Loop Syntax do { statement(s) } while (condition); Example Initialization Condition Update
Loops comparison For while do-while
Loops comparison
Exercise 6.6 Accept numbers until the user enters a 999 and output the average of the given numbers
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
Answer
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
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.