SlideShare a Scribd company logo
Session 4: Looping
Prepared by: Faculty 4.0 Team
C Programming-Special Training
(Crash Course)
Contents
2
1
3
5
4
2
6
Introduction
While
Do while
for
Nested Loop
Problem
Solving
LOOPING STATEMENTS
3
• Loops in programming come into use when we need to repeatedly
execute a block of statements.
• For example: Suppose we want to print “Hello World” 10 times.
• DEFINITION:A loop is a sequence of instructions that is repeated
until a certain condition is reached.
LOOPING STATEMENTS-TYPES
4
While Loop
5
•A while loop is a control flow statement that allows code
to be executed repeatedly based on a given Boolean
condition.
•The while loop can be thought of as a repeating if
statement.
•While loop has the following syntax:
while ( condition )
{
statements;
}
Trace while Loop
7
int count = 0;
while (count < 2)
{
printf("Welcome to C!");
count++;
}
Initialize count
Trace while Loop, cont.
8
int count = 0;
while (count < 2)
{
printf("Welcome to C!");
count++;
}
(count < 2) is true
Trace while Loop, cont.
9
int count = 0;
while (count < 2)
{
printf("Welcome to C!");
count++;
}
Print Welcome to C
Trace while Loop, cont.
10
int count = 0;
while (count < 2)
{
printf("Welcome to C!");
count++;
}
Increase count by 1
count is 1 now
Trace while Loop, cont.
11
int count = 0;
while (count < 2)
{
printf("Welcome to C!");
count++;
}
(count < 2) is still true since count is 1
Trace while Loop, cont.
12
int count = 0;
while (count < 2)
{
printf("Welcome to C!");
count++;
}
Print Welcome to C
Trace while Loop, cont.
13
int count = 0;
while (count < 2)
{
printf("Welcome to C!");
count++;
}
Increase count by 1
count is 2 now
Trace while Loop, cont.
14
int count = 0;
while (count < 2)
{
printf("Welcome to C!");
count++;
}
(count < 2) is false since count is 2 now
Trace while Loop, cont.
15
int count = 0;
while (count < 2)
{
printf("Welcome to C!");
count++;
}
The loop exits. Execute the next statement after
the loop.
15
#include <stdio.h>
int main() {
int num,sum=0,d;
scanf("%d",&num);
while(num!=0){
d=num%10;
sum+=d;
num=num/10;
}
printf("sum = %d",sum);
return 0;
}
do while Loop
16
• The do..while loop is similar to the while loop with
one important difference. The body
of do...while loop is executed at least once.
• Only then, the test expression is evaluated.
SYNTAX:
do
{
//Statements
}while(condition test);
17
Write a program to check the given number is palindrome
do while - Program
#include <stdio.h>
int main() {
int num;
scanf("%d",&num);
int rev=0,temp,d;
temp=num;
do{
d=temp%10;
rev=(rev*10)+d;
temp/=10;
}while(temp!=0);
if(num==rev)
printf("The given number is palindrome");
return 0;
}
18
19
for statement
for (initialization; condition; updation) {
// Body of the loop
}
Syntax
20
Points to remember:
• The initialization step occurs one time only, before the loop
begins.
• The condition is tested at the beginning of each iteration of the
loop.
• If the condition is true ( non-zero ), then the body of the loop
is executed next.
• If the condition is false ( zero ), then the body is not
executed, and execution continues with the code following
the loop.
•The incrementation happens AFTER the execution of the body,
and only when the body is executed.
21
22
23
24
Nested Loops
• Similar to nested if statements, loops can be nested as well
• That is, the body of a loop can contain another loop
• For each iteration of the outer loop, the inner loop iterates
completely
25
Nested Loops
 How many times will the string "Here" be printed?
26
count1 = 1;
while (count1 <= 10)
{
count2 = 1;
while (count2 <= 20)
{
printf ("Here");
count2++;
}
count1++;
}
10 * 20 = 200
Looping- nested for statements
What are nested for loops?
for(initialization; condition test ; updation)
{
//outer loop statement;
for(initialization; condition test ; updation)
{
//inner loop statements;
}
//outer loop statements;
}
Loop nested within loops
For example:
Looping- nested for statement working
for(int i=1; i<=2;i++)
{
int j;
for( j=1 ;i<=3 ; j++)
{
printf(“%d
%d”,i,j);
}
printf(“n”);
}
OUTER FOR
INNER FOR
How many
statements inside
outer for?
How many
statements inside
inner for?
Looping- nested for statement working
for(int i=1; i<=2;i++)
{
int j;
for( j=1 ;i<=3 ; j++)
{
printf(“%d %d”,i,j);
}
printf(“n”);
}
i=1 2( for true)
j= 1 2 3 (for true)
i j printf
1 1 1 1
1 2 1 2
1 3 1 3
2 1 2 1
2 2 2 2
2 3 2 3
How many
times i and
j are
printed?
Looping- nested for statement working
for(int i=1; i<=2;i++)
{
int j;
for( j=1 ;i<=3 ; j++)
{
printf(“%d %d”,i,j);
}
printf(“n”);
}
How many times outer for
loop got executed ?
How many times inner for
loop got executed ?
6
2
For each execution of the
outer loop the inner loop
is executed 3 times,
hence
2*3=6
Problem
31
1.Problem Statement: Write a program in C to display the triangle using “*” in the
following pattern.
Problem
32
JUMP STATEMENTS
Jump statements?
Transfer control from one point to another point
within the program
break
• The keyword break allows the programmer to terminate the loop.
• The break skips from the loop or block in which it is defined and
the control automatically goes to the first statement after the loop
or block.
• If we use break statement in the innermost loop, then the control
of the program is terminated only from the innermost loop.
• Syntax:
break;
04-Looping( For , while and do while looping) .pdf
continue
• The continue statement is exactly opposite to break.
• The continue statement is used for continuing next iteration of loop
statement.
• When it occurs in the loop, it does not terminate, but it skips the
statements after this statement.
• It is useful when we want to continue the program without
executing any part of the program.
04-Looping( For , while and do while looping) .pdf
38
#include <stdio.h>
int main() {
int i;
for(i=1;i<5;i++){
if(i==3)
break;
printf("%dt",i);
}
return 0;
}
#include <stdio.h>
int main() {
int i;
for(i=1;i<5;i++){
if(i==3)
continue;
printf("%dt",i);
}
return 0;
}
1 2
1 2 4
Looping-Problem Solving
39
Write a program in C to display a pyramid with “*”.
Looping-Problem Solving
40
Looping-Problem Solving
41
Problem Statement: Read the amount of money you have and the prices of the
items you intend to buy. Determine whether you have enough money to buy
everything you selected or whether you are short of money. If you do not have
enough money, indicate the amount of the shortfall. Be sure to include 8% tax
when figuring the amount, you need.
Input: The first line in the data set is an integer that represents the number of data
collections that follow. There are an unknown number of
money amounts in each data set. The value –1 is used to indicate the end of the
collection of prices.
Output: All letters are to be upper case. Include
the amount of shortfall if you do not have enough money. This money amount is
to have a dollar sign ($) in front of the amount and it is to be rounded to 2
decimal places.
Looping-Problem Solving
42
Sample Input:
3
10.50 7.60 1.26 3.49 -1
15.75 6.00 3.98 -1
21.00 5.25 5.75 4.76 3.98 1.50 -1
Sample Output:
$2.84 SHORT
ENOUGH MONEY
$1.94 SHORT
Looping-Problem Solving
43
#include <stdio.h>
int main()
{
int test_case;
scanf("%d", &test_case);
while (test_case--)
{
double total_amount = 0;
scanf("%lf", &total_amount);
double total_expense = 0;
double value;
scanf("%lf", &value);
while (value != -1)
{
total_expense += value;
scanf("%lf", &value);
}
total_expense = total_expense +
(total_expense * 0.08);
if (total_amount>= total_expense)
printf("ENOUGH MONEYn");
else
printf("$%.2lf SHORTn",
total_expense - total_amount);
}
return 0;
}
44
45
46
47
Thank You
48

More Related Content

PPT
12 lec 12 loop
PPTX
COM1407: Program Control Structures – Repetition and Loops
PPTX
industry coding practice unit-2 ppt.pptx
PDF
Workbook_2_Problem_Solving_and_programming.pdf
PDF
Programming fundamental 02
12 lec 12 loop
COM1407: Program Control Structures – Repetition and Loops
industry coding practice unit-2 ppt.pptx
Workbook_2_Problem_Solving_and_programming.pdf
Programming fundamental 02

Similar to 04-Looping( For , while and do while looping) .pdf (20)

PPTX
Control structure of c
DOC
Slide07 repetitions
PPTX
while loop in C.pptx
PPT
Lecture 13 Loops1 with C++ programming.PPT
PDF
SPL 8 | Loop Statements in C
PDF
Learning C programming - from lynxbee.com
PPT
C Sharp Jn (3)
PPTX
06.Loops
PPT
Java căn bản - Chapter6
PPTX
PDF
Programming Fundamentals presentation slide
PPTX
lecture 2.pptx
PPTX
Loops in c
PPT
Loops
PPTX
C decision making and looping.
PPTX
C Programming: Control Structure
PPTX
Loop control structure
Control structure of c
Slide07 repetitions
while loop in C.pptx
Lecture 13 Loops1 with C++ programming.PPT
SPL 8 | Loop Statements in C
Learning C programming - from lynxbee.com
C Sharp Jn (3)
06.Loops
Java căn bản - Chapter6
Programming Fundamentals presentation slide
lecture 2.pptx
Loops in c
Loops
C decision making and looping.
C Programming: Control Structure
Loop control structure
Ad

Recently uploaded (20)

PPTX
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
PPTX
CH1 Production IntroductoryConcepts.pptx
PPTX
Lecture Notes Electrical Wiring System Components
PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
PPT
Project quality management in manufacturing
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
DOCX
573137875-Attendance-Management-System-original
PPTX
Internet of Things (IOT) - A guide to understanding
PPTX
Lesson 3_Tessellation.pptx finite Mathematics
PDF
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
PPTX
UNIT 4 Total Quality Management .pptx
PDF
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
PPTX
Foundation to blockchain - A guide to Blockchain Tech
PDF
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
PDF
composite construction of structures.pdf
DOCX
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
CH1 Production IntroductoryConcepts.pptx
Lecture Notes Electrical Wiring System Components
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
Operating System & Kernel Study Guide-1 - converted.pdf
Project quality management in manufacturing
CYBER-CRIMES AND SECURITY A guide to understanding
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
573137875-Attendance-Management-System-original
Internet of Things (IOT) - A guide to understanding
Lesson 3_Tessellation.pptx finite Mathematics
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
UNIT 4 Total Quality Management .pptx
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
Foundation to blockchain - A guide to Blockchain Tech
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
composite construction of structures.pdf
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
Ad

04-Looping( For , while and do while looping) .pdf

  • 1. Session 4: Looping Prepared by: Faculty 4.0 Team C Programming-Special Training (Crash Course)
  • 3. LOOPING STATEMENTS 3 • Loops in programming come into use when we need to repeatedly execute a block of statements. • For example: Suppose we want to print “Hello World” 10 times. • DEFINITION:A loop is a sequence of instructions that is repeated until a certain condition is reached.
  • 5. While Loop 5 •A while loop is a control flow statement that allows code to be executed repeatedly based on a given Boolean condition. •The while loop can be thought of as a repeating if statement. •While loop has the following syntax: while ( condition ) { statements; }
  • 6. Trace while Loop 7 int count = 0; while (count < 2) { printf("Welcome to C!"); count++; } Initialize count
  • 7. Trace while Loop, cont. 8 int count = 0; while (count < 2) { printf("Welcome to C!"); count++; } (count < 2) is true
  • 8. Trace while Loop, cont. 9 int count = 0; while (count < 2) { printf("Welcome to C!"); count++; } Print Welcome to C
  • 9. Trace while Loop, cont. 10 int count = 0; while (count < 2) { printf("Welcome to C!"); count++; } Increase count by 1 count is 1 now
  • 10. Trace while Loop, cont. 11 int count = 0; while (count < 2) { printf("Welcome to C!"); count++; } (count < 2) is still true since count is 1
  • 11. Trace while Loop, cont. 12 int count = 0; while (count < 2) { printf("Welcome to C!"); count++; } Print Welcome to C
  • 12. Trace while Loop, cont. 13 int count = 0; while (count < 2) { printf("Welcome to C!"); count++; } Increase count by 1 count is 2 now
  • 13. Trace while Loop, cont. 14 int count = 0; while (count < 2) { printf("Welcome to C!"); count++; } (count < 2) is false since count is 2 now
  • 14. Trace while Loop, cont. 15 int count = 0; while (count < 2) { printf("Welcome to C!"); count++; } The loop exits. Execute the next statement after the loop.
  • 15. 15 #include <stdio.h> int main() { int num,sum=0,d; scanf("%d",&num); while(num!=0){ d=num%10; sum+=d; num=num/10; } printf("sum = %d",sum); return 0; }
  • 16. do while Loop 16 • The do..while loop is similar to the while loop with one important difference. The body of do...while loop is executed at least once. • Only then, the test expression is evaluated. SYNTAX: do { //Statements }while(condition test);
  • 17. 17 Write a program to check the given number is palindrome do while - Program #include <stdio.h> int main() { int num; scanf("%d",&num); int rev=0,temp,d; temp=num; do{ d=temp%10; rev=(rev*10)+d; temp/=10; }while(temp!=0); if(num==rev) printf("The given number is palindrome"); return 0; }
  • 18. 18
  • 19. 19 for statement for (initialization; condition; updation) { // Body of the loop } Syntax
  • 20. 20 Points to remember: • The initialization step occurs one time only, before the loop begins. • The condition is tested at the beginning of each iteration of the loop. • If the condition is true ( non-zero ), then the body of the loop is executed next. • If the condition is false ( zero ), then the body is not executed, and execution continues with the code following the loop. •The incrementation happens AFTER the execution of the body, and only when the body is executed.
  • 21. 21
  • 22. 22
  • 23. 23
  • 24. 24
  • 25. Nested Loops • Similar to nested if statements, loops can be nested as well • That is, the body of a loop can contain another loop • For each iteration of the outer loop, the inner loop iterates completely 25
  • 26. Nested Loops  How many times will the string "Here" be printed? 26 count1 = 1; while (count1 <= 10) { count2 = 1; while (count2 <= 20) { printf ("Here"); count2++; } count1++; } 10 * 20 = 200
  • 27. Looping- nested for statements What are nested for loops? for(initialization; condition test ; updation) { //outer loop statement; for(initialization; condition test ; updation) { //inner loop statements; } //outer loop statements; } Loop nested within loops For example:
  • 28. Looping- nested for statement working for(int i=1; i<=2;i++) { int j; for( j=1 ;i<=3 ; j++) { printf(“%d %d”,i,j); } printf(“n”); } OUTER FOR INNER FOR How many statements inside outer for? How many statements inside inner for?
  • 29. Looping- nested for statement working for(int i=1; i<=2;i++) { int j; for( j=1 ;i<=3 ; j++) { printf(“%d %d”,i,j); } printf(“n”); } i=1 2( for true) j= 1 2 3 (for true) i j printf 1 1 1 1 1 2 1 2 1 3 1 3 2 1 2 1 2 2 2 2 2 3 2 3 How many times i and j are printed?
  • 30. Looping- nested for statement working for(int i=1; i<=2;i++) { int j; for( j=1 ;i<=3 ; j++) { printf(“%d %d”,i,j); } printf(“n”); } How many times outer for loop got executed ? How many times inner for loop got executed ? 6 2 For each execution of the outer loop the inner loop is executed 3 times, hence 2*3=6
  • 31. Problem 31 1.Problem Statement: Write a program in C to display the triangle using “*” in the following pattern.
  • 33. JUMP STATEMENTS Jump statements? Transfer control from one point to another point within the program
  • 34. break • The keyword break allows the programmer to terminate the loop. • The break skips from the loop or block in which it is defined and the control automatically goes to the first statement after the loop or block. • If we use break statement in the innermost loop, then the control of the program is terminated only from the innermost loop. • Syntax: break;
  • 36. continue • The continue statement is exactly opposite to break. • The continue statement is used for continuing next iteration of loop statement. • When it occurs in the loop, it does not terminate, but it skips the statements after this statement. • It is useful when we want to continue the program without executing any part of the program.
  • 38. 38 #include <stdio.h> int main() { int i; for(i=1;i<5;i++){ if(i==3) break; printf("%dt",i); } return 0; } #include <stdio.h> int main() { int i; for(i=1;i<5;i++){ if(i==3) continue; printf("%dt",i); } return 0; } 1 2 1 2 4
  • 39. Looping-Problem Solving 39 Write a program in C to display a pyramid with “*”.
  • 41. Looping-Problem Solving 41 Problem Statement: Read the amount of money you have and the prices of the items you intend to buy. Determine whether you have enough money to buy everything you selected or whether you are short of money. If you do not have enough money, indicate the amount of the shortfall. Be sure to include 8% tax when figuring the amount, you need. Input: The first line in the data set is an integer that represents the number of data collections that follow. There are an unknown number of money amounts in each data set. The value –1 is used to indicate the end of the collection of prices. Output: All letters are to be upper case. Include the amount of shortfall if you do not have enough money. This money amount is to have a dollar sign ($) in front of the amount and it is to be rounded to 2 decimal places.
  • 42. Looping-Problem Solving 42 Sample Input: 3 10.50 7.60 1.26 3.49 -1 15.75 6.00 3.98 -1 21.00 5.25 5.75 4.76 3.98 1.50 -1 Sample Output: $2.84 SHORT ENOUGH MONEY $1.94 SHORT
  • 43. Looping-Problem Solving 43 #include <stdio.h> int main() { int test_case; scanf("%d", &test_case); while (test_case--) { double total_amount = 0; scanf("%lf", &total_amount); double total_expense = 0; double value; scanf("%lf", &value); while (value != -1) { total_expense += value; scanf("%lf", &value); } total_expense = total_expense + (total_expense * 0.08); if (total_amount>= total_expense) printf("ENOUGH MONEYn"); else printf("$%.2lf SHORTn", total_expense - total_amount); } return 0; }
  • 44. 44
  • 45. 45
  • 46. 46
  • 47. 47