0% found this document useful (0 votes)
5 views

Lecture_08___Loops

The document provides an introduction to loops in programming, specifically focusing on C language constructs such as for, while, and do-while statements. It explains how these loops work, including control statements like break and continue, and introduces nested loops. Additionally, it includes programming problems for practice and emphasizes the importance of understanding loop structures in coding.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Lecture_08___Loops

The document provides an introduction to loops in programming, specifically focusing on C language constructs such as for, while, and do-while statements. It explains how these loops work, including control statements like break and continue, and introduces nested loops. Additionally, it includes programming problems for practice and emphasizes the importance of understanding loop structures in coding.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 47

Introduction to Computer & Programming Techniques

Loops

Md. Jahidul Islam


BGMEA University of Fashion and Technology

Nishatnagar, Turag,
Dhaka-1230, Bangladesh

March 29, 2023


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
2
Loops

• Often, programs contain blocks of code to be executed more than once.


• A statement whose job is to repeatedly execute the same block of code as long as
the value of a controlling expression is true is called an iteration loop.
• In this lecture, you’ll learn about C’s iteration statements: for , while, and
do−while, which allow us to set up loops.
• As well as break, continue, and goto statements, which can be used to transfer
control from one point of a program to another.

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:

f o r ( i n i t e x p ; cond exp ; update exp )


{
/∗ a b l o c k o f s t a t e m e n t s ( t h e 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 c o n d e x p e v a l u a t e s t o t r u e . ∗/
}

The expressions init exp , cond exp, and update exp can be any valid C expressions.

5
for Statement

In practice, a for statement works like this:


1 The init exp is executed only once, just before the first iteration. Typically,
init exp initializes a variable used in the other for expressions.
2 The cond exp is evaluated. If its value is false , the for loop terminates. If it is
true, the block of statements, called the loop body, is executed.
3 The update exp is executed to initiate the next loop iteration. Typically,
update exp changes the value of a variable used in cond exp.
4 Steps 2 and 3 are repeated until the value of cond exp becomes false .

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;
}

The output of the aforementioned listing would be 0 1 2 3 4.

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++)

for (a = 0; a < 5;)


{
p r i n t f ( ”%d ” , a ) ;
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 displays the integers from 10 down to 1.

Write a program that reads the grades of 10 students and displays those within
[5, 10].

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 , j = 10;
f o r ( i = 0 ; j < 1 0 ; i ++)
p r i n t f ( ”One\n” ) ;
return 0;
}
12
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
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

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 ( )
{
int i ;
f o r ( i = 0 ; i < 2 ; i ++)
{
p r i n t f ( ”One ” ) ;
f o r ( i = 0 ; i < 2 ; i ++)
p r i n t f ( ”Two ” ) ;
}
p r i n t f ( ” V a l = %d\n” , i ) ;
return 0;
}

19
Practice Problems

Write a program that produces the following output.


*
**
***
****
*****

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 . ∗/
}

while( i != 0) is equivalent to while( i ) and while (! i ) is equivalent to while( i == 0)

Any constant value other than 0 is considered to be always true.

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.

How many times is the next while loop executed?


#i n c l u d e < s t d i o . h>
i n t main ( )
{
i n t a = 256 , b = 4 ;
w h i l e ( a != b )
b = b∗b ;
}

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 ) ;

A do-while statement should end with a semicolon.

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

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 ( )
{
int i , j = 5;
f o r ( i = 0 ; i+j == 5 ; j ++)
{
p r i n t f ( ”One\n” ) ;
i = 4;
j = 1;
}
p r i n t f ( ” V a l 1 = %d V a l 2 = %d\n” , i , j ) ;
return 0;
}

35
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 ( )
{
int i ;
f o r ( i = 3 ; i && i −1; i −−)
p r i n t f ( ”%d\n” , i ) ;
return 0;
}

36
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 ( )
{
int i ;
f o r ( i = 0 ; i ? 0 : i +1; i ++)
p r i n t f ( ”%d\n” , i ) ;
return 0;
}

37
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 ( )
{
int i ;
f o r ( i = 0 ; i < 3 ; p r i n t f ( ”%d ” , ++i ) )
; /∗ Empty b l o c k o f s t a t e m e n t s . ∗/
return 0;
}

38
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 ( )
{
int i ;
f o r ( i = 0 ; p r i n t f ( ”%d” , i ++) < 2 ; )
;
p r i n t f ( ” \ nEnd = %d\n” , i ) ;
return 0;
}

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 produces the following output.


*****
****
***
**
*

Write a program that produces the following output.


*
* *
* **
* ***
* **
* *
* 42
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!

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy