SlideShare ist ein Scribd-Unternehmen logo
1 von 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);

Weitere ähnliche Inhalte

Was ist angesagt?

Was ist angesagt? (20)

Data Types and Variables In C Programming
Data Types and Variables In C ProgrammingData Types and Variables In C Programming
Data Types and Variables In C Programming
 
Constructor
ConstructorConstructor
Constructor
 
While , For , Do-While Loop
While , For , Do-While LoopWhile , For , Do-While Loop
While , For , Do-While Loop
 
Function C programming
Function C programmingFunction C programming
Function C programming
 
Loops c++
Loops c++Loops c++
Loops c++
 
PHP - DataType,Variable,Constant,Operators,Array,Include and require
PHP - DataType,Variable,Constant,Operators,Array,Include and requirePHP - DataType,Variable,Constant,Operators,Array,Include and require
PHP - DataType,Variable,Constant,Operators,Array,Include and require
 
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
 
Control statements in java
Control statements in javaControl statements in java
Control statements in java
 
Loop(for, while, do while) condition Presentation
Loop(for, while, do while) condition PresentationLoop(for, while, do while) condition Presentation
Loop(for, while, do while) condition Presentation
 
Command line-arguments-in-java-tutorial
Command line-arguments-in-java-tutorialCommand line-arguments-in-java-tutorial
Command line-arguments-in-java-tutorial
 
Control structure in c
Control structure in cControl structure in c
Control structure in c
 
Function overloading
Function overloadingFunction overloading
Function overloading
 
JavaScript - Chapter 11 - Events
 JavaScript - Chapter 11 - Events  JavaScript - Chapter 11 - Events
JavaScript - Chapter 11 - Events
 
Function & Recursion
Function & RecursionFunction & Recursion
Function & Recursion
 
Operators and expressions in C++
Operators and expressions in C++Operators and expressions in C++
Operators and expressions in C++
 
Dynamic memory allocation
Dynamic memory allocationDynamic memory allocation
Dynamic memory allocation
 
Conditional statement c++
Conditional statement c++Conditional statement c++
Conditional statement c++
 
Event In JavaScript
Event In JavaScriptEvent In JavaScript
Event In JavaScript
 
C Programming: Structure and Union
C Programming: Structure and UnionC Programming: Structure and Union
C Programming: Structure and Union
 
Introduction to c++ ppt 1
Introduction to c++ ppt 1Introduction to c++ ppt 1
Introduction to c++ ppt 1
 

Andere mochten auch

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

Andere mochten auch (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
 

Ähnlich wie Chap 6(decision making-looping)

Ähnlich wie 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
 

Mehr von Bangabandhu Sheikh Mujibur Rahman Science and Technology University

Mehr von 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
 

Kürzlich hochgeladen

Kürzlich hochgeladen (20)

Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
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
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
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)
 
Basic Intentional Injuries Health Education
Basic Intentional Injuries Health EducationBasic Intentional Injuries Health Education
Basic Intentional Injuries Health Education
 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & Systems
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
 
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
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
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Ữ Â...
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxExploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 

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