SlideShare a Scribd company logo
Introducing to Loops
Goals
By the end of this unit, you should
  understand …
• … basic loop concepts, including pretest &
  posttest loops, loop initialization ,condition
  and increment.
• … how to program a while loop.
• … how to program a do-while loop.
• … how to program a for loops.
What is a loop?
• A loop is a programming structure that
  allows an action to repeat until the
  program meets a given condition.
• After each iteration of a loop, the loop
  checks against a loop control expression
  to see if the program met the given
  condition. If it did, the loop stops. If not,
  the loop moves on to the next iteration.
Types of Loops
• C supports two categories of loops,
  based on where the program tests the
  condition:
  – Pretest Loops
  – Post-test Loops
Pretest Loops
• With each iteration, the program tests the
  condition first before executing the loop’s
  block.
• If the condition results to true, the loop
  continues and executes the block; if the
  condition results to false, the loop
  terminates.
• With a pretest loop, there is a chance the
  loop may never execute once in the program.
Post-Test Loops
• With each iteration, the program
  executes the loop’s block first and tests
  against a condition.
• If the condition tests to true, the loop
  continues and executes another
  iteration; if the condition tests to false,
  the loop terminates.
• With a post-test loop, the loop will
  always execute at least once!
Pretest vs. Post-Test Loops




   from Figure 6-2 in Forouzan & Gilberg, p. 305
Variable Initialization
• int i=5;
• I have initialized the variable i with a value i=5;
Updating a Loop
• A loop update is what happens inside a
  loop’s block that eventually causes the
  loop to satisfy the condition, thus ending
  the loop.
• Updating happens during each loop
  iteration.
• Without a loop update, the loop would be
  an infinite loop.
Initialization & Updating
Loop Comparison




from Table 6-1 in Forouzan & Gilberg, p. 308
C Implementation of Loops
• Pretest Loops
  – while Loop
  – for Loop
• Post-test Loop
  – do … while loop
• All loops in C execute so long as a
  condition evaluates to true and terminate
  when the condition evaluates to false.
The while Loop




from Figure 6-11 in Forouzan & Gilberg, p. 311
Example of while loop
#include<stdio.h>
#include<conio.h>
                         If we input value
int main()
                         of end as 10 then
{                        output will be 1 to
int start, end;          10
scanf("%d",&end);
start = 0;
 while ( start < end)
{
start++;
printf("%dn", start);
 }
return 0;
Example
#include<stdio.h>             The output of the postfix and
                              prefix increment example will
int main()                    look like this:
 { int i;                      1
                               2
i = 0;                         3
                               4
while(i++ < 5)
                               5
{ printf("%dn", i);
}                              1
printf("n");                  2
                               3
 i = 0;                        4
while(++i < 5)
 { printf("%dn", i); }
return 0; }
The for Loop
• A for loop is a pretest loop that includes three
  expressions in its header:
   –   Loop initialization statement
   –   Limit test expression(condition)
   –   Loop update statement


• The for loop is often used as a start-controlled
  loop since we can accurately predict the
  maximum number of iterations.
The for Loop
Comparing while with for
Comparing while with for
i = 1;               sum = 0;
sum =0;              for (i = 1; i <= 20; i++)
while (i <= 20)      {
{                      scanf(“%d”, &a);
  scanf(“%d”, &a);     sum = sum+a;
  sum = sum+a;       }//end for
  i++
}//end while
 The while Loop            The for Loop
Example of for loop
main ( )
{
int p, n, count ;
float r, si ;
for ( count = 1 ; count <= 3 ; count = count + 1 )
{
printf ( "Enter values of p, n, and r " ) ;
scanf ( "%d %d %f", &p, &n, &r ) ;
si = (p * n * r) / 100 ;
printf ( "Simple Interest = Rs.%fn", si ) ;
}
}
Example
#include <stdio.h>
 #include<conio.h>
void main()
{
   int i = 0, j = 8;
   printf("Times 8 Tablen");
   for(i = 0; i <= 12; i = i + 1)
   {
    printf("%d x %d = %dn", i, j, j*i);
    }
    printf("n");
}
Nested for Loops
• We can nest any statement, even
  another for loop, inside the body of
  a parent for loop.
• When we nest a child for loop, it
  iterates all of it’s cycles for each
  iteration of the parent.
Example of nested for loop
• /* Demonstration of nested loops */
                                              When you run this
main( )                                       program you will get the
{                                             following output:
                                              r = 1 c = 1 sum = 2
int r, c, sum ;                               r = 1 c = 2 sum = 3
                                              r = 2 c = 1 sum = 3
for ( r = 1 ; r <= 3 ; r++ ) /* outer loop */ r = 2 c = 2 sum = 4
{                                             r = 3 c = 1 sum = 4
                                              r = 3 c = 2 sum = 5
for ( c = 1 ; c <= 2 ; c++ ) /* inner loop */
{
sum = r + c ;
printf ( "r = %d c = %d sum = %dn", r, c, sum ) ;
}
}
}
The do … while Loop
• C implements a post-test loop using a
  structure called a do … while loop.
• In the do … while, the loop begins with
  the keyword do, followed by the body,
  followed by the keyword while and the
  loop expression.
• A semi-colon (;) follows the loop
  expression.
The do … while Loop
Example of do-while
/* Execution of a loop an unknown number of time*/
main( )
{
                                     Output:
char another ;                       Enter a number 5
int num ;                            square of 5 is 25
                                     Want to enter another number y/n y
do                                   Enter a number 7
                                     square of 7 is 49
{                                    Want to enter another number y/n n
printf ( "Enter a number " ) ;
scanf ( "%d", &num ) ;
printf ( "square of %d is %d", num, num * num ) ;
printf ( "nWant to enter another number y/n " ) ;
scanf ( " %c", &another ) ;
} while ( another == 'y' ) ;}
Same using for loop
/* odd loop using a for loop */
main( )
{
char another = 'y' ;
int num ;
for ( ; another == 'y' ; )
{
printf ( "Enter a number " ) ;
scanf ( "%d", &num ) ;
printf ( "square of %d is %d", num, num * num ) ;
printf ( "nWant to enter another number y/n " ) ;
scanf ( " %c", &another ) ;
Same using while loop
/* odd loop using a while loop */
main( )
{
char another = 'y' ;
int num ;
while ( another == 'y' )
{
printf ( "Enter a number " ) ;
scanf ( "%d", &num ) ;
printf ( "square of %d is %d", num, num * num ) ;
printf ( "nWant to enter another number y/n " ) ;
scanf ( " %c", &another ) ;
}}
Comparing
do … while with while
The break Statement in Loops
• We often come across situations where we want
  to jump out of a loop instantly, without waiting to
  get back to the conditional test.
• The keyword break allows us to do this.
• When break is encountered inside any loop,
  control automatically passes to the first
  statement after the loop.
Example of break
• Write a program to determine whether a number is
  prime or not. A prime number is one, which is
  divisible only by 1 or itself.
• All we have to do to test whether a number is prime or
  not, is to divide it successively by all numbers from 2 to
  one less than itself.
• If remainder of any of these divisions is zero, the number
  is not a prime. If no division yields a zero then the
  number is a prime number.
Example
main( )
{int num, i ;
printf ( "Enter a number " ) ;
scanf ( "%d", &num ) ;
i=2;
while ( i <= num - 1 )
{if ( num % i == 0 )
{
printf ( "Not a prime number" ) ;
break ;
}
i++ ;
} if ( i == num )
printf ( "Prime number" ) ;
}
Example
                          main( )
The keyword break
  breaks the              {
  control only            int i = 1 , j = 1 ;
  from the while in       while ( i++ <= 100 )
  which it is placed.     {
                          while ( j++ <= 200 )
                          {
                          if ( j == 150 )
                          break ;
                          else
                          printf ( "%d %dn", i, j ) ;
                          }
                          }
                          }
The continue Statement
• In some programming situations we want to take
  the control to the beginning of the loop,
  bypassing the statements inside the loop,
   which have not yet been executed. The keyword
  continue allows us to do this.
• When continue is encountered inside any
  loop, control automatically passes to the
  beginning of the loop.
Example of continue statement
main( )
{
                                 The output of the
int i, j ;
                                 above program would
for ( i = 1 ; i <= 2 ; i++ )     be...
{                                12
for ( j = 1 ; j <= 2 ; j++ )     21
{
if ( i == j )
continue ;
printf ( "n%d %dn", i, j ) ;
}
}

More Related Content

What's hot

C++ control loops
C++ control loopsC++ control loops
C++ control loops
pratikborsadiya
 
Nested loops
Nested loopsNested loops
Nested loops
Neeru Mittal
 
For Loop
For LoopFor Loop
For Loop
Ghaffar Khan
 
C Language - Switch and For Loop
C Language - Switch and For LoopC Language - Switch and For Loop
C Language - Switch and For Loop
Sukrit Gupta
 
7 functions
7  functions7  functions
7 functions
MomenMostafa
 
Looping Statement And Flow Chart
 Looping Statement And Flow Chart Looping Statement And Flow Chart
Looping Statement And Flow Chart
Rahul Sahu
 
The solution manual of c by robin
The solution manual of c by robinThe solution manual of c by robin
The solution manual of c by robin
Abdullah Al Naser
 
Programming ppt files (final)
Programming ppt files (final)Programming ppt files (final)
Programming ppt files (final)
yap_raiza
 
C programs
C programsC programs
C programs
Minu S
 
8 arrays and pointers
8  arrays and pointers8  arrays and pointers
8 arrays and pointers
MomenMostafa
 
Decision Making and Looping
Decision Making and LoopingDecision Making and Looping
Decision Making and Looping
Munazza-Mah-Jabeen
 
88 c-programs
88 c-programs88 c-programs
88 c-programs
Leandro Schenone
 
4 operators, expressions &amp; statements
4  operators, expressions &amp; statements4  operators, expressions &amp; statements
4 operators, expressions &amp; statements
MomenMostafa
 
Unit2 C
Unit2 CUnit2 C
Unit2 C
arnold 7490
 
Unit 5 Foc
Unit 5 FocUnit 5 Foc
Unit 5 Foc
JAYA
 
Practical write a c program to reverse a given number
Practical write a c program to reverse a given numberPractical write a c program to reverse a given number
Practical write a c program to reverse a given number
Mainak Sasmal
 
Cpds lab
Cpds labCpds lab
Ansi c
Ansi cAnsi c
C PROGRAMS
C PROGRAMSC PROGRAMS
Introduction to c part -1
Introduction to c   part -1Introduction to c   part -1

What's hot (20)

C++ control loops
C++ control loopsC++ control loops
C++ control loops
 
Nested loops
Nested loopsNested loops
Nested loops
 
For Loop
For LoopFor Loop
For Loop
 
C Language - Switch and For Loop
C Language - Switch and For LoopC Language - Switch and For Loop
C Language - Switch and For Loop
 
7 functions
7  functions7  functions
7 functions
 
Looping Statement And Flow Chart
 Looping Statement And Flow Chart Looping Statement And Flow Chart
Looping Statement And Flow Chart
 
The solution manual of c by robin
The solution manual of c by robinThe solution manual of c by robin
The solution manual of c by robin
 
Programming ppt files (final)
Programming ppt files (final)Programming ppt files (final)
Programming ppt files (final)
 
C programs
C programsC programs
C programs
 
8 arrays and pointers
8  arrays and pointers8  arrays and pointers
8 arrays and pointers
 
Decision Making and Looping
Decision Making and LoopingDecision Making and Looping
Decision Making and Looping
 
88 c-programs
88 c-programs88 c-programs
88 c-programs
 
4 operators, expressions &amp; statements
4  operators, expressions &amp; statements4  operators, expressions &amp; statements
4 operators, expressions &amp; statements
 
Unit2 C
Unit2 CUnit2 C
Unit2 C
 
Unit 5 Foc
Unit 5 FocUnit 5 Foc
Unit 5 Foc
 
Practical write a c program to reverse a given number
Practical write a c program to reverse a given numberPractical write a c program to reverse a given number
Practical write a c program to reverse a given number
 
Cpds lab
Cpds labCpds lab
Cpds lab
 
Ansi c
Ansi cAnsi c
Ansi c
 
C PROGRAMS
C PROGRAMSC PROGRAMS
C PROGRAMS
 
Introduction to c part -1
Introduction to c   part -1Introduction to c   part -1
Introduction to c part -1
 

Viewers also liked

Cmp 104
Cmp 104Cmp 104
Cmp 104
kapil078
 
Programming in c++
Programming in c++Programming in c++
Programming in c++
Baljit Saini
 
04 control structures 1
04 control structures 104 control structures 1
04 control structures 1
Jomel Penalba
 
Control structures in C++ Programming Language
Control structures in C++ Programming LanguageControl structures in C++ Programming Language
Control structures in C++ Programming Language
Ahmad Idrees
 
Control Structures in Visual Basic
Control Structures in  Visual BasicControl Structures in  Visual Basic
Control Structures in Visual Basic
Tushar Jain
 
Control statements
Control statementsControl statements
Control statements
Kanwalpreet Kaur
 
Loops c++
Loops c++Loops c++
Loops c++
Shivani Singh
 
C++ programming
C++ programmingC++ programming
C++ programming
viancagerone
 

Viewers also liked (8)

Cmp 104
Cmp 104Cmp 104
Cmp 104
 
Programming in c++
Programming in c++Programming in c++
Programming in c++
 
04 control structures 1
04 control structures 104 control structures 1
04 control structures 1
 
Control structures in C++ Programming Language
Control structures in C++ Programming LanguageControl structures in C++ Programming Language
Control structures in C++ Programming Language
 
Control Structures in Visual Basic
Control Structures in  Visual BasicControl Structures in  Visual Basic
Control Structures in Visual Basic
 
Control statements
Control statementsControl statements
Control statements
 
Loops c++
Loops c++Loops c++
Loops c++
 
C++ programming
C++ programmingC++ programming
C++ programming
 

Similar to 12 lec 12 loop

Loops in c
Loops in cLoops in c
Loops in c
shubhampandav3
 
Loop control structure
Loop control structureLoop control structure
Loop control structure
Karan Pandey
 
COM1407: Program Control Structures – Repetition and Loops
COM1407: Program Control Structures – Repetition and Loops COM1407: Program Control Structures – Repetition and Loops
COM1407: Program Control Structures – Repetition and Loops
Hemantha Kulathilake
 
INPUT AND OUTPUT STATEMENTS IN PROGRAMMING IN C
INPUT AND OUTPUT STATEMENTS IN PROGRAMMING IN CINPUT AND OUTPUT STATEMENTS IN PROGRAMMING IN C
INPUT AND OUTPUT STATEMENTS IN PROGRAMMING IN C
Chandrakant Divate
 
Cse115 lecture08repetitionstructures part02
Cse115 lecture08repetitionstructures part02Cse115 lecture08repetitionstructures part02
Cse115 lecture08repetitionstructures part02
Md. Ashikur Rahman
 
Control structure of c
Control structure of cControl structure of c
Control structure of c
Komal Kotak
 
Workbook_2_Problem_Solving_and_programming.pdf
Workbook_2_Problem_Solving_and_programming.pdfWorkbook_2_Problem_Solving_and_programming.pdf
Workbook_2_Problem_Solving_and_programming.pdf
DrDineshenScientist
 
Programming fundamental 02
Programming fundamental 02Programming fundamental 02
Programming fundamental 02
Suhail Akraam
 
Control Structures in C
Control Structures in CControl Structures in C
Control Structures in C
sana shaikh
 
Chapter06.PPT
Chapter06.PPTChapter06.PPT
Chapter06.PPT
vamsiKrishnasai3
 
Dti2143 chap 4 control statement part 2
Dti2143 chap 4 control statement part 2Dti2143 chap 4 control statement part 2
Dti2143 chap 4 control statement part 2
alish sha
 
Learning C programming - from lynxbee.com
Learning C programming - from lynxbee.comLearning C programming - from lynxbee.com
Learning C programming - from lynxbee.com
Green Ecosystem
 
Session 3
Session 3Session 3
Vcs5
Vcs5Vcs5
C programming Control Structure.pptx
C programming Control Structure.pptxC programming Control Structure.pptx
C programming Control Structure.pptx
DEEPAK948083
 
Ch6 Loops
Ch6 LoopsCh6 Loops
Ch6 Loops
SzeChingChen
 
Computer programming subject notes. Quick easy notes for C Programming.Cheat ...
Computer programming subject notes. Quick easy notes for C Programming.Cheat ...Computer programming subject notes. Quick easy notes for C Programming.Cheat ...
Computer programming subject notes. Quick easy notes for C Programming.Cheat ...
DR B.Surendiran .
 
1 introduction to c program
1 introduction to c program1 introduction to c program
1 introduction to c program
NishmaNJ
 
What is c
What is cWhat is c
What is c
Nitesh Saitwal
 
Understand more about C
Understand more about CUnderstand more about C
Understand more about C
Yi-Hsiu Hsu
 

Similar to 12 lec 12 loop (20)

Loops in c
Loops in cLoops in c
Loops in c
 
Loop control structure
Loop control structureLoop control structure
Loop control structure
 
COM1407: Program Control Structures – Repetition and Loops
COM1407: Program Control Structures – Repetition and Loops COM1407: Program Control Structures – Repetition and Loops
COM1407: Program Control Structures – Repetition and Loops
 
INPUT AND OUTPUT STATEMENTS IN PROGRAMMING IN C
INPUT AND OUTPUT STATEMENTS IN PROGRAMMING IN CINPUT AND OUTPUT STATEMENTS IN PROGRAMMING IN C
INPUT AND OUTPUT STATEMENTS IN PROGRAMMING IN C
 
Cse115 lecture08repetitionstructures part02
Cse115 lecture08repetitionstructures part02Cse115 lecture08repetitionstructures part02
Cse115 lecture08repetitionstructures part02
 
Control structure of c
Control structure of cControl structure of c
Control structure of c
 
Workbook_2_Problem_Solving_and_programming.pdf
Workbook_2_Problem_Solving_and_programming.pdfWorkbook_2_Problem_Solving_and_programming.pdf
Workbook_2_Problem_Solving_and_programming.pdf
 
Programming fundamental 02
Programming fundamental 02Programming fundamental 02
Programming fundamental 02
 
Control Structures in C
Control Structures in CControl Structures in C
Control Structures in C
 
Chapter06.PPT
Chapter06.PPTChapter06.PPT
Chapter06.PPT
 
Dti2143 chap 4 control statement part 2
Dti2143 chap 4 control statement part 2Dti2143 chap 4 control statement part 2
Dti2143 chap 4 control statement part 2
 
Learning C programming - from lynxbee.com
Learning C programming - from lynxbee.comLearning C programming - from lynxbee.com
Learning C programming - from lynxbee.com
 
Session 3
Session 3Session 3
Session 3
 
Vcs5
Vcs5Vcs5
Vcs5
 
C programming Control Structure.pptx
C programming Control Structure.pptxC programming Control Structure.pptx
C programming Control Structure.pptx
 
Ch6 Loops
Ch6 LoopsCh6 Loops
Ch6 Loops
 
Computer programming subject notes. Quick easy notes for C Programming.Cheat ...
Computer programming subject notes. Quick easy notes for C Programming.Cheat ...Computer programming subject notes. Quick easy notes for C Programming.Cheat ...
Computer programming subject notes. Quick easy notes for C Programming.Cheat ...
 
1 introduction to c program
1 introduction to c program1 introduction to c program
1 introduction to c program
 
What is c
What is cWhat is c
What is c
 
Understand more about C
Understand more about CUnderstand more about C
Understand more about C
 

More from kapil078

11 lec 11 storage class
11 lec 11 storage class11 lec 11 storage class
11 lec 11 storage class
kapil078
 
Lec 10
Lec 10Lec 10
Lec 10
kapil078
 
Btech 1st yr midterm 1st
Btech 1st yr midterm 1stBtech 1st yr midterm 1st
Btech 1st yr midterm 1stkapil078
 
Lec9
Lec9Lec9
Lec9
kapil078
 
Cmp104 lec 7 algorithm and flowcharts
Cmp104 lec 7 algorithm and flowchartsCmp104 lec 7 algorithm and flowcharts
Cmp104 lec 7 algorithm and flowcharts
kapil078
 
Cmp104 lec 6 computer lang
Cmp104 lec 6 computer langCmp104 lec 6 computer lang
Cmp104 lec 6 computer lang
kapil078
 
Cmp104 lec 2 number system
Cmp104 lec 2 number systemCmp104 lec 2 number system
Cmp104 lec 2 number system
kapil078
 
Cmp104 lec 6 computer lang
Cmp104 lec 6 computer langCmp104 lec 6 computer lang
Cmp104 lec 6 computer lang
kapil078
 
Cmp104 lec 4 types of computer
Cmp104 lec 4 types of computerCmp104 lec 4 types of computer
Cmp104 lec 4 types of computer
kapil078
 
Cmp104 lec 3 component of computer
Cmp104 lec 3 component of computerCmp104 lec 3 component of computer
Cmp104 lec 3 component of computer
kapil078
 
Cmp104 lec 1
Cmp104 lec 1Cmp104 lec 1
Cmp104 lec 1
kapil078
 
cmp104 lec 8
cmp104 lec 8cmp104 lec 8
cmp104 lec 8
kapil078
 

More from kapil078 (12)

11 lec 11 storage class
11 lec 11 storage class11 lec 11 storage class
11 lec 11 storage class
 
Lec 10
Lec 10Lec 10
Lec 10
 
Btech 1st yr midterm 1st
Btech 1st yr midterm 1stBtech 1st yr midterm 1st
Btech 1st yr midterm 1st
 
Lec9
Lec9Lec9
Lec9
 
Cmp104 lec 7 algorithm and flowcharts
Cmp104 lec 7 algorithm and flowchartsCmp104 lec 7 algorithm and flowcharts
Cmp104 lec 7 algorithm and flowcharts
 
Cmp104 lec 6 computer lang
Cmp104 lec 6 computer langCmp104 lec 6 computer lang
Cmp104 lec 6 computer lang
 
Cmp104 lec 2 number system
Cmp104 lec 2 number systemCmp104 lec 2 number system
Cmp104 lec 2 number system
 
Cmp104 lec 6 computer lang
Cmp104 lec 6 computer langCmp104 lec 6 computer lang
Cmp104 lec 6 computer lang
 
Cmp104 lec 4 types of computer
Cmp104 lec 4 types of computerCmp104 lec 4 types of computer
Cmp104 lec 4 types of computer
 
Cmp104 lec 3 component of computer
Cmp104 lec 3 component of computerCmp104 lec 3 component of computer
Cmp104 lec 3 component of computer
 
Cmp104 lec 1
Cmp104 lec 1Cmp104 lec 1
Cmp104 lec 1
 
cmp104 lec 8
cmp104 lec 8cmp104 lec 8
cmp104 lec 8
 

Recently uploaded

World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024
ak6969907
 
South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)
Academy of Science of South Africa
 
Film vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movieFilm vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movie
Nicholas Montgomery
 
Smart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICTSmart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICT
simonomuemu
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Akanksha trivedi rama nursing college kanpur.
 
Main Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docxMain Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docx
adhitya5119
 
clinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdfclinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdf
Priyankaranawat4
 
S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
tarandeep35
 
Advanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docxAdvanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docx
adhitya5119
 
How to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold MethodHow to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold Method
Celine George
 
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
National Information Standards Organization (NISO)
 
Walmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdfWalmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdf
TechSoup
 
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective UpskillingYour Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Excellence Foundation for South Sudan
 
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
RitikBhardwaj56
 
BBR 2024 Summer Sessions Interview Training
BBR  2024 Summer Sessions Interview TrainingBBR  2024 Summer Sessions Interview Training
BBR 2024 Summer Sessions Interview Training
Katrina Pritchard
 
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
PECB
 
Community pharmacy- Social and preventive pharmacy UNIT 5
Community pharmacy- Social and preventive pharmacy UNIT 5Community pharmacy- Social and preventive pharmacy UNIT 5
Community pharmacy- Social and preventive pharmacy UNIT 5
sayalidalavi006
 
Hindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdfHindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdf
Dr. Mulla Adam Ali
 
Digital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental DesignDigital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental Design
amberjdewit93
 
The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
History of Stoke Newington
 

Recently uploaded (20)

World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024
 
South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)
 
Film vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movieFilm vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movie
 
Smart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICTSmart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICT
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
 
Main Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docxMain Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docx
 
clinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdfclinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdf
 
S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
 
Advanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docxAdvanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docx
 
How to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold MethodHow to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold Method
 
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
 
Walmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdfWalmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdf
 
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective UpskillingYour Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective Upskilling
 
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
 
BBR 2024 Summer Sessions Interview Training
BBR  2024 Summer Sessions Interview TrainingBBR  2024 Summer Sessions Interview Training
BBR 2024 Summer Sessions Interview Training
 
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
 
Community pharmacy- Social and preventive pharmacy UNIT 5
Community pharmacy- Social and preventive pharmacy UNIT 5Community pharmacy- Social and preventive pharmacy UNIT 5
Community pharmacy- Social and preventive pharmacy UNIT 5
 
Hindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdfHindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdf
 
Digital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental DesignDigital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental Design
 
The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
 

12 lec 12 loop

  • 2. Goals By the end of this unit, you should understand … • … basic loop concepts, including pretest & posttest loops, loop initialization ,condition and increment. • … how to program a while loop. • … how to program a do-while loop. • … how to program a for loops.
  • 3. What is a loop? • A loop is a programming structure that allows an action to repeat until the program meets a given condition. • After each iteration of a loop, the loop checks against a loop control expression to see if the program met the given condition. If it did, the loop stops. If not, the loop moves on to the next iteration.
  • 4. Types of Loops • C supports two categories of loops, based on where the program tests the condition: – Pretest Loops – Post-test Loops
  • 5. Pretest Loops • With each iteration, the program tests the condition first before executing the loop’s block. • If the condition results to true, the loop continues and executes the block; if the condition results to false, the loop terminates. • With a pretest loop, there is a chance the loop may never execute once in the program.
  • 6. Post-Test Loops • With each iteration, the program executes the loop’s block first and tests against a condition. • If the condition tests to true, the loop continues and executes another iteration; if the condition tests to false, the loop terminates. • With a post-test loop, the loop will always execute at least once!
  • 7. Pretest vs. Post-Test Loops from Figure 6-2 in Forouzan & Gilberg, p. 305
  • 8. Variable Initialization • int i=5; • I have initialized the variable i with a value i=5;
  • 9. Updating a Loop • A loop update is what happens inside a loop’s block that eventually causes the loop to satisfy the condition, thus ending the loop. • Updating happens during each loop iteration. • Without a loop update, the loop would be an infinite loop.
  • 11.
  • 12. Loop Comparison from Table 6-1 in Forouzan & Gilberg, p. 308
  • 13. C Implementation of Loops • Pretest Loops – while Loop – for Loop • Post-test Loop – do … while loop • All loops in C execute so long as a condition evaluates to true and terminate when the condition evaluates to false.
  • 14. The while Loop from Figure 6-11 in Forouzan & Gilberg, p. 311
  • 15. Example of while loop #include<stdio.h> #include<conio.h> If we input value int main() of end as 10 then { output will be 1 to int start, end; 10 scanf("%d",&end); start = 0; while ( start < end) { start++; printf("%dn", start); } return 0;
  • 16. Example #include<stdio.h> The output of the postfix and prefix increment example will int main() look like this: { int i; 1 2 i = 0; 3 4 while(i++ < 5) 5 { printf("%dn", i); } 1 printf("n"); 2 3 i = 0; 4 while(++i < 5) { printf("%dn", i); } return 0; }
  • 17. The for Loop • A for loop is a pretest loop that includes three expressions in its header: – Loop initialization statement – Limit test expression(condition) – Loop update statement • The for loop is often used as a start-controlled loop since we can accurately predict the maximum number of iterations.
  • 20. Comparing while with for i = 1; sum = 0; sum =0; for (i = 1; i <= 20; i++) while (i <= 20) { { scanf(“%d”, &a); scanf(“%d”, &a); sum = sum+a; sum = sum+a; }//end for i++ }//end while The while Loop The for Loop
  • 21. Example of for loop main ( ) { int p, n, count ; float r, si ; for ( count = 1 ; count <= 3 ; count = count + 1 ) { printf ( "Enter values of p, n, and r " ) ; scanf ( "%d %d %f", &p, &n, &r ) ; si = (p * n * r) / 100 ; printf ( "Simple Interest = Rs.%fn", si ) ; } }
  • 22. Example #include <stdio.h> #include<conio.h> void main() { int i = 0, j = 8; printf("Times 8 Tablen"); for(i = 0; i <= 12; i = i + 1) { printf("%d x %d = %dn", i, j, j*i); } printf("n"); }
  • 23. Nested for Loops • We can nest any statement, even another for loop, inside the body of a parent for loop. • When we nest a child for loop, it iterates all of it’s cycles for each iteration of the parent.
  • 24. Example of nested for loop • /* Demonstration of nested loops */ When you run this main( ) program you will get the { following output: r = 1 c = 1 sum = 2 int r, c, sum ; r = 1 c = 2 sum = 3 r = 2 c = 1 sum = 3 for ( r = 1 ; r <= 3 ; r++ ) /* outer loop */ r = 2 c = 2 sum = 4 { r = 3 c = 1 sum = 4 r = 3 c = 2 sum = 5 for ( c = 1 ; c <= 2 ; c++ ) /* inner loop */ { sum = r + c ; printf ( "r = %d c = %d sum = %dn", r, c, sum ) ; } } }
  • 25. The do … while Loop • C implements a post-test loop using a structure called a do … while loop. • In the do … while, the loop begins with the keyword do, followed by the body, followed by the keyword while and the loop expression. • A semi-colon (;) follows the loop expression.
  • 26. The do … while Loop
  • 27. Example of do-while /* Execution of a loop an unknown number of time*/ main( ) { Output: char another ; Enter a number 5 int num ; square of 5 is 25 Want to enter another number y/n y do Enter a number 7 square of 7 is 49 { Want to enter another number y/n n printf ( "Enter a number " ) ; scanf ( "%d", &num ) ; printf ( "square of %d is %d", num, num * num ) ; printf ( "nWant to enter another number y/n " ) ; scanf ( " %c", &another ) ; } while ( another == 'y' ) ;}
  • 28. Same using for loop /* odd loop using a for loop */ main( ) { char another = 'y' ; int num ; for ( ; another == 'y' ; ) { printf ( "Enter a number " ) ; scanf ( "%d", &num ) ; printf ( "square of %d is %d", num, num * num ) ; printf ( "nWant to enter another number y/n " ) ; scanf ( " %c", &another ) ;
  • 29. Same using while loop /* odd loop using a while loop */ main( ) { char another = 'y' ; int num ; while ( another == 'y' ) { printf ( "Enter a number " ) ; scanf ( "%d", &num ) ; printf ( "square of %d is %d", num, num * num ) ; printf ( "nWant to enter another number y/n " ) ; scanf ( " %c", &another ) ; }}
  • 31. The break Statement in Loops • We often come across situations where we want to jump out of a loop instantly, without waiting to get back to the conditional test. • The keyword break allows us to do this. • When break is encountered inside any loop, control automatically passes to the first statement after the loop.
  • 32. Example of break • Write a program to determine whether a number is prime or not. A prime number is one, which is divisible only by 1 or itself. • All we have to do to test whether a number is prime or not, is to divide it successively by all numbers from 2 to one less than itself. • If remainder of any of these divisions is zero, the number is not a prime. If no division yields a zero then the number is a prime number.
  • 33. Example main( ) {int num, i ; printf ( "Enter a number " ) ; scanf ( "%d", &num ) ; i=2; while ( i <= num - 1 ) {if ( num % i == 0 ) { printf ( "Not a prime number" ) ; break ; } i++ ; } if ( i == num ) printf ( "Prime number" ) ; }
  • 34. Example main( ) The keyword break breaks the { control only int i = 1 , j = 1 ; from the while in while ( i++ <= 100 ) which it is placed. { while ( j++ <= 200 ) { if ( j == 150 ) break ; else printf ( "%d %dn", i, j ) ; } } }
  • 35. The continue Statement • In some programming situations we want to take the control to the beginning of the loop, bypassing the statements inside the loop, which have not yet been executed. The keyword continue allows us to do this. • When continue is encountered inside any loop, control automatically passes to the beginning of the loop.
  • 36. Example of continue statement main( ) { The output of the int i, j ; above program would for ( i = 1 ; i <= 2 ; i++ ) be... { 12 for ( j = 1 ; j <= 2 ; j++ ) 21 { if ( i == j ) continue ; printf ( "n%d %dn", i, j ) ; } }

Editor's Notes

  1. 02/21/13
  2. 02/21/13