Programming 
Fundamentals
Loops & Its Logic 
 What is a Loop? 
 An iterative statement is called loop. A type of control structure that 
repeats a statement or set of statements is known as looping 
structures. It is also known as iterative or repetitive structure. There 
are two elements of the loop: the body of loop which is to be executed 
number of times and a loop condition which terminates the loop when a 
particular condition is met. 
Repetition 
A single statement 
A set of statements 
12/15/14 2
Types of Loop 
 Types of Loop 
 Counter loop (for loop) 
 Conditional loops (while, do while) 
Types of Loop 
For Loop While Loop Do While Loop 
12/15/14 3
For Loop Operation 
Initialization Expression 
False 
Test Exit 
Expression 
True 
Body of Loop 
Increment Expression 
12/15/14 4
For Loop Structure 
Test Expression 
Initialization Expression Increment Expression 
for ( j=0 ; j < 5 ; j++ ) 
statement; 
Single Statement Loop Body 
keyword 
12/15/14 5
For Loop Structure 
Initialization Expression 
Test Expression 
for ( j=0 ; j < 5 ; j++ ) 
{ statement; 
statement; 
statement; 
} 
Increment Expression 
Multiple Statement Loop Body 
keyword 
12/15/14 6
For Loop 
// demonstrate simple for loop 
# include <iostream> 
#include<conio.h> 
void main() 
{ 
int j; 
for (j=0 ; j < 5 ; j++) 
cout<< j * j; 
getch(); 
} 
Output: 0 1 4 9 16 
12/15/14 7
For Loop Variations 
 Multiple initialization and increment Expressions 
for (x=0 , j=0 ; j < 5 ; j++ , x++) 
controlling 
 Increment or Decrement 
for ( j=5 ; j > 0 ; j-- ) 
for ( j=0 ; j <10 ; j+=2 ) 
12/15/14 8
The Infinite loop 
 A loop that does not terminate is called 
Infinite loop. 
A loop who's condition always remain true is 
called infinite loop. 
e.g. 
for(int i=0;i>0;i++) 
Cout<<i; 
12/15/14 9
While Loop 
Single Statement Loop Body 
while (test expression ) 
statement; 
Multiple Statement Loop Body 
while ( test expression ) 
{ 
Statement; 
Statement; 
Statement; 
} 
False 
Test Exit 
Expression 
True 
Body of Loop 
While Operation 
12/15/14 10
The body of the loop is executed until the condition becomes false. Here is a 
program which illustrates the working of the while loop. 
void main () 
{ 
int c=0; 
int i=0; 
char str=‘y’; 
while(str==’y’) 
{ 
cout<<”Enter the number you want to add”<<endl; 
Cin>>i; 
c=c+1; 
cout<<”Do you want to enter another number y/n:”<<endl; 
Cin>>str; 
} 
Cout<<“The sum of the numbers are : <<c; 
getch(); 
} 
12/15/14 11
 // program that inputs a number from the user and displays a 
table of that number using while loop. 
 void main() 
 { 
 int n,c; 
 C=1; 
 Cout<<“Enter a number”; 
 Cin>>n; 
 while(c<=10) 
 { 
 Cout<<n<<“ * ”<<c” = ”<<n*c<<endl; 
 C=c+1; 
 } 
 Getch(); 
 } 
12/15/14 12
Do While Loop 
Do 
Single Statement Loop Body 
while (test expression ); Body of Loop 
Do 
statement; 
MMuullttiippllee Multiple SSttaatteemmeenntt Statement LLoooopp Loop BBooddyy 
Body 
{ 
Statement; 
Statement; 
Statement; 
} 
while ( test expression ); 
False 
Test Exit 
Expression 
True 
Do While Operation 
12/15/14 13
The body of the loop is executed until the condition becomes false. Here is a 
program which illustrates the working of the do while loop. 
void main () 
{ 
int c=0; 
int i=0; 
char str=‘y’; 
do 
{ 
cout<<”Enter the number you want to add”<<endl; 
Cin>>i; 
c=c+1; 
cout<<”Do you want to enter another number y/n:”<<endl; 
Cin>>str; 
} while(str==’y’); 
Cout<<”The sum of the numbers are : <<c; 
getch(); 
} 
12/15/14 14
//Program that gets starting and ending point from the user and 
displays all odd numbers in the given range using do-while loop 
void main() 
{ 
int c,s,e; 
Cout<<“Enter Starting Number: ”; 
Cin>>s; 
Cout<<“Enter Ending Number: ”; 
Cin>>e; 
C=s; 
do 
{ 
If(c%2!=0) 
Cout<<c<<endl; 
C=c+1; 
} while(c<=e); 
} 
12/15/14 15
When to use Which Loop? 
 Advance knowledge 
 For Loop 
 No Prior Knowledge 
 While Loop 
 Do While (executes at least once) 
12/15/14 16
Break & Continue Statement 
 To exit a loop you can use the 
break statement at any time. 
This can be very useful if you 
want to stop running a loop 
because a condition has been 
met other than the loop end 
condition. 
 With “continue;” it is possible 
to skip the rest of the 
commands in the current loop 
and start from the top again. 
(the loop variable must still be 
incremented). 
EXAMPLE 
int main() 
{ 
int j, total; 
for (int i=0; i<20;i++) 
{ 
cin>>j; 
if ( j == 10) 
continue; 
total = i + j; 
cout<<“total is=“<<total; 
} 
return 0; 
} 
12/15/14 17
Continue statement 
 With “continue;” it is possible to skip the rest of the commands in 
the current loop and start from the top again. (the loop variable 
must still be incremented). 
EXAMPLE 
int main() 
{ 
int i; 
i = 0; 
while ( i < 20 ) 
{ 
i++; 
continue; 
printf(“hey wait! you are skipping men”); 
} 
return 0; 
} 
12/15/14 18
Nested Loop 
 A loop within an other loop 
is called nested loop. e.g. 
 for(int i=1;i<=5;i++) 
 { 
 for(int j=1;j<=i;j++) 
 { 
 Cout<<j; 
 } 
 Cout<<“n”; 
} 
Output 
1 12 
123 
1234 
12345 
12/15/14 19
Nested Loop 
 for(int i=5;i>=1;i--) 
 { 
 for(int j=i;j>=1;j--) 
 { 
 Cout<<j; 
 } 
 Cout<<“n”; 
} 
Output 
54321 
4321 
321 
21 
1 
12/15/14 20
Nested Loop 
 for(int i=5;i>=1;i--) 
 { 
 for(int j=1;j<=i;j++) 
 { 
 Cout<<j; 
 } 
 Cout<<“n”; 
} 
Output 
12345 
1234 
123 
12 
1 
12/15/14 21
Nested Loop 
 for(int i=5;i>=1;i--) 
 { 
 for(int j=1;j<=i;j++) 
 { 
 Cout<<“*”; 
 } 
 Cout<<“n”; 
} 
Output 
***** 
**** 
*** 
** 
* 
12/15/14 22
Nested Loop 
 int i=5; 
 while(i>=1) 
 { 
 int j=1; 
 while(j<=i) 
 { 
 Cout<<j; 
 j++; 
 } 
 i--; 
 Cout<<“n”; 
} 
Output 
12345 
1234 
123 
12 
1 
12/15/14 23
What makes a bad program? 
 Repeating trial and error without 
understanding the problem 
 Writing Code without detailed 
analysis and design (Abstraction, 
Algo) 
 Writing tricky and dirty programs
Programms: 
 Write a program using for, while and do while loops to: 
 Display numbers from 0 to 100 
 Display numbers from 100 to 0 
 Display even numbers from 0 to 100 
 Display odd numbers from 0 to 100 
 Display even numbers from 100 to 0 
 Display odd numbers from 100 to 0 
 Display Square of numbers from 0 to 100 
 Display Cube of numbers from 0 to 100 
 Display Square of numbers from 100 to 0 
 Display Cube of numbers from 100 to 0 
 Display Square of numbers from 40 to 100 
 Display Cube of numbers from 50 to 100 
 Display Square of numbers from 500 to 1000 
 Display Cube of numbers from 1000 to 1500 
 Display Average of even numbers from 1000 to 1200 
 Display Average of odd numbers from 1200 to 1000 
12/15/14 25
Programs: 
 Write a program using for, while and do while and nested loops to display the 
following outputs. You are also required to submit the dry runs of all these 
programs on paper. 
12345678910 
123456789 
12345678 
1234567 
123456 
12345 
1234 
123 
12 
1 
=========== 
**************** 
******** 
**** 
** 
* 
=========== 
* *** 
***** 
******* 
******* 
***** 
*** 
* 
1 12 
123 
1234 
12345 
123456 
1234567 
12345678 
123456789 
12345678910 
12/15/14 26
Programs: 
 Write a program using for, while and do while and nested loops to display the 
following outputs. You are also required to submit the dry runs of all these 
programs on paper. 
13579 
13579 
1357 
1357 
135 
135 
13 
1 
* 
*** 
***** 
******* 
***** 
*** 
* 
& && 
&&& 
&&&& 
&&&&& 
&&&&& 
&&&& 
&&& 
&& 
& 
0 02 
024 
0246 
02468 
0246810 
024681012 
02468101214 
12/15/14 27

More Related Content

PPTX
Loops c++
PPT
Database schema
PPT
Overview of TCP IP
PPT
Control structure C++
PDF
OOAD - UML - Sequence and Communication Diagrams - Lab
PPT
Control structures repetition
PPTX
Looping statements in C
PPTX
Control and conditional statements
Loops c++
Database schema
Overview of TCP IP
Control structure C++
OOAD - UML - Sequence and Communication Diagrams - Lab
Control structures repetition
Looping statements in C
Control and conditional statements

What's hot (20)

PPTX
Jsp lifecycle
PPTX
Control structure of c
PPTX
Loops in c programming
PDF
Storage organization and stack allocation of space
ODP
OOP java
PPT
Services provided by os
PPTX
15 puzzle problem using branch and bound
PPTX
Conditional statements
PPTX
Simple if else statement,nesting of if else statement &amp; else if ladder
ODP
CProgrammingTutorial
PPTX
Iteration Statement in C++
PPTX
TCP/IP and UDP protocols
PDF
Traffic types in internet
PPTX
HDLC(high level data link control)
PPT
Super and final in java
PPTX
C programming enumeration
PPT
Object Oriented Programming Concepts
PPT
Functions in C++
Jsp lifecycle
Control structure of c
Loops in c programming
Storage organization and stack allocation of space
OOP java
Services provided by os
15 puzzle problem using branch and bound
Conditional statements
Simple if else statement,nesting of if else statement &amp; else if ladder
CProgrammingTutorial
Iteration Statement in C++
TCP/IP and UDP protocols
Traffic types in internet
HDLC(high level data link control)
Super and final in java
C programming enumeration
Object Oriented Programming Concepts
Functions in C++
Ad

Viewers also liked (15)

PPSX
C lecture 4 nested loops and jumping statements slideshare
PDF
Az ve Öz C++ Muhammet ÇAĞATAY
PPTX
The Loops
PPTX
PPTX
Loops in C
PPTX
Loops in C Programming
PPTX
control statements of clangauge (ii unit)
PPT
Chapter 1 Nested Control Structures
PDF
Nesting of for loops using C++
PPTX
C++ loop
PPSX
INTRODUCTION TO C PROGRAMMING
PPTX
1G vs 2G vs 3G vs 4G vs 5G
C lecture 4 nested loops and jumping statements slideshare
Az ve Öz C++ Muhammet ÇAĞATAY
The Loops
Loops in C
Loops in C Programming
control statements of clangauge (ii unit)
Chapter 1 Nested Control Structures
Nesting of for loops using C++
C++ loop
INTRODUCTION TO C PROGRAMMING
1G vs 2G vs 3G vs 4G vs 5G
Ad

Similar to Loops (20)

PPTX
Cs1123 6 loops
PPT
C++ control loops
PPT
Iteration
PPT
FP 201 - Unit 3 Part 2
PDF
Repetition, Basic loop structures, Loop programming techniques
PPTX
Loops IN COMPUTER SCIENCE STANDARD 11 BY KR
PPTX
Iterative control structures, looping, types of loops, loop working
PPT
Chapter 5 Looping
PPTX
Lec7 - Loops updated.pptx
PDF
04-Looping( For , while and do while looping) .pdf
DOCX
C++ Loops General Discussion of Loops A loop is a.docx
PDF
Cse115 lecture08repetitionstructures part02
PPT
PPSX
C lecture 3 control statements slideshare
PDF
Chapter 3 - Flow of Control Part II.pdf
PPT
C++ CH3-P2 using c++ in all other parts.ppt
PPT
04a intro while
PPTX
operating system introduction to programming
PPTX
Introduction to Loops using C++. Exploring While&Do-While Loop
Cs1123 6 loops
C++ control loops
Iteration
FP 201 - Unit 3 Part 2
Repetition, Basic loop structures, Loop programming techniques
Loops IN COMPUTER SCIENCE STANDARD 11 BY KR
Iterative control structures, looping, types of loops, loop working
Chapter 5 Looping
Lec7 - Loops updated.pptx
04-Looping( For , while and do while looping) .pdf
C++ Loops General Discussion of Loops A loop is a.docx
Cse115 lecture08repetitionstructures part02
C lecture 3 control statements slideshare
Chapter 3 - Flow of Control Part II.pdf
C++ CH3-P2 using c++ in all other parts.ppt
04a intro while
operating system introduction to programming
Introduction to Loops using C++. Exploring While&Do-While Loop

Recently uploaded (20)

PPTX
Comprehensive Guide to Digital Image Processing Concepts and Applications
PPT
3.Software Design for software engineering
PDF
Top 10 Project Management Software for Small Teams in 2025.pdf
PPTX
Chapter 1 - Transaction Processing and Mgt.pptx
PPTX
UNIT II: Software design, software .pptx
PDF
Crypto Loss And Recovery Guide By Expert Recovery Agency.
PPTX
ROI from Efficient Content & Campaign Management in the Digital Media Industry
PDF
Sanket Mhaiskar Resume - Senior Software Engineer (Backend, AI)
PPTX
Bandicam Screen Recorder 8.2.1 Build 2529 Crack
PDF
Understanding the Need for Systemic Change in Open Source Through Intersectio...
PDF
Building an Inclusive Web Accessibility Made Simple with Accessibility Analyzer
PDF
infoteam HELLAS company profile 2025 presentation
PPTX
StacksandQueuesCLASS 12 COMPUTER SCIENCE.pptx
PDF
SOFTWARE ENGINEERING Software Engineering (3rd Edition) by K.K. Aggarwal & Yo...
PPTX
HackYourBrain__UtrechtJUG__11092025.pptx
PDF
Mobile App Backend Development with WordPress REST API: The Complete eBook
PDF
Engineering Document Management System (EDMS)
PDF
Odoo Construction Management System by CandidRoot
PPTX
Why 2025 Is the Best Year to Hire Software Developers in India
PPTX
SAP Business AI_L1 Overview_EXTERNAL.pptx
Comprehensive Guide to Digital Image Processing Concepts and Applications
3.Software Design for software engineering
Top 10 Project Management Software for Small Teams in 2025.pdf
Chapter 1 - Transaction Processing and Mgt.pptx
UNIT II: Software design, software .pptx
Crypto Loss And Recovery Guide By Expert Recovery Agency.
ROI from Efficient Content & Campaign Management in the Digital Media Industry
Sanket Mhaiskar Resume - Senior Software Engineer (Backend, AI)
Bandicam Screen Recorder 8.2.1 Build 2529 Crack
Understanding the Need for Systemic Change in Open Source Through Intersectio...
Building an Inclusive Web Accessibility Made Simple with Accessibility Analyzer
infoteam HELLAS company profile 2025 presentation
StacksandQueuesCLASS 12 COMPUTER SCIENCE.pptx
SOFTWARE ENGINEERING Software Engineering (3rd Edition) by K.K. Aggarwal & Yo...
HackYourBrain__UtrechtJUG__11092025.pptx
Mobile App Backend Development with WordPress REST API: The Complete eBook
Engineering Document Management System (EDMS)
Odoo Construction Management System by CandidRoot
Why 2025 Is the Best Year to Hire Software Developers in India
SAP Business AI_L1 Overview_EXTERNAL.pptx

Loops

  • 2. Loops & Its Logic  What is a Loop?  An iterative statement is called loop. A type of control structure that repeats a statement or set of statements is known as looping structures. It is also known as iterative or repetitive structure. There are two elements of the loop: the body of loop which is to be executed number of times and a loop condition which terminates the loop when a particular condition is met. Repetition A single statement A set of statements 12/15/14 2
  • 3. Types of Loop  Types of Loop  Counter loop (for loop)  Conditional loops (while, do while) Types of Loop For Loop While Loop Do While Loop 12/15/14 3
  • 4. For Loop Operation Initialization Expression False Test Exit Expression True Body of Loop Increment Expression 12/15/14 4
  • 5. For Loop Structure Test Expression Initialization Expression Increment Expression for ( j=0 ; j < 5 ; j++ ) statement; Single Statement Loop Body keyword 12/15/14 5
  • 6. For Loop Structure Initialization Expression Test Expression for ( j=0 ; j < 5 ; j++ ) { statement; statement; statement; } Increment Expression Multiple Statement Loop Body keyword 12/15/14 6
  • 7. For Loop // demonstrate simple for loop # include <iostream> #include<conio.h> void main() { int j; for (j=0 ; j < 5 ; j++) cout<< j * j; getch(); } Output: 0 1 4 9 16 12/15/14 7
  • 8. For Loop Variations  Multiple initialization and increment Expressions for (x=0 , j=0 ; j < 5 ; j++ , x++) controlling  Increment or Decrement for ( j=5 ; j > 0 ; j-- ) for ( j=0 ; j <10 ; j+=2 ) 12/15/14 8
  • 9. The Infinite loop  A loop that does not terminate is called Infinite loop. A loop who's condition always remain true is called infinite loop. e.g. for(int i=0;i>0;i++) Cout<<i; 12/15/14 9
  • 10. While Loop Single Statement Loop Body while (test expression ) statement; Multiple Statement Loop Body while ( test expression ) { Statement; Statement; Statement; } False Test Exit Expression True Body of Loop While Operation 12/15/14 10
  • 11. The body of the loop is executed until the condition becomes false. Here is a program which illustrates the working of the while loop. void main () { int c=0; int i=0; char str=‘y’; while(str==’y’) { cout<<”Enter the number you want to add”<<endl; Cin>>i; c=c+1; cout<<”Do you want to enter another number y/n:”<<endl; Cin>>str; } Cout<<“The sum of the numbers are : <<c; getch(); } 12/15/14 11
  • 12.  // program that inputs a number from the user and displays a table of that number using while loop.  void main()  {  int n,c;  C=1;  Cout<<“Enter a number”;  Cin>>n;  while(c<=10)  {  Cout<<n<<“ * ”<<c” = ”<<n*c<<endl;  C=c+1;  }  Getch();  } 12/15/14 12
  • 13. Do While Loop Do Single Statement Loop Body while (test expression ); Body of Loop Do statement; MMuullttiippllee Multiple SSttaatteemmeenntt Statement LLoooopp Loop BBooddyy Body { Statement; Statement; Statement; } while ( test expression ); False Test Exit Expression True Do While Operation 12/15/14 13
  • 14. The body of the loop is executed until the condition becomes false. Here is a program which illustrates the working of the do while loop. void main () { int c=0; int i=0; char str=‘y’; do { cout<<”Enter the number you want to add”<<endl; Cin>>i; c=c+1; cout<<”Do you want to enter another number y/n:”<<endl; Cin>>str; } while(str==’y’); Cout<<”The sum of the numbers are : <<c; getch(); } 12/15/14 14
  • 15. //Program that gets starting and ending point from the user and displays all odd numbers in the given range using do-while loop void main() { int c,s,e; Cout<<“Enter Starting Number: ”; Cin>>s; Cout<<“Enter Ending Number: ”; Cin>>e; C=s; do { If(c%2!=0) Cout<<c<<endl; C=c+1; } while(c<=e); } 12/15/14 15
  • 16. When to use Which Loop?  Advance knowledge  For Loop  No Prior Knowledge  While Loop  Do While (executes at least once) 12/15/14 16
  • 17. Break & Continue Statement  To exit a loop you can use the break statement at any time. This can be very useful if you want to stop running a loop because a condition has been met other than the loop end condition.  With “continue;” it is possible to skip the rest of the commands in the current loop and start from the top again. (the loop variable must still be incremented). EXAMPLE int main() { int j, total; for (int i=0; i<20;i++) { cin>>j; if ( j == 10) continue; total = i + j; cout<<“total is=“<<total; } return 0; } 12/15/14 17
  • 18. Continue statement  With “continue;” it is possible to skip the rest of the commands in the current loop and start from the top again. (the loop variable must still be incremented). EXAMPLE int main() { int i; i = 0; while ( i < 20 ) { i++; continue; printf(“hey wait! you are skipping men”); } return 0; } 12/15/14 18
  • 19. Nested Loop  A loop within an other loop is called nested loop. e.g.  for(int i=1;i<=5;i++)  {  for(int j=1;j<=i;j++)  {  Cout<<j;  }  Cout<<“n”; } Output 1 12 123 1234 12345 12/15/14 19
  • 20. Nested Loop  for(int i=5;i>=1;i--)  {  for(int j=i;j>=1;j--)  {  Cout<<j;  }  Cout<<“n”; } Output 54321 4321 321 21 1 12/15/14 20
  • 21. Nested Loop  for(int i=5;i>=1;i--)  {  for(int j=1;j<=i;j++)  {  Cout<<j;  }  Cout<<“n”; } Output 12345 1234 123 12 1 12/15/14 21
  • 22. Nested Loop  for(int i=5;i>=1;i--)  {  for(int j=1;j<=i;j++)  {  Cout<<“*”;  }  Cout<<“n”; } Output ***** **** *** ** * 12/15/14 22
  • 23. Nested Loop  int i=5;  while(i>=1)  {  int j=1;  while(j<=i)  {  Cout<<j;  j++;  }  i--;  Cout<<“n”; } Output 12345 1234 123 12 1 12/15/14 23
  • 24. What makes a bad program?  Repeating trial and error without understanding the problem  Writing Code without detailed analysis and design (Abstraction, Algo)  Writing tricky and dirty programs
  • 25. Programms:  Write a program using for, while and do while loops to:  Display numbers from 0 to 100  Display numbers from 100 to 0  Display even numbers from 0 to 100  Display odd numbers from 0 to 100  Display even numbers from 100 to 0  Display odd numbers from 100 to 0  Display Square of numbers from 0 to 100  Display Cube of numbers from 0 to 100  Display Square of numbers from 100 to 0  Display Cube of numbers from 100 to 0  Display Square of numbers from 40 to 100  Display Cube of numbers from 50 to 100  Display Square of numbers from 500 to 1000  Display Cube of numbers from 1000 to 1500  Display Average of even numbers from 1000 to 1200  Display Average of odd numbers from 1200 to 1000 12/15/14 25
  • 26. Programs:  Write a program using for, while and do while and nested loops to display the following outputs. You are also required to submit the dry runs of all these programs on paper. 12345678910 123456789 12345678 1234567 123456 12345 1234 123 12 1 =========== **************** ******** **** ** * =========== * *** ***** ******* ******* ***** *** * 1 12 123 1234 12345 123456 1234567 12345678 123456789 12345678910 12/15/14 26
  • 27. Programs:  Write a program using for, while and do while and nested loops to display the following outputs. You are also required to submit the dry runs of all these programs on paper. 13579 13579 1357 1357 135 135 13 1 * *** ***** ******* ***** *** * & && &&& &&&& &&&&& &&&&& &&&& &&& && & 0 02 024 0246 02468 0246810 024681012 02468101214 12/15/14 27