Lecture-8

Programming Fundamental
Instructor Name:
Lecture-8
Today’s Lecture
 Repetition Structure (Loops) continued
 Do-while loop
2
Repetition Structure
Property of While Loop
 The property of while loop is that the body of while loop may execute zero
times or more.
 The reason is that the condition of while loop is first evaluated and then
the body of while loop executes.
 But we may face situations where we need to perform some task (body of
while loop) at-least once.
 Guess some situations
?
3
Repetition Structure
do-while Statement
 Let's consider a scenario where we want to take an integer input from user
until user have entered a positive number. In this case we will use a dowhile loop like this.
do
{
TakeInputFromUser()
}
while(input < 0)
 In this case we have to run loop at-least once because we want input from
user at-least once. This loop will continue running until user enters a
positive number.
4
Repetition Structure
do-while Statement – Flow Chart
5
Repetition Structure
do-while Statement Syntax
do
statement
while( condition );
VS
do
{
statement
}
while( condition );
It is good programming practice to always including braces in a
do...while statement helps eliminate ambiguity between the while
statement and the do...while statement containing one statement.
6
Repetition Structure
do-while Statement
 Consider another scenario where
a computer program has a character
stored from a-z. It gives to user five chances or tries to guess the character.
In this case, the task of guessing the character must be performed at least
once. To ensure that a block of statements is executed at least once, do
while loop is perfect choice
 In this case we have to run loop at-least once because we want input
(guess) from user to check whether the character is guessed or not.
 Broadly speaking, in while loop, the condition is tested at the beginning of
the loop before the body of the loop is performed. Whereas in do-while
loop, the condition is tested after the loop body is performed.
8
 Therefore, in do-while loop, the body of the loop is executed at least once.
Repetition Structure
Problem Analysis
 A character in the program to be guessed by User
 Lets call this character is ‘z’
 User is allowed to guess it in maximum five (5) attempts, so we need to
track the number of attempts a user made to guess the character
 We need a variable to store the number of tries, lets call it tryNum. It
would be of type int.
 Program prompts user to guess a character, we will store this character in a
variable, lets call it c. it would be of type char.
 A character can be assigned as c=‘a’;
9
Repetition Structure
Problem Analysis
 After getting the character in variable c we need to compare it with our
character that is ‘z’ in the program
 To keep track of number of attempts we will use if/else structure
 If character is same as we have in our program a message will appear to
congratulate the user other wise in the else clause we will add 1 in the
counter variable
 In the while condition we will check the counter value if it is less than or
equals to 5 we will ask user to guess character again
 Once the character is matched we must exit the loop
 Lets have a look on Flow chart of program
10
Repetition Structure
Problem/Scenario – Flow Chart
11
Repetition Structure
Program Code
using namespace std;
#include<iostream>
void main ( ) {
int tryNum = 0 ;
char c ;
do{
cout << “Please enter a character between a-z for guessing : “ ;
cin >> c ;
if ( c == ‘z’){
cout << “Congratulations, Your guess is correct” ;
tryNum = 6;
}
else {
tryNum = tryNum + 1;
}
} while ( tryNum <= 5);
}
12
Repetition Structure
Problem Solution
 There is an elegant way to exit the loop when the correct number is
guessed
 A change in the while condition is required to exit program elegantly
 The condition will check whether the number of tries is less than or equals
to five and the variable c is not equals to ‘z’
 Our while condition will now become a compound statement by using
logical &&
 When one of the condition in compound condition will become false the
control will exit the loop
 The change code will look like this:
13
Repetition Structure
Program Code
using namespace std;
#include<iostream>
void main ( ) {
int tryNum = 0 ;
char c ;
do{
cout << “Please enter a character between a-z for guessing : “ ;
cin >> c ;
if ( c == ‘z’){
cout << “Congratulations, Your guess is correct” ;
}
else {
tryNum = tryNum + 1;
}
} while ( tryNum <= 5 && c != ‘z’);
}
14
Increment Operator
What is Increment Operator
 An increment operator is used to increment the value of a variable by one
(1).
 We have seen an increment statement in the previous slide like this:
tryNum = tryNum + 1;
 This form of statement is very common and it is used in every repetition
structure
 The C++ provides a unary operator that increased the value of its operand
by 1
 This operator is called increment operator and is written as ++
 The above statement can be replaced with the statement
tryNum++;
15
Decrement Operator
What is Decrement Operator
 The statement tryNum++ increments the value of variable tryNum by 1
 There is another operator called decrement operator and it used the
operator - A decrement operator is used to decrement the value of a variable by one
(1).
 For example we have a statement that decrements the value of variable x
is written as :
x = x - 1;
 The above statement can be replaced with the statement
x--;
16
Increment Operator
Categories of Increment Operator
 An increment operator is categories as
 Prefix increment
 Post fix increment
 Similarly for decrement we have prefix decrement and postfix decrement
 Apparently the difference is that in case of pre increment the operator ++
is placed before variable while in case of post increment operator ++ is
placed after variable
 Both of these cases are equivalent if we are simply incrementing the value
of a variable. Then it doesn’t matter that we are doing pre increment or
post increment
17
Increment Operator
Difference of Pre & Post Increment
 The difference of pre and post increment matters when we used them in a
expression where it is used to assign value to anther variable
 Here the used of these operators is very tricky and you must use them
carefully
 Let suppose we have a variable j and we use pre increment (++j) , the
value of j is first increased by 1 and then it will be used in the expression
 If we use post increment (j++), the value of j is used in expression and after
than the value of j is incremented by 1.
 If j=5 , we write the expression
x=++j;
 What would be the value of x?
18
Increment Operator
Difference of Pre & Post Increment
19
Increment/Decrement Operator
Things to remember
 Increment /Decrement operator should not be used on constant
We cannot use increment /decrement operator on the constant values
because increment /decrement operator operates on only variables. It
increments /decrements the value of the variable by 1 and stores the
incremented/decremented value back to the variable
 Increment and Decrement operator only increases or decreases the value of
variable by 1.
 If you want to increase or decrease the value by a number other than one
for example you want j=j+5
 C++ provides operators to perform such kind of tasks in short. These
operators are called compound assignment operator.
 Compound Assignment Operator do two things they perform an action
(addition, subtraction, multiplication, division, etc) and do some
assignment.
20
Increment/Decrement Operator
Problems
 What would the value of x if, x=0,y=0
x=++y;
x=y++;
x+=y++;
x+=++y;
x=y--;
x=y++ + ++y + y + y--;
x=--y + --y + --y + --y + --y;
x=++y + y++ + ++y + ++y + y++;
x*=y++ + ++y;
x=x + x++ + y++ + ++y + ++x + y++;
21
22