SlideShare a Scribd company logo
1 of 11
Looping statements
Topics
 for statement
 while statement
 do…while statement
Why looping
 When similar task is performed repeatedly
then we suppose to use looping statement.
 Ex: 1+2+3+…………………+100
 We can directly add two or three numbers
but 100 or more !!!!!!!!
 In this case we can use one addition
operator and then repeat the process.
For statement
 The for statement is the most commonly used
looping statement in C.
 The general form of for statement:
for( initialization; condition; increment/decrement)
statement (s);
 Initialization of control variables is done first, using
assignment statements such as i=1.
 The value of the control variable is tested using the
condition. If the condition is true then the statement(s)
will be executed; otherwise the loop is terminated.
 The value of control variable is incremented or
decremented in the last section.
Continue…
 Ex: for(x=0;x<=9;x=x+1)
printf(“%d”,x);
This for loop is executed 10 times and prints
the digits 0 to 9 in one line.
 The for statement allows for negative
increments.
for(x=9; x>=0; x=x-1)
printf(“%d”,x);
This loop is also executed 10 times but
output would be from 9 to 0.
Continue…
 More than one variable can be initialized at
a time in the for statement.
for (p=1,n=0;n<17;n++)
printf(“%d”,n);
 The increment section may have more than
one part.
for(n=1,m=50; n<=m;n++,m--)
printf(“%d”,m+n);
Continue…
 The condition may have any compound
relation and the testing need not be limited
only to the loop control variable.
for(i=1; i<20 && sum<100; ++i)
{
sum = sum + i;
printf(“%dn”, sum);
}
Continue…
 One or more sections of for loop can be omitted.
m = 5;
for (; m!=100;)
{
printf(“%d ”,m);
m = m + 5;
}
 In for loop there can have no statements.
for(j=1000; j>0;j--)
;
 The above statement can be written as
for(j=1000;j>0;j--);
while statement
 The basic format of the while statement is
while(test-condition)
{
body of the loop
}
 The test-condition is evaluated first and if
the condition is true, then the body of the
loop is executed. This process of repeated
execution of the body continues until the
test-condition finally becomes false.
Continue…
 Ex: 1+2+3+…………………….+100
 Sum=0;
i = 1;
while(i<=100)
{
sum = sum + i;
i++;
}
printf(“Sum = %d”, sum);
Do…while statement
 The general format:
do
{
body of the loop
}while(test-condition);
 The program proceeds to evaluate the body of the
loop first. At the end of the loop, the test-condition
in the while statement is evaluated. If the condition
is true, the program continues to evaluate the body
of the loop once again. This process continues as
long as the condition is true.
Continue…
 Ex: 1+2+3+…………………….+100
 Sum=0;
i = 1;
do
{
sum = sum + i;
i++;
} while(i<=100);
printf(“Sum = %d”, sum);

More Related Content

What's hot

Looping statements in C
Looping statements in CLooping statements in C
Looping statements in C
Jeya Lakshmi
 
Jumping statements
Jumping statementsJumping statements
Jumping statements
Suneel Dogra
 

What's hot (20)

C# loops
C# loopsC# loops
C# loops
 
The Loops
The LoopsThe Loops
The Loops
 
Operators in java
Operators in javaOperators in java
Operators in java
 
Looping statements in C
Looping statements in CLooping statements in C
Looping statements in C
 
C# Strings
C# StringsC# Strings
C# Strings
 
Type casting in java
Type casting in javaType casting in java
Type casting in java
 
Decision making in JAVA
Decision making in JAVADecision making in JAVA
Decision making in JAVA
 
Decision making and branching in c programming
Decision making and branching in c programmingDecision making and branching in c programming
Decision making and branching in c programming
 
While , For , Do-While Loop
While , For , Do-While LoopWhile , For , Do-While Loop
While , For , Do-While Loop
 
Decision Making Statement in C ppt
Decision Making Statement in C pptDecision Making Statement in C ppt
Decision Making Statement in C ppt
 
Control statements in c
Control statements in cControl statements in c
Control statements in c
 
Decision Making and Looping
Decision Making and LoopingDecision Making and Looping
Decision Making and Looping
 
Java Decision Control
Java Decision ControlJava Decision Control
Java Decision Control
 
Jumping statements
Jumping statementsJumping statements
Jumping statements
 
Break and continue
Break and continueBreak and continue
Break and continue
 
Loops in C
Loops in CLoops in C
Loops in C
 
Operators in java
Operators in javaOperators in java
Operators in java
 
Operation on string presentation
Operation on string presentationOperation on string presentation
Operation on string presentation
 
Control structure C++
Control structure C++Control structure C++
Control structure C++
 
Literals,variables,datatype in C#
Literals,variables,datatype in C#Literals,variables,datatype in C#
Literals,variables,datatype in C#
 

Viewers also liked

Object oriented fundamentals_in_java
Object oriented fundamentals_in_javaObject oriented fundamentals_in_java
Object oriented fundamentals_in_java
Self
 

Viewers also liked (9)

Looping statement
Looping statementLooping statement
Looping statement
 
itft-Fundamentals of object–oriented programming in java
itft-Fundamentals of object–oriented programming in javaitft-Fundamentals of object–oriented programming in java
itft-Fundamentals of object–oriented programming in java
 
Object oriented programming
Object oriented programmingObject oriented programming
Object oriented programming
 
Object oriented fundamentals_in_java
Object oriented fundamentals_in_javaObject oriented fundamentals_in_java
Object oriented fundamentals_in_java
 
Unit 5. Control Statement
Unit 5. Control StatementUnit 5. Control Statement
Unit 5. Control Statement
 
10th class computer science notes in english by cstechz
10th class computer science notes in english by cstechz10th class computer science notes in english by cstechz
10th class computer science notes in english by cstechz
 
Hydraulic Ram
Hydraulic RamHydraulic Ram
Hydraulic Ram
 
Uml - An Overview
Uml - An OverviewUml - An Overview
Uml - An Overview
 
Introduction to UML
Introduction to UMLIntroduction to UML
Introduction to UML
 

Similar to Chap 6(decision making-looping)

Similar to Chap 6(decision making-looping) (20)

Lecture 5
Lecture 5Lecture 5
Lecture 5
 
M C6java6
M C6java6M C6java6
M C6java6
 
LOOPS AND DECISIONS
LOOPS AND DECISIONSLOOPS AND DECISIONS
LOOPS AND DECISIONS
 
C Programming - Decision making, Looping
C  Programming - Decision making, LoopingC  Programming - Decision making, Looping
C Programming - Decision making, Looping
 
[ITP - Lecture 11] Loops in C/C++
[ITP - Lecture 11] Loops in C/C++[ITP - Lecture 11] Loops in C/C++
[ITP - Lecture 11] Loops in C/C++
 
175035-cse LAB-04
175035-cse LAB-04 175035-cse LAB-04
175035-cse LAB-04
 
Bsc cs pic u-3 handling input output and control statements
Bsc cs  pic u-3 handling input output and control statementsBsc cs  pic u-3 handling input output and control statements
Bsc cs pic u-3 handling input output and control statements
 
Mca i pic u-3 handling input output and control statements
Mca i pic u-3 handling input output and control statementsMca i pic u-3 handling input output and control statements
Mca i pic u-3 handling input output and control statements
 
CONTROL STMTS.pptx
CONTROL STMTS.pptxCONTROL STMTS.pptx
CONTROL STMTS.pptx
 
Loops and conditional statements
Loops and conditional statementsLoops and conditional statements
Loops and conditional statements
 
ICP - Lecture 9
ICP - Lecture 9ICP - Lecture 9
ICP - Lecture 9
 
Programming Fundamentals lecture 8
Programming Fundamentals lecture 8Programming Fundamentals lecture 8
Programming Fundamentals lecture 8
 
handling input output and control statements
 handling input output and control statements handling input output and control statements
handling input output and control statements
 
Btech i pic u-3 handling input output and control statements
Btech i pic u-3 handling input output and control statementsBtech i pic u-3 handling input output and control statements
Btech i pic u-3 handling input output and control statements
 
Types of loops in c language
Types of loops in c languageTypes of loops in c language
Types of loops in c language
 
3 flow
3 flow3 flow
3 flow
 
3 flow
3 flow3 flow
3 flow
 
cpu.pdf
cpu.pdfcpu.pdf
cpu.pdf
 
Control statements anil
Control statements anilControl statements anil
Control statements anil
 
Diploma ii cfpc u-3 handling input output and control statements
Diploma ii  cfpc u-3 handling input output and control statementsDiploma ii  cfpc u-3 handling input output and control statements
Diploma ii cfpc u-3 handling input output and control statements
 

More from Bangabandhu Sheikh Mujibur Rahman Science and Technology University

More from Bangabandhu Sheikh Mujibur Rahman Science and Technology University (20)

Antenna (2)
Antenna (2)Antenna (2)
Antenna (2)
 
Voltage suppler..
Voltage suppler..Voltage suppler..
Voltage suppler..
 
Number system
Number systemNumber system
Number system
 
Chapter 15
Chapter 15Chapter 15
Chapter 15
 
Chap 13(dynamic memory allocation)
Chap 13(dynamic memory allocation)Chap 13(dynamic memory allocation)
Chap 13(dynamic memory allocation)
 
Chap 12(files)
Chap 12(files)Chap 12(files)
Chap 12(files)
 
Chap 11(pointers)
Chap 11(pointers)Chap 11(pointers)
Chap 11(pointers)
 
Chap 10(structure and unions)
Chap 10(structure and unions)Chap 10(structure and unions)
Chap 10(structure and unions)
 
Chap 9(functions)
Chap 9(functions)Chap 9(functions)
Chap 9(functions)
 
Chap 8(strings)
Chap 8(strings)Chap 8(strings)
Chap 8(strings)
 
Chap 7(array)
Chap 7(array)Chap 7(array)
Chap 7(array)
 
Chap 5(decision making-branching)
Chap 5(decision making-branching)Chap 5(decision making-branching)
Chap 5(decision making-branching)
 
Chap 3(operator expression)
Chap 3(operator expression)Chap 3(operator expression)
Chap 3(operator expression)
 
Chap 2(const var-datatype)
Chap 2(const var-datatype)Chap 2(const var-datatype)
Chap 2(const var-datatype)
 
Computer hardware ppt1
Computer hardware ppt1Computer hardware ppt1
Computer hardware ppt1
 
# Operating system
# Operating system# Operating system
# Operating system
 
Magnetism 3
Magnetism 3Magnetism 3
Magnetism 3
 
Magnetism 2
Magnetism 2Magnetism 2
Magnetism 2
 
2. sinusoidal waves
2. sinusoidal waves2. sinusoidal waves
2. sinusoidal waves
 
Magnetism 1
Magnetism 1Magnetism 1
Magnetism 1
 

Recently uploaded

The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
ZurliaSoop
 

Recently uploaded (20)

HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 

Chap 6(decision making-looping)

  • 1. Looping statements Topics  for statement  while statement  do…while statement
  • 2. Why looping  When similar task is performed repeatedly then we suppose to use looping statement.  Ex: 1+2+3+…………………+100  We can directly add two or three numbers but 100 or more !!!!!!!!  In this case we can use one addition operator and then repeat the process.
  • 3. For statement  The for statement is the most commonly used looping statement in C.  The general form of for statement: for( initialization; condition; increment/decrement) statement (s);  Initialization of control variables is done first, using assignment statements such as i=1.  The value of the control variable is tested using the condition. If the condition is true then the statement(s) will be executed; otherwise the loop is terminated.  The value of control variable is incremented or decremented in the last section.
  • 4. Continue…  Ex: for(x=0;x<=9;x=x+1) printf(“%d”,x); This for loop is executed 10 times and prints the digits 0 to 9 in one line.  The for statement allows for negative increments. for(x=9; x>=0; x=x-1) printf(“%d”,x); This loop is also executed 10 times but output would be from 9 to 0.
  • 5. Continue…  More than one variable can be initialized at a time in the for statement. for (p=1,n=0;n<17;n++) printf(“%d”,n);  The increment section may have more than one part. for(n=1,m=50; n<=m;n++,m--) printf(“%d”,m+n);
  • 6. Continue…  The condition may have any compound relation and the testing need not be limited only to the loop control variable. for(i=1; i<20 && sum<100; ++i) { sum = sum + i; printf(“%dn”, sum); }
  • 7. Continue…  One or more sections of for loop can be omitted. m = 5; for (; m!=100;) { printf(“%d ”,m); m = m + 5; }  In for loop there can have no statements. for(j=1000; j>0;j--) ;  The above statement can be written as for(j=1000;j>0;j--);
  • 8. while statement  The basic format of the while statement is while(test-condition) { body of the loop }  The test-condition is evaluated first and if the condition is true, then the body of the loop is executed. This process of repeated execution of the body continues until the test-condition finally becomes false.
  • 9. Continue…  Ex: 1+2+3+…………………….+100  Sum=0; i = 1; while(i<=100) { sum = sum + i; i++; } printf(“Sum = %d”, sum);
  • 10. Do…while statement  The general format: do { body of the loop }while(test-condition);  The program proceeds to evaluate the body of the loop first. At the end of the loop, the test-condition in the while statement is evaluated. If the condition is true, the program continues to evaluate the body of the loop once again. This process continues as long as the condition is true.
  • 11. Continue…  Ex: 1+2+3+…………………….+100  Sum=0; i = 1; do { sum = sum + i; i++; } while(i<=100); printf(“Sum = %d”, sum);