Ch-12 (ICS II) - Loop Constructs
Ch-12 (ICS II) - Loop Constructs
Chapter 12
Loop Constructs
Contents
• Loop
• The ‘while’ Loop
• The ‘do-while’ Loop
• The ‘for’ Loop
• The Nested Loop
• Sentinel Controlled Loop
• The ‘goto’ Statement
• The ‘continue’ Statement
• The ‘break’ Statement
Loop
Q.1What is a loop and loop statement? How many types of loop statements are
available in C? Also describe the use of control variable.
Loop
• Mechanism through which a statement or a set of statements can be executed
repeatedly
• Statements that are executed repeatedly body of loop
• Body of loop can be executed repeatedly for a specified number of times or until
given condition remains true
Loop Statements
• Execute a statement or a set of statements repeatedly
• Important feature of any programming language
• Repetitive structure or iterative structure
• Beginning point and ending point of set of statements that have to be executed
repeatedly are marked
• The ‘while’ loop
• The ‘do-while’ loop
Loop
Control Variable
• A variable, which is used in loop statement to control number of repetition of
loop
• Loop control variable
• Initialized before the body of loop
• Value must be changed inside body of loop or loop structure
• so that repetition of loop can be controlled
The ‘while’ Loop
Q.2What is ‘while’ loop? Explain its working with flowchart and example.
The ‘while’ Loop
• Conditional loop structure
• Execute a statement or a set of statements as long as the given condition remains true
• Used when the programmer does not know in advance the number of repetition of loop
The ‘while’ Loop
The ‘while’ Loop
The ‘do-while’ Loop
Q.3What is ‘do-while’ loop? Explain its working with flowchart and example.
The ‘do-while’ Loop
• Execute a statement or set of statements repeatedly as long as given condition remains
true
• Similar to ‘while’ loop but the body of loop comes before the test condition
• Body of loop must be executed at least once even if the condition is false
• Mostly used for menu selection (or to enter records)
• User has to execute the body of loop at least once
• Write a program that display the sum of the following series using ‘while’ loop.
1+ 1/2 + 1/4 + 1/6 + 1/8 + ………… + 1/100
• Write a program that inputs an integer value and displays the digits of that value in reverse
order. For example, if input value is 1235, it should be displayed as 5321.
• Write a program that displays the first 15 even numbers using ‘while’ loop statement.
• Write a program to display five numbers and their sum using ‘while’ loop.
Assignment
• Write a program that inputs a number and displays the message “Prime number” if it is a prime
number, otherwise displays “Not a prime number”.
• Write a program that displays back-counting from 10 to 1 using ‘do-while’ loop.
• Write a program that inputs starting number and ending number and displays all odd numbers in
the given range using do-while loop. For example, if starting number is 10 and ending number is
15. The odd numbers of this range are 11, 13, and 15.
• Write a program that displays first five numbers with their cubes using do-while loop.
• Write a program that generates following series by using do-while loop.
1, 5, 25, 125, 625
• Write a program that inputs a number and displays the factorial of that number using ‘do-while’
loop.
• Write a program using do-while loop that repeatedly prompts for and takes input until a value in
the range 0 through 15 inclusive is input. The program should add all the values before exiting
the loop and displays their sum at the end.
• Write a program that inputs a number and displays the table of that number using ‘do-while’
loop.
Assignment
• Write a program to calculate and display the sum of the following series using for loop.
x + x2 + x3 +…..+ xn Input the values of ‘x’ and ‘n’ by user.
• Write a program that inputs a number and displays a table of that number using ‘for’ loop.
• Write a program that inputs a number and displays a table of that number in descending order using
‘for’ loop.
• Write a program that inputs a table number and its length. It displays table using ‘for’ loop.
• Write a program that displays a table of equivalent temperatures in Fahrenheit and Celsius from 50F
to 100F with an increment of 5 using ‘for’ loop.
• Write a program that inputs a number and displays factorial of that number using “for” loop
statement.
• Write a program that displays the product of all odd numbers from 1 to 10 using ‘for’ loop.
• Write a program that displays all odd positive integer numbers less 100 by skipping those that are
exactly divisible by 7. Use the for loop In the program.
• Write a program that displays sum of positive odd numbers and the product of positive even numbers
from 1 to 10.
• Write a program that displays the even numbers between 1 to 15 in descending order by using “for”
Assignment
• Write a program that displays the following shapes using nested ‘for’ loop.
PM Series
Computer Science
ICS Part-II
by
CM Aslam, Aqsa Aslam, Abdur Rehman &
Mudassir Saleem