Lecture_08___Loops
Lecture_08___Loops
Loops
Nishatnagar, Turag,
Dhaka-1230, Bangladesh
1 Loops
2 for Statement
3 Control Statements
4 Nested Loops
5 while Statement
6 do-while Statement
7 goto Statement
8 Programming Problems
9 Summary
2
Loops
3
Outlines
1 Loops
2 for Statement
3 Control Statements
4 Nested Loops
5 while Statement
6 do-while Statement
7 goto Statement
8 Programming Problems
9 Summary
4
for Statement
for statement is one of C’s three loop statements. It is typically used when the
number of iterations is predetermined. The general form of the for statement is:
The expressions init exp , cond exp, and update exp can be any valid C expressions.
5
for Statement
6
for Statement
Example
#i n c l u d e < s t d i o . h>
i n t main ( )
{
int a;
f o r ( a = 0 ; a < 5 ; a++)
{
p r i n t f ( ”%d ” , a ) ;
}
return 0;
}
As with the if statement, if the loop body consists of a single statement, the braces
can be omitted. 7
for Statement
Expressions in the for statement can contain more than one statement when we use
the comma (,) operator.
int a , b;
f o r ( a = 1 , b = 2 ; b < 1 0 ; a++, b++)
Here, the first expression assigns values to both a and b, and the last one increases
both of their values.
8
for Statement
Common Error
As with if statement, a common error is to add accidentally a semicolon (;) at the
end of the for statement.
#i n c l u d e < s t d i o . h>
i n t main ( )
{
int i ;
f o r ( i = 2 ; i < 7 ; i +=3) ;
p r i n t f ( ”%d\n” , i ) ;
return 0;
}
Here, the first expression assigns values to both a and b, and the last one increases
both of their values.
9
for Statement
Omitting Expressions
The three expressions of a for statement are optional. In fact, C allows us to omit any
or all of them.
int a = 0;
f o r ( ; a < 5 ; a++)
10
for Statement
Omitting Expressions
If the conditional expression is missing, the compiler treats it as always true, and the
for loop never ends. Such a loop is called infinite loop.
f o r ( a = 0 ; ; a++)
for ( ; ; )
If both the first and third expressions are missing, the for statement is equivalent to a
while statement.
f o r ( ; a < 5 ; ) /∗ i s e q u i v a l e n t t o ∗/
w h i l e ( a < 5)
11
Practice Problems
Write a program that reads the grades of 10 students and displays those within
[5, 10].
1 Loops
2 for Statement
3 Control Statements
4 Nested Loops
5 while Statement
6 do-while Statement
7 goto Statement
8 Programming Problems
9 Summary
13
break Statement
break statement used to terminate a for , while, or do−while loop and transfer control
to the first statement after the loop.
Example
#i n c l u d e < s t d i o . h>
i n t main ( )
{
int i ;
f o r ( i = 1 ; i < 1 0 ; i ++)
{
i f ( i == 5 )
break ;
p r i n t f ( ”%d ” , i ) ;
}
p r i n t f ( ”End = %d\n” , i ) ;
return 0;
}
14
continue Statement
The continue statement can be used within a for , while, or do−while loop. While the
break statement completely terminates a loop, the continue statement terminates the
current loop iteration and continues with the next iteration, skipping the rest of the
statements in the loop body.
Example
#i n c l u d e < s t d i o . h>
i n t main ( )
{
int i ;
f o r ( i = 1 ; i < 1 0 ; i ++)
{
i f ( i < 5)
continue ;
p r i n t f ( ”%d ” , i ) ;
}
return 0;
} 15
Outlines
1 Loops
2 for Statement
3 Control Statements
4 Nested Loops
5 while Statement
6 do-while Statement
7 goto Statement
8 Programming Problems
9 Summary
16
Nested Loops
When an iteration loop is included in the body of another loop, each iteration of the
outer loop triggers the full completion of the nested loop.
#i n c l u d e < s t d i o . h>
i n t main ( )
{
int i , j ;
f o r ( i = 0 ; i < 2 ; i ++)
{
p r i n t f ( ”One ” ) ;
f o r ( j = 0 ; j < i ; j ++)
p r i n t f ( ”Two ” ) ;
}
return 0;
}
17
Nested Loops
The break statement always terminates the loop in which it belongs to.
#i n c l u d e < s t d i o . h>
i n t main ( )
{
int i , j ;
f o r ( i = 0 ; i < 2 ; i ++)
{
f o r ( j = 0 ; j < 2 ; j ++)
{
i f ( i+j == 1 )
break ;
p r i n t f ( ”Two ” ) ;
}
p r i n t f ( ”One ” ) ;
}
p r i n t f ( ” \ n V a l 1 = %d V a l 2 = %d\n” , i , j ) ;
18
}
Practice Problems
19
Practice Problems
Write a program that reads an integer and displays the prime numbers that are less
than or equal to it.
20
Outlines
1 Loops
2 for Statement
3 Control Statements
4 Nested Loops
5 while Statement
6 do-while Statement
7 goto Statement
8 Programming Problems
9 Summary
21
while Statement
The while statement is the simplest way to create iteration loops in C. It is mostly
used when the number of the iterations is unknown.
while ( condition )
{
/∗ b l o c k o f s t a t e m e n t s ( l o o p body ) t h a t i s r e p e a t e d l y e x e c u t e d a s
l o n g a s t h e c o n d i t i o n r e m a i n s t r u e . ∗/
}
22
while Statement
The following program uses a while statement to print the integers from 1 to 10.
#i n c l u d e < s t d i o . h>
i n t main ( )
{
int i = 1;
w h i l e ( i <= 1 0 )
{
p r i n t f ( ”%d\n” , i ) ;
i ++;
}
return 0;
}
23
while Statement
The following program uses a while statement to print the integers from 10 down to 1.
#i n c l u d e < s t d i o . h>
i n t main ( )
{
i n t i = 10;
w h i l e ( i != 0 )
{
p r i n t f ( ”%d\n” , i ) ;
i −−;
}
return 0;
}
24
Practice Problems
Write a program that reads an integer continuously and displays them. If the user
enters 0, the insertion of numbers should terminate. Note that the number 0 must not
be displayed.
Write a program that reads a float number (i.e., a) and an integer (i.e., b) and displays
the result of ab . 25
Outlines
1 Loops
2 for Statement
3 Control Statements
4 Nested Loops
5 while Statement
6 do-while Statement
7 goto Statement
8 Programming Problems
9 Summary
26
do-while Statement
The do−while statement is similar to the while statement except that the value of
condition is checked after each execution of the loop body, not before. Therefore, a
do−while loop is executed at least once.
do
{
/∗ b l o c k o f s t a t e m e n t s ( l o o p body ) t h a t i s e x e c u t e d a t l e a s t o n c e
and t h e n r e p e a t e d a s l o n g a s t h e c o n d i t i o n r e m a i n s t r u e . ∗/
} while ( condition ) ;
27
do-while Statement
The following program uses the do−while statement to display the integers from 1 to
10.
#i n c l u d e < s t d i o . h>
i n t main ( )
{
int i = 1;
do
{
p r i n t f ( ”%d\n” , i ) ;
i ++;
} w h i l e ( i <= 1 0 ) ;
return 0;
}
28
Practice Problem
Write a program that reads an integer N > 3 and calculates the result of the
expression R1 = 1/1 − 1/2 + 1/3 − 1/4 + ... 1/N. The program should force the
user to enter an integer greater than 3.
29
Outlines
1 Loops
2 for Statement
3 Control Statements
4 Nested Loops
5 while Statement
6 do-while Statement
7 goto Statement
8 Programming Problems
9 Summary
30
goto Statement
• The goto statement is used to transfer control to another statement within the
same function, provided that this statement has a label.
• Its syntax is -
goto location ;
• When the goto is executed, the program transfers control to the statement that
follows the location label.
• The label’s name must be unique in the function where the goto statement is
used.
31
goto Statement
• The label is placed at the beginning of the target statement and its name must be
followed by a colon (:).
#i n c l u d e < s t d i o . h>
i n t main ( )
{
i n t i , num ;
START :
f o r ( i = 0 ; i < 5 ; i ++)
{
p r i n t f ( ” E n t e r number : ” ) ;
s c a n f ( ”%d” , &num ) ;
i f ( num == −1)
g o t o START ;
}
return 0;
}
32
goto Statement
• Usually, it is better to avoid the use of goto because the transition of the
program’s execution from one point to another and then to another and so on
leads to obscure, hard-to-read and complex code that is hard to maintain.
• However, there are cases where goto statement can be helpful, such as when
exiting from nested for or switch statements, as in this example:
f o r ( i = 0 ; i < 1 0 ; i ++)
f o r ( j = 0 ; j < 2 0 ; j ++)
f o r ( k = 0 ; k < 3 0 ; k++)
{
i f ( condition )
g o t o NEXT ;
}
NEXT :
...
33
Outlines
1 Loops
2 for Statement
3 Control Statements
4 Nested Loops
5 while Statement
6 do-while Statement
7 goto Statement
8 Programming Problems
9 Summary
34
Home Works
35
Home Works
36
Home Works
37
Home Works
38
Home Works
39
Home Works
Write a program that displays the sum of all numbers from 1 to 200.
Write a program that displays the product of all odd numbers from 1 to 20.
Write a program that reads an integer and displays its multiplication table.
Write a program that reads an integer and displays a message to indicate whether it is
prime or not.
Write a program that reads an integer and displays the number of its digits and their
sum.
Write a program that reads a positive integer and displays the maximum positive
integer n for which the sum 12 + 22 + 32 + ... + n2 is less than the given number. 40
Home Works
What is the output of the following program?
#i n c l u d e < s t d i o . h>
i n t main ( )
{
i n t i = −2;
w h i l e ( i −6)
{
p r i n t f ( ”One ” ) ;
i ++;
w h i l e ( ! ( i +1) )
{
p r i n t f ( ”Two ” ) ;
i −−;
}
i += 2 ;
}
}
41
Home Works
Write a program that reads two integers (i.e., M, N) and produces an M×N grid.
Each grid cell should be 3×2 characters wide. As an example, a 3×5 grid follows:
+−−+−−+−−+−−+−−+
| | | | | |
+−−+−−+−−+−−+−−+
| | | | | |
+−−+−−+−−+−−+−−+
| | | | | |
+−−+−−+−−+−−+−−+
The three horizontal characters of each cell should be + − − and the two verticals +|
43
Home Works
Write a program that reads an integer (i.e., N) and displays the result of:
1 1 1 1
+ + + ... +
1∗3 3∗5 5∗7 (N − 2) ∗ N
The program should force the user to enter an odd integer greater or equal to 3.
44
Outlines
1 Loops
2 for Statement
3 Control Statements
4 Nested Loops
5 while Statement
6 do-while Statement
7 goto Statement
8 Programming Problems
9 Summary
45
Summary
• for Statement
• break Statement
• continue Statement
• Nested Loops
• while Statement
• do−while Statement
• goto Statement
46
Thank You!