0% found this document useful (0 votes)
9 views42 pages

Ch-12 (ICS II) - Loop Constructs

This document covers loop constructs in C programming, detailing various types such as 'while', 'do-while', 'for', and nested loops. It explains the use of control variables, sentinel controlled loops, and statements like 'goto', 'continue', and 'break'. Additionally, it includes assignments for practical implementation of these concepts.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views42 pages

Ch-12 (ICS II) - Loop Constructs

This document covers loop constructs in C programming, detailing various types such as 'while', 'do-while', 'for', and nested loops. It explains the use of control variables, sentinel controlled loops, and statements like 'goto', 'continue', and 'break'. Additionally, it includes assignments for practical implementation of these concepts.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 42

COMPUTER SCIENCE – 12

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

• There is a semicolon (;) at the end of keyword


while
• Indicates the end of do-while loop structure
• Semicolon (;) must be given at the end of this
statement
• otherwise an error will occur
The ‘do-while’ Loop
Difference Between ‘while’ Loop & ‘do-
while’ Loop
The ‘while’ Loop Structure The ‘do-while’ Loop Structure
• Test condition comes before the • Test condition comes after the
body of loop body of loop
• At the beginning of loop, • Condition is evaluated after
condition is evaluated and then executing the body of loop. The
body of loop is executed if body of the loop must be
condition is True executed at least once even if
• Semicolon (;) is not given after the condition is False
the while(condition) • Semicolon(;) is given after the
while(expression)
The ‘do-while’ Loop
The ‘do-while’ Loop
The ‘for’ Loop
Q.5Explain the ‘for’ loop with flowchart.
The ‘for’ Loop
• Execute a statement or set of statements repeatedly for a specified number of times
• Counter loop
• Format of ‘for’ loop is different from both the ‘while’ and the ‘dowhile’ loop structures
• All parts of ‘for’ loop structure are optional
• Syntax of ‘for’ loop structure  for(; ;)
The ‘for’ Loop
The ‘for’ Loop
The Nested Loop
Q.6What is nested loop? Explain with program example.
The Nested Loop
• A loop inside the body of another loop
• Outer loop  loop that contains another loop in its
body
• Inner loop  loop used inside the body of outer loop
• Nesting can be done up to any level
• As the level of nesting increases, nested loop
becomes more complex
• Any loop structure can be placed inside the body of
any other loop
• For example, a “for” loop can be placed inside the body
of “while” loop and vice versa
The Nested Loop
The Nested Loop
The Nested Loop
Sentinel Controlled Loop
Q.7Define sentinel value. What is sentinel controlled loop and how it is
implemented? Discuss some of the situations where it can be useful.
Sentinel Controlled Loop
Sentinel Value
• Sentinel means lookout or guard or watch
• Some loops require a special unique value from user to terminate loop
• Special value  sentinel value
• Sentinel value is specified in loop condition
• In body of loop, a value is entered into control variable
• Each time body of loop is executed  sentinel value is checked or watched
with the value of control variable
• When value of control variable becomes equal to sentinel value, loop is
terminated
Sentinel Controlled Loop
Sentinel Controlled Loop
• Loop that depends upon sentinel value
• Conditional loop
• Used when programmer does not know in advance the number of repetition of the
loop
• For example: in a superstore,
• Different customers purchase different number of items
• Bills of items purchased by customers are prepared through computer
• Computer operator enters data of items one by one to prepare the bill for a particular customer
• When all data of purchased items have been entered, he/she enters a unique value such as -1
to end the process or loop
• Bill is printed
• Repetition of loop is controlled through control variable
• Loop is executed while the value of control variable is not -1 (referred to as sentinel
Sentinel Controlled Loop
• General form of the sentinel controlled loop
• Gets a data value
• Compares the entered data value with sentinel value
• If entered data value is not sentinel value, processes the data value and prompts
the user to enter next data value
• If entered data value is sentinel value, loop is terminated
• Examples of sentinel controlled loop
• A loop that accepts records of students or employees of a college until ‘N’ is
entered
• A loop that accepts menu choices until ‘E’ is entered
• A loop that reads data from a data file until EOF character is reached
Sentinel Controlled Loop
The ‘goto’ Statement
Q.8 Describe the ‘goto’ statement with program example.
The ‘goto’ Statement
• Although the ‘goto’ statement is not used in structured
programming but it is used to clear the concept of control
transfer (or program flow)
• Unconditional control transfer statement
• Transfers the execution control to a specified label in
same function without evaluating any condition
• General syntax  goto label;
• “label” represents a valid C identifier followed by a colon (:) to
which a control is to be transferred
• Can be used with ‘if’ statement to transfer the control
from one part of function to another on conditional basis
• For example, a program is given below to display a
message 5 times using ‘if’ statement and ‘goto’ statement
The ‘continue’ Statement
Q.9Describe the ‘continue’ statement with program example.
The ‘continue’ Statement
• Used in the body of any loop structure
• When this statement is executed, it skips remaining
statements in the body of loop and execution
control is shifted to the beginning of loop
• In case of ‘while’ and ‘do-while’ loop structures,
control shifts to test condition
• Condition is evaluated, if true then control shifts to first
statement of the body of loop
• In case of ‘for’ loop structure, when ‘continue’
statement is executed in  control shifts to
increment/decrement part
• After taking action on increment/decrement part, test
condition is evaluated
• If condition is true  control shifts to first statement of
body of ‘for’ loop and so on
The ‘break’ Statement
Q.10 Describe the ‘break’ statement with program example.
The ‘break’ Statement
• Although ‘break’ statement is used to terminate
a ‘case’ in ‘switch’ structure
• Also used to terminate a loop
• When executed in a ‘while’, ‘do-while’, ‘for’ or
‘switch’ structure
• Execution control exits from that structure and
shifts to the statement that comes immediately
after that structure
• Commonly used in loops for special purposes
• For example, used to terminate a loop when any
key is pressed
Assignment
• Write a program that inputs starting number and ending number from user and then displays
all even numbers in the given range. For example, if starting number is 1 and ending number
is 6. The even numbers of this range are 2, 4, and 6.
• Write a program that displays first five numbers with their squares using while loop. The
output should be as follows:

• 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.

• Write a program to produce the following output:


For more details, and solved assignment refers to

PM Series

Computer Science
ICS Part-II

by
CM Aslam, Aqsa Aslam, Abdur Rehman &
Mudassir Saleem

Publisher: Majeed Sons


22- Urdu Bazar, Lahore

You might also like