4) Repetition Control Structure (Part 1)
-
Upload
bear-zoobear -
Category
Documents
-
view
27 -
download
3
description
Transcript of 4) Repetition Control Structure (Part 1)
CSC128
FUNDAMENTALS OF
COMPUTER PROBLEM SOLVING
Topic 4: Repetition Control Structure (Part 1)
Objectives
o Understand the requirement of a loop
o Understand the Loop Control Variable (LVC)
o Use increment (++) and decrement (‐‐) operators
o Understand Counter‐controlled, Sentinel‐controlled and Flag‐controlled structures
o Program loop with for, while statements
Repetition Structure
Many applications require certain operations to be carried out more than once. Such situations require repetition in control flow
Repetition structure is the control structure that allow a sequence of statement to be executed repeatedly until certain condition is reached.
Repetition Structure
Requirement of a repetition structure :- Loop control variable (LCV) – a variable that
determines whether the loop body will be executed.
Loop condition – if the condition is true, the loop body is executed, otherwise the loop exits
Loop body – statement block (statement to be repeated)
Execution of the loop is controlled by 3 operations on LCV:- 1) Initialization of LCV
2) Evaluation of the LCV in loop condition
3) Update LCV by incrementing or decrementing
Shortcut Assignment
C++ has a set of operators for applying an operation to a variable and then storing the result back into the variable
Shortcut assignments: *=, /=, +=, -=, %=
Examples:
int i = 3; i += 4; // i = i + 4 cout << i << endl; // i is now 7 double a = 3.2; a *= 2.0; // a = a * 2.0 cout << a << endl; // a is now 6.4 int change = 1265; change %= 100; // change = change % 100 cout << change << endl; // change is now 65
Increment and Decrement Operator
Increment and decrement operator – unary operator (operator that requires one operand)
Special operators that increment or decrement a variable by one
Two types of increment and decrement operator Preincrement or predecrement operator – operator is
placed before a variable example : ++a increment a by 1, then use
the new value of a. --b decrement b by 1, then use
the new value of b
Increment and Decrement Operator
Postincrement or postdecrement operator – operator is placed after a variable
example : a++ use the current value of a, then increment by 1
b-- use the current value of b, then decrement by 1
Increment and Decrement Operator
Examples
int a, b, c, d, k;
k = 3;
a = ++k; // k=4, a=4
b = --a; // a=3, b=3
c = b++; // c=3, b=4
d = c--; // d=3, c=2
Increment and Decrement Operator
Examples
int k = 4;
++k; // k=k+1 : k is 5
k++; // k=k+1 : k is 6
cout << k << endl;
int i = k++; // i is 6, k is 7
cout << i << " " << k << endl;
int j = ++k; // j is 8, k is 8
cout << j << " " << k << endl
Types of Repetition Structure
Three types of repetition structure:
Counter controlled repetition structure is used when
the number of repetition is fixed. – repeat a process for N times.
example : enter mark for 10 students.
This type of repetition structure requires :- The name of a control variable (loop counter)
The initial value of the control variable
The condition that tests for the final value of the control variable.
The increment (or decrement) by which the control variable modified each time through the loop.
Types of Repetition Structure
Sentinel controlled repetition structure is used when the number of repetition is depends on certain condition. – repeat a process until user input a value to terminate the process
sentinel value = a value used to terminate the loop
process example : enter all the positive number
This repetition structure depends on given condition. example : the loop will be terminated when user key
in the value of zero or –999 (to indicate the termination)
Types of Repetition Structure
Flag controlled repetition structure uses a bool variable to control the loop.
General form of flag‐controlled structure:
found = false; //initialize the loop control variable while (!found) //test the loop control variable { … … if (expression) found = true; //update LCV … … }
Flag Control Example
#include<iostream.h>
main()
{
int num;
bool found = false; //initialize the loop control variable
while (!found) //test the loop control variable
{
cout << "Enter num: ";
cin >> num;
if (num == -1)
found = true; //update the LCV
}
}
Types of Repetition Structure
The variable found above, which is used to control
the execution of the while‐loop is called flag variable.
Can use bool, int or char variable.
bool variable:
bool testValue = true;
cout << “The value is true:” << testValue << endl;
testValue = false;
cout << “The value is false:” << testValue << endl;
Types of Repetition Structure
Repetition structure in C+ comes in three form :-
For loop
While loop
Do… while loop
For loop
A counter controlled loop in which a loop variable manages the repetition through counting
Purpose : execute the initialization statement. While the condition remains true, execute the statement and the update expression.
For loop
o Syntax :-
for (initialization_statement; condition; update_expression)
statementBlock;
where :-
initialiazation_statement = initialization of loop control variable
condition = evaluation of loop condition
update_expression = updating of loop control variable
statementBlock = either or single statement or a group of statement
enclosed between braces
For loop
How it works:
Execute initialization_statement
While condition is true
Execute statementBlock
Execute update_expression
Initalize LCV
condition
LCV Statement block Update LCV
false
true
For loop Flowchart
For loop
Example 1 :- int sum =0; for (int i = 1; i <= 10; i++) sum+=i; cout << “the sum is “ <<sum<<endl; Example 2 :- to find summation of 5 numbers entered by user int num, sum = 0; for (int i = 0; i <= 4; i++) { cout < “Enter a number : “; cin >> num; sum+= num; } cout << “The sum is “ << sum << endl;
While loop
o While loop employs a loop pretest, testing the condition before each loop iteration/repetition
o Can apply counter, sentinel and flag control loop o Purpose : Execute the statement while the condition remains true. Syntax :- while (condition) statementBlock; where :- condition = evaluation of loop condition statementBlock = either or single statement or a group of
statement enclosed between braces
While loop
How it works:
If condition is true then execute statementBlock
Repeat this process until condition evaluates to false
Initalize LCV
condition
LCV Statement block Update LCV
false
true
While loop Flowchart
While loop Example
o Example 1 :-
while (x >= 10)
x = sqrt(x);
o Example 2 :- to find summation of 5 numbers entered by user
int num, sum = 0;
int count = 0; initialization of LCV
while (count <= 4) evaluate loop condition
{
cout < “Enter a number : “;
cin >> num;
sum+= num;
count++; update loop LCV
}
cout << “The sum is “ << sum << endl;
o Example 3 :- to find maximum number among 6 numbers entered by user
int x, max = 0;
int count = 0; initialization of LCV
while (count <= 5) evaluate loop condition
{
cout < “Enter a number : “;
cin >> x;
if ( x > max)
max = x;
count++; update loop LCV
}
cout << “The maximum number is “ << max << endl;
While Loop - counter-controlled repetition
While Loop – sentinel-controlled repetition
o A repetition statement in which the loop control condition changes within the body of the loop. It uses end-of-data value called sentinel value that serves as a signal for termination.
o Example 1 :-
while (mark != 999)
{ statementblock; }
o Example 2 :-
char choice;
cout << “Do you want to quit? “;
cin >> choice;
while (choice !=‘Y’)
{
cout <<“HELLO!!! \n”;
cout <<“Do you want to quit? “;
cin >> choice;
}
Test Yourself
Write a program to find summation of 5 integer numbers using:
For loop
While – counter loop
While – sentinel loop
While – flag control loop
END
Thank You