SlideShare a Scribd company logo
1 of 13
LOOP CONSTRUCTS
CHAPTER # 12
SUBJECT: COMPUTER ICS (PART II)
Presented By :
Prof. HASEEB ASLAM
M.Phil. (IT)
(ASPIRE GROUP OF COLLEGES WAZIRABAD)
1
LOOP
 A statement or set of statements that is
executed repeatedly is known as Loop.
 The structure that repeats a statement(s) is known as
iterative , repetitive or looping construct.
-: Uses :-
1. To execute statement or number of statements for
specified number of times.
2. To use a sequence of values.
-: Types :-
1. While Loop
2. Do-while Loop
3. For Loop
2
ASPIRE
COLLEGE
WAZIRABAD
WHILE LOOP
 Is a simplest loop in a C language.
 This loop executes one or more statements
while given condition remains true.
 It is useful where the number of iterations not known
in advance.
-: Syntax :-
while (condition)
{
statement(s) ;
}
3
1. Condition is given as
relational expression.
2. It controls the iteration
of loop.
3. Statements are
executed only if given
condition is true.
4. Statements are not
executed if given
condition is false.
WORKING OF “WHILE” LOOP
 First of all the condition is checked.
 If it is true
the control enters the body of the loop
and executes all statements in the body.
 After executing the statements
it again moves to the start of the loop and
check the condition again.
 This process continuous as long as the condition
remains true.
 When the condition becomes false
the loop is terminated.
 While loop ends only
when the condition becomes false.
 If the condition remains true loop never ends .
 A loop that has no end point is known as infinite loop. 4
ASPIRE
COLLEGE
WAZIRABAD
FLOW CHART
5
ASPIRE
COLLEGE
WAZIRABAD
EXAMPLE
6
ASPIRE
COLLEGE
WAZIRABAD
DO-WHILE LOOP
 The do-while is an iterative control in C language.
 This loop executes one or more statements
while given condition remains true.
 In this loop condition comes after the body of loop.
 The loop is important in a situation
where statement must be executed at least once.
-: Syntax :-
do
{
statement(s) ;
}
while (condition);
7
FOR LOOP
For loop executes one or more statements for a a
specified number of times this loop is also called
counter controlled loop it is the most flexible loop
this is why the most programmers use this loop in
programs .
Initialization.
It specifies the starting value of counter variable
one are many variables can be initialized in this
part to initialize many variables each variable is
separated by comma. Condition:
8
SENTINEL CONTROL LOOP
# include <stdio.h>
#include <conio.h>
void main( )
{
int n = 1;
clrscr();
while( n!= -1)
{
printf(“Enter a number”);
scanf(“%d”, &n);
printf(“you entered %d  n”, n);
}
printf(“End of program..”);
getch();
}
9
ASPIRE
COLLEGE
WAZIRABAD
CONTINUE STATEMENT
#include <stdio.h>
#include <conio.h>
void main( )
{
int n = 1 , num ;
clrscr();
while( n <= 5)
{
printf(“Enter a number”);
scanf(“%d”, &num);
n++;
if (num<= 0 )
continue ;
printf(“you entered %d  n”, num);
}
printf(“End of program..”);
getch();
}
10
ASPIRE
COLLEGE
WAZIRABAD
CONTINUE STATEMENT
#include <stdio.h>
#include <conio.h>
void main( )
{
int n = 1 , num ;
clrscr();
while( n <= 5)
{
printf(“Enter a number”);
scanf(“%d”, &num);
if (num<= 0 )
continue ;
printf(“you entered %d  n”, num);
n++;
}
printf(“End of program..”);
getch();
}
11
ASPIRE
COLLEGE
WAZIRABAD
BREAK STATEMENT
#include <stdio.h>
#include <conio.h>
void main( )
{
int n = 1 , num ;
clrscr();
while( n <= 5)
{
printf(“Enter a number”);
scanf(“%d”, &num);
n++;
if (num<= 0 )
break ;
printf(“you entered %d  n”, num);
}
printf(“End of program..”);
getch();
}
12
ASPIRE
COLLEGE
WAZIRABAD
GOTO STATEMENT
#include <stdio.h>
#include <conio.h>
void main( )
{
int num ;
clrscr();
positive:
printf(“Enter a number”);
scanf(“%d”, &num);
if(num < 0)
goto positive:
printf(“You Entered %d” , num);
getch();
}
13
ASPIRE
COLLEGE
WAZIRABAD

More Related Content

Similar to 2nd year computer science chapter 12 notes

Do While Repetition Structure
Do While Repetition StructureDo While Repetition Structure
Do While Repetition StructureShahzu2
 
Loops IN COMPUTER SCIENCE STANDARD 11 BY KR
Loops IN COMPUTER SCIENCE STANDARD 11 BY KRLoops IN COMPUTER SCIENCE STANDARD 11 BY KR
Loops IN COMPUTER SCIENCE STANDARD 11 BY KRKrishna Raj
 
Cpp loop types
Cpp loop typesCpp loop types
Cpp loop typesRj Baculo
 
2. Control structures with for while and do while.ppt
2. Control structures with for while and do while.ppt2. Control structures with for while and do while.ppt
2. Control structures with for while and do while.pptManojKhadilkar1
 
Question Pattern in Structure Programming
Question Pattern in Structure ProgrammingQuestion Pattern in Structure Programming
Question Pattern in Structure ProgrammingSuman Mia
 
Loop in C Properties & Applications
Loop in C Properties & ApplicationsLoop in C Properties & Applications
Loop in C Properties & ApplicationsEmroz Sardar
 
Control statements
Control statementsControl statements
Control statementsCutyChhaya
 
web presentation 138.pptx
web presentation 138.pptxweb presentation 138.pptx
web presentation 138.pptxAbhiYadav655132
 
Programming Fundamentals in C++ structures
Programming Fundamentals in  C++ structuresProgramming Fundamentals in  C++ structures
Programming Fundamentals in C++ structuresayshasafdarwaada
 
Bansal presentation (1).pptx
Bansal presentation (1).pptxBansal presentation (1).pptx
Bansal presentation (1).pptxAbhiYadav655132
 
loops and iteration.docx
loops and iteration.docxloops and iteration.docx
loops and iteration.docxJavvajiVenkat
 
Loops Basics
Loops BasicsLoops Basics
Loops BasicsMushiii
 
Loops in c language
Loops in c languageLoops in c language
Loops in c languagetanmaymodi4
 

Similar to 2nd year computer science chapter 12 notes (20)

Do While Repetition Structure
Do While Repetition StructureDo While Repetition Structure
Do While Repetition Structure
 
Loops c++
Loops c++Loops c++
Loops c++
 
Loop structures
Loop structuresLoop structures
Loop structures
 
Loops IN COMPUTER SCIENCE STANDARD 11 BY KR
Loops IN COMPUTER SCIENCE STANDARD 11 BY KRLoops IN COMPUTER SCIENCE STANDARD 11 BY KR
Loops IN COMPUTER SCIENCE STANDARD 11 BY KR
 
Cpp loop types
Cpp loop typesCpp loop types
Cpp loop types
 
Control statements in java
Control statements in javaControl statements in java
Control statements in java
 
2. Control structures with for while and do while.ppt
2. Control structures with for while and do while.ppt2. Control structures with for while and do while.ppt
2. Control structures with for while and do while.ppt
 
Computer programming 2 Lesson 8
Computer programming 2  Lesson 8Computer programming 2  Lesson 8
Computer programming 2 Lesson 8
 
Question Pattern in Structure Programming
Question Pattern in Structure ProgrammingQuestion Pattern in Structure Programming
Question Pattern in Structure Programming
 
Programming in Arduino (Part 2)
Programming in Arduino  (Part 2)Programming in Arduino  (Part 2)
Programming in Arduino (Part 2)
 
Loop in C Properties & Applications
Loop in C Properties & ApplicationsLoop in C Properties & Applications
Loop in C Properties & Applications
 
Control statements
Control statementsControl statements
Control statements
 
web presentation 138.pptx
web presentation 138.pptxweb presentation 138.pptx
web presentation 138.pptx
 
Programming Fundamentals in C++ structures
Programming Fundamentals in  C++ structuresProgramming Fundamentals in  C++ structures
Programming Fundamentals in C++ structures
 
R loops
R   loopsR   loops
R loops
 
Bansal presentation (1).pptx
Bansal presentation (1).pptxBansal presentation (1).pptx
Bansal presentation (1).pptx
 
loops and iteration.docx
loops and iteration.docxloops and iteration.docx
loops and iteration.docx
 
Final requirement
Final requirementFinal requirement
Final requirement
 
Loops Basics
Loops BasicsLoops Basics
Loops Basics
 
Loops in c language
Loops in c languageLoops in c language
Loops in c language
 

More from muhammadFaheem656405

5. Input Output Devices for B com students
5. Input Output Devices for B com students5. Input Output Devices for B com students
5. Input Output Devices for B com studentsmuhammadFaheem656405
 
Characteristics of computer for b com students
Characteristics of computer for b com studentsCharacteristics of computer for b com students
Characteristics of computer for b com studentsmuhammadFaheem656405
 
Ch # 11 2nd year computer science notes1
Ch # 11 2nd year computer science notes1Ch # 11 2nd year computer science notes1
Ch # 11 2nd year computer science notes1muhammadFaheem656405
 
The building block of theory development
The building block of theory developmentThe building block of theory development
The building block of theory developmentmuhammadFaheem656405
 
1. Ch # 02 1st year computer science notes
1. Ch # 02 1st year computer science notes1. Ch # 02 1st year computer science notes
1. Ch # 02 1st year computer science notesmuhammadFaheem656405
 
VLAN chapters for networking CCNA_RSE_Chp6.pptx
VLAN chapters for networking CCNA_RSE_Chp6.pptxVLAN chapters for networking CCNA_RSE_Chp6.pptx
VLAN chapters for networking CCNA_RSE_Chp6.pptxmuhammadFaheem656405
 

More from muhammadFaheem656405 (6)

5. Input Output Devices for B com students
5. Input Output Devices for B com students5. Input Output Devices for B com students
5. Input Output Devices for B com students
 
Characteristics of computer for b com students
Characteristics of computer for b com studentsCharacteristics of computer for b com students
Characteristics of computer for b com students
 
Ch # 11 2nd year computer science notes1
Ch # 11 2nd year computer science notes1Ch # 11 2nd year computer science notes1
Ch # 11 2nd year computer science notes1
 
The building block of theory development
The building block of theory developmentThe building block of theory development
The building block of theory development
 
1. Ch # 02 1st year computer science notes
1. Ch # 02 1st year computer science notes1. Ch # 02 1st year computer science notes
1. Ch # 02 1st year computer science notes
 
VLAN chapters for networking CCNA_RSE_Chp6.pptx
VLAN chapters for networking CCNA_RSE_Chp6.pptxVLAN chapters for networking CCNA_RSE_Chp6.pptx
VLAN chapters for networking CCNA_RSE_Chp6.pptx
 

Recently uploaded

Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfMahmoud M. Sallam
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxEyham Joco
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Celine George
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaVirag Sontakke
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPCeline George
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...JhezDiaz1
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitolTechU
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentInMediaRes1
 

Recently uploaded (20)

ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdf
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptx
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of India
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERP
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptx
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media Component
 

2nd year computer science chapter 12 notes

  • 1. LOOP CONSTRUCTS CHAPTER # 12 SUBJECT: COMPUTER ICS (PART II) Presented By : Prof. HASEEB ASLAM M.Phil. (IT) (ASPIRE GROUP OF COLLEGES WAZIRABAD) 1
  • 2. LOOP  A statement or set of statements that is executed repeatedly is known as Loop.  The structure that repeats a statement(s) is known as iterative , repetitive or looping construct. -: Uses :- 1. To execute statement or number of statements for specified number of times. 2. To use a sequence of values. -: Types :- 1. While Loop 2. Do-while Loop 3. For Loop 2 ASPIRE COLLEGE WAZIRABAD
  • 3. WHILE LOOP  Is a simplest loop in a C language.  This loop executes one or more statements while given condition remains true.  It is useful where the number of iterations not known in advance. -: Syntax :- while (condition) { statement(s) ; } 3 1. Condition is given as relational expression. 2. It controls the iteration of loop. 3. Statements are executed only if given condition is true. 4. Statements are not executed if given condition is false.
  • 4. WORKING OF “WHILE” LOOP  First of all the condition is checked.  If it is true the control enters the body of the loop and executes all statements in the body.  After executing the statements it again moves to the start of the loop and check the condition again.  This process continuous as long as the condition remains true.  When the condition becomes false the loop is terminated.  While loop ends only when the condition becomes false.  If the condition remains true loop never ends .  A loop that has no end point is known as infinite loop. 4 ASPIRE COLLEGE WAZIRABAD
  • 7. DO-WHILE LOOP  The do-while is an iterative control in C language.  This loop executes one or more statements while given condition remains true.  In this loop condition comes after the body of loop.  The loop is important in a situation where statement must be executed at least once. -: Syntax :- do { statement(s) ; } while (condition); 7
  • 8. FOR LOOP For loop executes one or more statements for a a specified number of times this loop is also called counter controlled loop it is the most flexible loop this is why the most programmers use this loop in programs . Initialization. It specifies the starting value of counter variable one are many variables can be initialized in this part to initialize many variables each variable is separated by comma. Condition: 8
  • 9. SENTINEL CONTROL LOOP # include <stdio.h> #include <conio.h> void main( ) { int n = 1; clrscr(); while( n!= -1) { printf(“Enter a number”); scanf(“%d”, &n); printf(“you entered %d n”, n); } printf(“End of program..”); getch(); } 9 ASPIRE COLLEGE WAZIRABAD
  • 10. CONTINUE STATEMENT #include <stdio.h> #include <conio.h> void main( ) { int n = 1 , num ; clrscr(); while( n <= 5) { printf(“Enter a number”); scanf(“%d”, &num); n++; if (num<= 0 ) continue ; printf(“you entered %d n”, num); } printf(“End of program..”); getch(); } 10 ASPIRE COLLEGE WAZIRABAD
  • 11. CONTINUE STATEMENT #include <stdio.h> #include <conio.h> void main( ) { int n = 1 , num ; clrscr(); while( n <= 5) { printf(“Enter a number”); scanf(“%d”, &num); if (num<= 0 ) continue ; printf(“you entered %d n”, num); n++; } printf(“End of program..”); getch(); } 11 ASPIRE COLLEGE WAZIRABAD
  • 12. BREAK STATEMENT #include <stdio.h> #include <conio.h> void main( ) { int n = 1 , num ; clrscr(); while( n <= 5) { printf(“Enter a number”); scanf(“%d”, &num); n++; if (num<= 0 ) break ; printf(“you entered %d n”, num); } printf(“End of program..”); getch(); } 12 ASPIRE COLLEGE WAZIRABAD
  • 13. GOTO STATEMENT #include <stdio.h> #include <conio.h> void main( ) { int num ; clrscr(); positive: printf(“Enter a number”); scanf(“%d”, &num); if(num < 0) goto positive: printf(“You Entered %d” , num); getch(); } 13 ASPIRE COLLEGE WAZIRABAD