PROGRAMMING
FUNDAMENTALS
LECTURE 01
LOOPS
• Loops cause a section of your program to be repeated a certain
number of times.
• The repetition continues while a condition is true.
• When the condition becomes false, the loop ends, and control
passes to the statements following the loop.
• Each repetition is also referred to as an iteration or a pass
through the loop.
• Real power of programming lies in loops as retyping the same
set of instructions is tedious, time consuming, and subject to
error.
• There are three kinds of loops in C++: the for loop, the while
loop, and the do loop.
4 ELEMENTS OF LOOPS
1. Repetition statements: It defines the boundaries of the
repeating section of code and controls whether the code is
executed. In general, C++ provides three different forms of
repetition statements.
• while • for • do while
2. Each statement must include a condition. i.e. while (no>3).
Valid conditions are identical to those used in selection
statements. If the condition is true, the code is executed;
otherwise, it’s not.
3. The third required element is a statement that initially sets
the condition.
4. Finally, there must be a statement in the repeating section of
code that allows the condition to become false. This
THE FOR LOOP
• The for loop is (for many people, anyway) the easiest C++
loop to understand.
• All its loop control elements are gathered in one place.
• The for loop executes a section of code a fixed number of
times.
• It’s usually (although not always) used when you know, before
entering the loop, how many times you want to execute the
code.
HOW IT WORKS
HOW IT WORKS
• The initialization expression is executed only once, when
the loop first starts.
• It gives the loop variable an initial value.
• The test expression usually involves a relational operator.
• It is evaluated each time through the loop, just before the
body of the loop is executed.
• If the test expression is true, the loop is executed one more
time. If it’s false, the loop ends, and control passes to the
statements following the loop.
• The increment expression changes the value of the loop
variable, often by incrementing it.
• It is always executed at the end of the loop, after the loop
body has been executed.
• For Loop can also be decremental.
FOR EXAMPLE, 2
INFINITE FOR LOOP
Can you suggest other conditions such that
for loop become an infinite loop?
for(int
for(int for(int x=1;x<10;x++,
x=1;x<10;x++);
x=1;x<10;cout<<x,x++); cout<<x);
{ Output:
cout<<“hello\ 123456789
n”; Output:
} 2345678910
Output:
hello
MULTIPLE INITIALIZATION AND TEST EXPRESSION
• You can put more than one expression in the initialization part
of the for statement, separating the different expressions by
commas.
• You can also have more than one increment expression.
• You can have only one test expression. Here’s an example:
• This example has a normal loop variable j, but it also initializes
another variable, alpha, and decrements a third, beta.
MULTIPLE INITIALIZATION AND TEST EXPRESSION
• The variables alpha and beta don’t need to have anything to
do with each other, or with j.
• Multiple initialization expressions and multiple increment
expressions are separated by commas.
• You can leave out some or all the expressions if you want to.
The expression for( ; ; ) is the same as a while loop with a test
expression of true.
THE WHILE LOOP
• The for loop does something a fixed number of times.
• What happens if you don’t know how many times, you want
to do something before you start the loop
• In this case a different kind of loop may be used: the while
loop.
• Like FOR loop, While loop is also called pretest loop as it tests
the condition before each iteration.
HOW IT WORKS
1. Test the expression
2. If the expression has a
non-zero (true) value
a. Execute the statement
following the parentheses
b. Go back to Step 1
else
Exit the while statement and
execute the next executable
statement
following the while
statement
WHILE LOOP EXAMPLE
INFINITE LOOP: Infinite loop is one
that never ends; the program just
keeps displaying numbers until you
realize it isn’t working as you
expected.
While (true)
While(1==1)
While (!true = = !true)
WHILE LOOP EXAMPLE
THREE TYPES OF LOOPS
Counter-Controlled while loop:
When we wish a while loop to execute or iterate N number of times, we use
the expression to test a counter variable value against N and increment the
counter each times the body of loop executes
counter = 0;
while(counter < N)
{
counter++;
}
THREE TYPES OF LOOPS
Sentinel-Controlled loop:
When we do not know how many times to run a loop, but we know that the
last entry will be a unique value, called a sentinel, then we make our
expression check that sentinel value to decide when to stop a loop. In such
a case we must read one value before the while loop so that we have a
valid value to test against the sentinel value.
cin >> variable;
while(variable != sentinel)
{
body of the loop
cin >> variable;
}
THREE TYPES OF LOOPS
Flag-Controlled while loop:
Sometimes neither do we know the number of times to run a loop nor is
there any sentinel value present in the data to check for. However, there
may a condition which if true will signal the loop to stop. In this case, use a
bool variable as a flag to indicate when that condition has occurred so that
the next time the loop is not executed.
isFound = false;
while(!isFound)
{
//body of the loop
if(expression)
isFound = true;
remaining body of the loop
}