SlideShare une entreprise Scribd logo
1  sur  27
Iteration and Repetitive Executions


        www.eshikshak.co.in
Introduction
• It is a tedious process perform such kind of
  tasks repeatedly with pen/pencil and paper.
• Computer Programming language and
  packages, the task becomes easy, accurate
  and fast.
What is a “Loop” ?
• A loop is defined as a block of statements,
  which are repeatedly executed for a certain
  number of times.
• The loops are of two types
  – Counter Controlled Repetition
  – Sentinel Controlled Repetition
Counter Controlled Repetition
• It is also called the definite repetition action i.e. the
  number of iteration to be performed is defined in
  advance in the program itself.
• Steps in this type of loop
   – Loop Variable
       • Variable used in the loop
   – Initialization
       • It is first step in which starting and final values are assigned to the
         loop variable.
       • Each time the value updated is checked by the loop itself.
   – Increment and Decrement
       • It is the numerical value added or subtracted to the variable in
         each round of the loop
       • The updated value is compared with the final value and it is found
         less than the final value the steps in the loop are executed
Sentinel-Controlled Repetition
• It is also called the indefinite repetition action
  i.e. One cannot estimate how many iteration
  are to be performed.
• In this type, Loop termination happens on the
  basis of certain condition using decision-
  making statement.
Types of ‘C’ loops
for                            while                 do-while



for(expression 1; expression 2 expression 1;         expression 1;
; expression 3)                while(expression 2)   do
{                              {                     {
      statement 1;                  statement 1;           statement 1;
      statement 2;                   statement 2;          statement 2;
      .                              .                     .
      statement N;                   statement N;          statement N;
}                                    expression 3;   } while(expression 3);
                               }
for Loop
    • for loop allows to execute a set of instruction
      until certain condition is satisfied.
    • Condition may be predefined or open-ended.
for(initialize counter; test condition; re-evaluation parameter)
{
       statement 1;
       statement 2;
       .
       statement N;
}
for Loop
• The initialize counter sets to an initial value. This
  statement is executed only once.
• The test condition is a relational expression that
  determines the number of iterations desired or
  determines when to exit from the loop.
• The “for” loop continues to execute as long as
  conditional test is satisfied.
• When condition becomes false the control of
  program exists from the body of the “for” loop
  and executes next statement after the body of the
  loop.
for Loop
• The re-evaluation parameter decides how to make
  changes int the loop (increment or decrement
  operations are to be used quite often).
• The body of loop may contain either a single
  statement or multiple statements.
for Loop
Various formats of ‘for’ loop
Syntax                   Output                        Remarks
for( ; ; )               Infinite loop                 No Arguments
for( a=0; a<=20 ; )      Infinite loop                 ‘a’ is neither incremented nor
                                                       decremented
for( a=0; a<=10 ; a++) Displays value from 0 to 10     ‘a’ is increment from 0 to 10.
printf(“%d”, a);                                       Curly braces are not necessary.
                                                       Default scope of the for loop is
                                                       one statement after the for loop.
for( a=10; a>=0 ; a--)   Displays value from 10 to 0   ‘a’ is decremented from 10 to 0.
printf(“%d”, a);
Print the first five numbers starting from 1 together with their squares
void main()
{
       int I;
       clrscr();

        for(i=1;i<=5;i++)
        {
                  printf(“Number :%d it’s Square : %d”, i, i * i);
        }
}

OUTPUT :
Number : 1 it’s Square: 1
Number : 2 it’s Square: 4
Number : 3 it’s Square : 9
Number : 4 it’s Square: 16
Number : 5 it’s Square: 25
Print the first five numbers starting from 1 together with their squares
void main()
{
       int i;
       clrscr();

        for(i=1;i<=5;i++)
                            printf(“Number :%d it’s Square : %d”, i, i * i);
}

OUTPUT :
Number : 1 it’s Square: 1
Number : 2 it’s Square: 4
Number : 3 it’s Square : 9
Number : 4 it’s Square: 16
Number : 5 it’s Square: 25
Print the first five numbers starting from 1 together with their squares
void main()
{
       int i=1;
       clrscr();

        for(;i<=5;i++)
                         printf(“Number :%d it’s Square : %d”, i, i * i);
}

OUTPUT :
Number : 1 it’s Square: 1
Number : 2 it’s Square: 4
Number : 3 it’s Square : 9
Number : 4 it’s Square: 16
Number : 5 it’s Square: 25
Print the first five numbers starting from 1 together with their squares
void main()
{
       int i=1;
       clrscr();

        for(;i<=5;)
        {
                         printf(“Number :%d it’s Square : %d”, i, i * i);
                         i++;
        }
}

OUTPUT :
Number : 1 it’s Square: 1
Number : 2 it’s Square: 4
Number : 3 it’s Square : 9
Number : 4 it’s Square: 16
Number : 5 it’s Square: 25
Print the first five numbers starting from 1 together with their squares
void main()
{
       int I;
       clrscr();

        for(i=1;i<=5;i++);
        {
                  printf(“Number :%d it’s Square : %d”, i, i * i);
        }
                  printf(“Number :%d it’s Square : %d”, i, i * i);
        getch();
}
Nested for loops
• We can use loop inside loop, which is known
  as nested loop
• In nested for loops one or more for
  statements are included in the body of the
  loop.
• The numbers of iterations in this type of
  structure will be equal to the number of
  iteration in the outer loop multiplied by the
  number of iterations in the inner loop.
Print the first five numbers starting from 1 together with their squares

void main()
{
       int a, b, sub;
       clrscr();

         for(a=3;a>=1;a--);
         {
                  for(b=1; b<=2; b++)
                  {
                       sub = a – b;
                       printf(“a = %d b = %d a – b = %dn”, a, b, sub);
                  }
         }
         getch();
}
Output:
a=3b=1a–b=2
a=3b=2a–b=1
a=2b=1a–b=1
a=2b=2a–b=0
a=2b=2a–b=0
a=1b=1a–b=0
a = 1 b = 1 a – b = -1
while loop
• The while loop is frequently used in programs
  for the repeated execution of statement in a
  loop.
• The loop continues until a certain condition is
  satisfied.
while loop
The syntax of while loop


while(test-condition)
{
       body of the loop;
}
while loop
• The test condition is indicated at the top and its tests the
  value of the expression before processing the body of
  the loop.
• The loop statements will be executed till the condition is
  true.
• When the condition becomes false the execution will be
  out of the loop.
• The braces are needed only if the body of the loop
  contains more than one statement.
• However, it is a good practice to use braces even if the
  body of the loop contains only one statement.
• Steps of while loops are as
    Entry
                                follows
                                 – The     test    condition      is
   Initialize                      evaluated and if it is true, the
                                   body of the loop is executed.
                                 – On execution of the body,
                   F
     Test                          test condition is repetitively
   Condition                       checked and if it is true the
                                   body is executed.
                       Stop
    T                            – The process of execution of
                                   the body will be continued till
Body of the loop                   the test condition becomes
                                   true.
                                 – The control is transferred out
    Update                         of the loop if test condition
                                   fails.
Print “You are in MESICS” string for nine times
                                                  OUTPUT :
                                                  You are in MESICS
                                                  You are in MESICS
void main()                                       You are in MESICS
{                                                 You are in MESICS
                                                  You are in MESICS
       int i=1;                                   You are in MESICS
       clrscr();                                  You are in MESICS
                                                  You are in MESICS
         while(i<=9)                              You are in MESICS
         {
               printf(“You are in MESICSn”);
               i++;
         }
}
do…while loop
The syntax of do…while loop


do
{
    statements;
} while(test-condition);
do…while loop
• The difference between the while and do-while loop
  is the place where the condition is to be tested.
• In the while loops the condition is tested following
  the while statement, and then the body gets
  executed.
• In the do…while the condition is checked at the end
  of the loop.
• The do…while loop will execute at least one time
  even if the condition is false initially.
• The do…while loop executes until the condition
  becomes false.
Comparison of while and do..while
Sr. No   while loop                        do…while loop

         Condition is specified at the     Condition is mentioned at the
1.
         top.                              bottom.

         Body statement/s is/are
                                           Body statement/s executes even
2.       executed when condition is
                                           when the condition is false.
         satisfied.


         No brackets for a single          Brackets are essential even when a
3.
         statement.                        single statement exists.


4.       It is an entry-controlled loop.   It is an exit-controlled loop.
Print “You are in MESICS” string for nine times
                                                  OUTPUT :
                                                  You are in MESICS
                                                  You are in MESICS
void main()                                       You are in MESICS
{                                                 You are in MESICS
                                                  You are in MESICS
       int i=1;
       clrscr();

         do
         {
                printf(“You are in MESICSn”);
                i++;
         } while(i<=5);
}
Sr. No.   Program
1.

Contenu connexe

Tendances

Loops in matlab
Loops in matlabLoops in matlab
Loops in matlab
TUOS-Sam
 
Looping statements in Java
Looping statements in JavaLooping statements in Java
Looping statements in Java
Jin Castor
 
Java Programming: Loops
Java Programming: LoopsJava Programming: Loops
Java Programming: Loops
Karwan Mustafa Kareem
 
Chapter 13.1.5
Chapter 13.1.5Chapter 13.1.5
Chapter 13.1.5
patcha535
 

Tendances (20)

Loops in c language
Loops in c languageLoops in c language
Loops in c language
 
Loops in matlab
Loops in matlabLoops in matlab
Loops in matlab
 
MATLAB Programming - Loop Control Part 2
MATLAB Programming - Loop Control Part 2MATLAB Programming - Loop Control Part 2
MATLAB Programming - Loop Control Part 2
 
Flow of control ppt
Flow of control pptFlow of control ppt
Flow of control ppt
 
Algorithm and Programming (Branching Structure)
Algorithm and Programming (Branching Structure)Algorithm and Programming (Branching Structure)
Algorithm and Programming (Branching Structure)
 
Looping
LoopingLooping
Looping
 
Java loops
Java loopsJava loops
Java loops
 
Control statement in c
Control statement in cControl statement in c
Control statement in c
 
Looping statements in Java
Looping statements in JavaLooping statements in Java
Looping statements in Java
 
Java Programming: Loops
Java Programming: LoopsJava Programming: Loops
Java Programming: Loops
 
Chapter 13.1.5
Chapter 13.1.5Chapter 13.1.5
Chapter 13.1.5
 
C++ loop
C++ loop C++ loop
C++ loop
 
Loop c++
Loop c++Loop c++
Loop c++
 
Iteration
IterationIteration
Iteration
 
Looping Statement And Flow Chart
 Looping Statement And Flow Chart Looping Statement And Flow Chart
Looping Statement And Flow Chart
 
Comp ppt (1)
Comp ppt (1)Comp ppt (1)
Comp ppt (1)
 
While , For , Do-While Loop
While , For , Do-While LoopWhile , For , Do-While Loop
While , For , Do-While Loop
 
Loops in C Programming | for Loop | do-while Loop | while Loop | Nested Loop
Loops in C Programming | for Loop | do-while Loop | while Loop | Nested LoopLoops in C Programming | for Loop | do-while Loop | while Loop | Nested Loop
Loops in C Programming | for Loop | do-while Loop | while Loop | Nested Loop
 
Loops Basics
Loops BasicsLoops Basics
Loops Basics
 
Loops in c++ programming language
Loops in c++ programming language Loops in c++ programming language
Loops in c++ programming language
 

En vedette

Lecture7relationalandlogicaloperators 110823181038-phpapp02
Lecture7relationalandlogicaloperators 110823181038-phpapp02Lecture7relationalandlogicaloperators 110823181038-phpapp02
Lecture7relationalandlogicaloperators 110823181038-phpapp02
eShikshak
 
Algorithm chapter 11
Algorithm chapter 11Algorithm chapter 11
Algorithm chapter 11
chidabdu
 
Platform A Mobile July 2008
Platform A Mobile July 2008Platform A Mobile July 2008
Platform A Mobile July 2008
space150
 
Lecture19 unionsin c.ppt
Lecture19 unionsin c.pptLecture19 unionsin c.ppt
Lecture19 unionsin c.ppt
eShikshak
 
Html text and formatting
Html text and formattingHtml text and formatting
Html text and formatting
eShikshak
 

En vedette (20)

03b loops
03b   loops03b   loops
03b loops
 
Html phrase tags
Html phrase tagsHtml phrase tags
Html phrase tags
 
Lecture21 categoriesof userdefinedfunctions.ppt
Lecture21 categoriesof userdefinedfunctions.pptLecture21 categoriesof userdefinedfunctions.ppt
Lecture21 categoriesof userdefinedfunctions.ppt
 
Mesics lecture 5 input – output in ‘c’
Mesics lecture 5   input – output in ‘c’Mesics lecture 5   input – output in ‘c’
Mesics lecture 5 input – output in ‘c’
 
Lecture15 comparisonoftheloopcontrolstructures.ppt
Lecture15 comparisonoftheloopcontrolstructures.pptLecture15 comparisonoftheloopcontrolstructures.ppt
Lecture15 comparisonoftheloopcontrolstructures.ppt
 
Lecture 7 relational_and_logical_operators
Lecture 7 relational_and_logical_operatorsLecture 7 relational_and_logical_operators
Lecture 7 relational_and_logical_operators
 
Mesics lecture 3 c – constants and variables
Mesics lecture 3   c – constants and variablesMesics lecture 3   c – constants and variables
Mesics lecture 3 c – constants and variables
 
Lecture7relationalandlogicaloperators 110823181038-phpapp02
Lecture7relationalandlogicaloperators 110823181038-phpapp02Lecture7relationalandlogicaloperators 110823181038-phpapp02
Lecture7relationalandlogicaloperators 110823181038-phpapp02
 
Mesics lecture files in 'c'
Mesics lecture   files in 'c'Mesics lecture   files in 'c'
Mesics lecture files in 'c'
 
Algorithm
AlgorithmAlgorithm
Algorithm
 
Mesics lecture 8 arrays in 'c'
Mesics lecture 8   arrays in 'c'Mesics lecture 8   arrays in 'c'
Mesics lecture 8 arrays in 'c'
 
Unit 1.3 types of cloud
Unit 1.3 types of cloudUnit 1.3 types of cloud
Unit 1.3 types of cloud
 
Programming note C#
Programming note C#Programming note C#
Programming note C#
 
Unit 1.1 introduction to cloud computing
Unit 1.1   introduction to cloud computingUnit 1.1   introduction to cloud computing
Unit 1.1 introduction to cloud computing
 
Algorithm chapter 11
Algorithm chapter 11Algorithm chapter 11
Algorithm chapter 11
 
Platform A Mobile July 2008
Platform A Mobile July 2008Platform A Mobile July 2008
Platform A Mobile July 2008
 
Lecture19 unionsin c.ppt
Lecture19 unionsin c.pptLecture19 unionsin c.ppt
Lecture19 unionsin c.ppt
 
Unit 1.2 move to cloud computing
Unit 1.2   move to cloud computingUnit 1.2   move to cloud computing
Unit 1.2 move to cloud computing
 
Html text and formatting
Html text and formattingHtml text and formatting
Html text and formatting
 
Linked list
Linked listLinked list
Linked list
 

Similaire à Mesics lecture 7 iteration and repetitive executions

Similaire à Mesics lecture 7 iteration and repetitive executions (20)

Loop and while Loop
Loop and while LoopLoop and while Loop
Loop and while Loop
 
Ch6 Loops
Ch6 LoopsCh6 Loops
Ch6 Loops
 
3. Flow Controls in C (Part II).pdf
3. Flow Controls in C (Part II).pdf3. Flow Controls in C (Part II).pdf
3. Flow Controls in C (Part II).pdf
 
12 lec 12 loop
12 lec 12 loop 12 lec 12 loop
12 lec 12 loop
 
Loops in c language
Loops in c languageLoops in c language
Loops in c language
 
C Programming - Decision making, Looping
C  Programming - Decision making, LoopingC  Programming - Decision making, Looping
C Programming - Decision making, Looping
 
Loops in c
Loops in cLoops in c
Loops in c
 
Programming loop
Programming loopProgramming loop
Programming loop
 
cpu.pdf
cpu.pdfcpu.pdf
cpu.pdf
 
matrices_and_loops.pptx
matrices_and_loops.pptxmatrices_and_loops.pptx
matrices_and_loops.pptx
 
Chapter06.PPT
Chapter06.PPTChapter06.PPT
Chapter06.PPT
 
Session 3
Session 3Session 3
Session 3
 
[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++
 
loops and iteration.docx
loops and iteration.docxloops and iteration.docx
loops and iteration.docx
 
M C6java6
M C6java6M C6java6
M C6java6
 
Loops c++
Loops c++Loops c++
Loops c++
 
Loops
LoopsLoops
Loops
 
Mesics lecture 6 control statement = if -else if__else
Mesics lecture 6   control statement = if -else if__elseMesics lecture 6   control statement = if -else if__else
Mesics lecture 6 control statement = if -else if__else
 
Fundamental of Information Technology - UNIT 8
Fundamental of Information Technology - UNIT 8Fundamental of Information Technology - UNIT 8
Fundamental of Information Technology - UNIT 8
 
Looping
LoopingLooping
Looping
 

Plus de eShikshak

Mesics lecture 4 c operators and experssions
Mesics lecture  4   c operators and experssionsMesics lecture  4   c operators and experssions
Mesics lecture 4 c operators and experssions
eShikshak
 
Mesics lecture 5 input – output in ‘c’
Mesics lecture 5   input – output in ‘c’Mesics lecture 5   input – output in ‘c’
Mesics lecture 5 input – output in ‘c’
eShikshak
 
Lecturer23 pointersin c.ppt
Lecturer23 pointersin c.pptLecturer23 pointersin c.ppt
Lecturer23 pointersin c.ppt
eShikshak
 
Language processors
Language processorsLanguage processors
Language processors
eShikshak
 

Plus de eShikshak (17)

Modelling and evaluation
Modelling and evaluationModelling and evaluation
Modelling and evaluation
 
Operators in python
Operators in pythonOperators in python
Operators in python
 
Datatypes in python
Datatypes in pythonDatatypes in python
Datatypes in python
 
Introduction to python
Introduction to pythonIntroduction to python
Introduction to python
 
Introduction to e commerce
Introduction to e commerceIntroduction to e commerce
Introduction to e commerce
 
Chapeter 2 introduction to cloud computing
Chapeter 2   introduction to cloud computingChapeter 2   introduction to cloud computing
Chapeter 2 introduction to cloud computing
 
Unit 1.4 working of cloud computing
Unit 1.4 working of cloud computingUnit 1.4 working of cloud computing
Unit 1.4 working of cloud computing
 
Mesics lecture 4 c operators and experssions
Mesics lecture  4   c operators and experssionsMesics lecture  4   c operators and experssions
Mesics lecture 4 c operators and experssions
 
Mesics lecture 5 input – output in ‘c’
Mesics lecture 5   input – output in ‘c’Mesics lecture 5   input – output in ‘c’
Mesics lecture 5 input – output in ‘c’
 
Lecture20 user definedfunctions.ppt
Lecture20 user definedfunctions.pptLecture20 user definedfunctions.ppt
Lecture20 user definedfunctions.ppt
 
Lecture18 structurein c.ppt
Lecture18 structurein c.pptLecture18 structurein c.ppt
Lecture18 structurein c.ppt
 
Lecture17 arrays.ppt
Lecture17 arrays.pptLecture17 arrays.ppt
Lecture17 arrays.ppt
 
Lecture13 control statementswitch.ppt
Lecture13 control statementswitch.pptLecture13 control statementswitch.ppt
Lecture13 control statementswitch.ppt
 
Lecturer23 pointersin c.ppt
Lecturer23 pointersin c.pptLecturer23 pointersin c.ppt
Lecturer23 pointersin c.ppt
 
Program development cyle
Program development cyleProgram development cyle
Program development cyle
 
Language processors
Language processorsLanguage processors
Language processors
 
Computer programming programming_langugages
Computer programming programming_langugagesComputer programming programming_langugages
Computer programming programming_langugages
 

Dernier

Breaking Down the Flutterwave Scandal What You Need to Know.pdf
Breaking Down the Flutterwave Scandal What You Need to Know.pdfBreaking Down the Flutterwave Scandal What You Need to Know.pdf
Breaking Down the Flutterwave Scandal What You Need to Know.pdf
UK Journal
 
Tales from a Passkey Provider Progress from Awareness to Implementation.pptx
Tales from a Passkey Provider  Progress from Awareness to Implementation.pptxTales from a Passkey Provider  Progress from Awareness to Implementation.pptx
Tales from a Passkey Provider Progress from Awareness to Implementation.pptx
FIDO Alliance
 
Structuring Teams and Portfolios for Success
Structuring Teams and Portfolios for SuccessStructuring Teams and Portfolios for Success
Structuring Teams and Portfolios for Success
UXDXConf
 
Easier, Faster, and More Powerful – Alles Neu macht der Mai -Wir durchleuchte...
Easier, Faster, and More Powerful – Alles Neu macht der Mai -Wir durchleuchte...Easier, Faster, and More Powerful – Alles Neu macht der Mai -Wir durchleuchte...
Easier, Faster, and More Powerful – Alles Neu macht der Mai -Wir durchleuchte...
panagenda
 
Harnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptx
Harnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptxHarnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptx
Harnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptx
FIDO Alliance
 

Dernier (20)

Portal Kombat : extension du réseau de propagande russe
Portal Kombat : extension du réseau de propagande russePortal Kombat : extension du réseau de propagande russe
Portal Kombat : extension du réseau de propagande russe
 
Breaking Down the Flutterwave Scandal What You Need to Know.pdf
Breaking Down the Flutterwave Scandal What You Need to Know.pdfBreaking Down the Flutterwave Scandal What You Need to Know.pdf
Breaking Down the Flutterwave Scandal What You Need to Know.pdf
 
Design Guidelines for Passkeys 2024.pptx
Design Guidelines for Passkeys 2024.pptxDesign Guidelines for Passkeys 2024.pptx
Design Guidelines for Passkeys 2024.pptx
 
The Value of Certifying Products for FDO _ Paul at FIDO Alliance.pdf
The Value of Certifying Products for FDO _ Paul at FIDO Alliance.pdfThe Value of Certifying Products for FDO _ Paul at FIDO Alliance.pdf
The Value of Certifying Products for FDO _ Paul at FIDO Alliance.pdf
 
Tales from a Passkey Provider Progress from Awareness to Implementation.pptx
Tales from a Passkey Provider  Progress from Awareness to Implementation.pptxTales from a Passkey Provider  Progress from Awareness to Implementation.pptx
Tales from a Passkey Provider Progress from Awareness to Implementation.pptx
 
Structuring Teams and Portfolios for Success
Structuring Teams and Portfolios for SuccessStructuring Teams and Portfolios for Success
Structuring Teams and Portfolios for Success
 
Introduction to FDO and How It works Applications _ Richard at FIDO Alliance.pdf
Introduction to FDO and How It works Applications _ Richard at FIDO Alliance.pdfIntroduction to FDO and How It works Applications _ Richard at FIDO Alliance.pdf
Introduction to FDO and How It works Applications _ Richard at FIDO Alliance.pdf
 
Working together SRE & Platform Engineering
Working together SRE & Platform EngineeringWorking together SRE & Platform Engineering
Working together SRE & Platform Engineering
 
Simplified FDO Manufacturing Flow with TPMs _ Liam at Infineon.pdf
Simplified FDO Manufacturing Flow with TPMs _ Liam at Infineon.pdfSimplified FDO Manufacturing Flow with TPMs _ Liam at Infineon.pdf
Simplified FDO Manufacturing Flow with TPMs _ Liam at Infineon.pdf
 
2024 May Patch Tuesday
2024 May Patch Tuesday2024 May Patch Tuesday
2024 May Patch Tuesday
 
Microsoft CSP Briefing Pre-Engagement - Questionnaire
Microsoft CSP Briefing Pre-Engagement - QuestionnaireMicrosoft CSP Briefing Pre-Engagement - Questionnaire
Microsoft CSP Briefing Pre-Engagement - Questionnaire
 
Collecting & Temporal Analysis of Behavioral Web Data - Tales From The Inside
Collecting & Temporal Analysis of Behavioral Web Data - Tales From The InsideCollecting & Temporal Analysis of Behavioral Web Data - Tales From The Inside
Collecting & Temporal Analysis of Behavioral Web Data - Tales From The Inside
 
Event-Driven Architecture Masterclass: Engineering a Robust, High-performance...
Event-Driven Architecture Masterclass: Engineering a Robust, High-performance...Event-Driven Architecture Masterclass: Engineering a Robust, High-performance...
Event-Driven Architecture Masterclass: Engineering a Robust, High-performance...
 
Long journey of Ruby Standard library at RubyKaigi 2024
Long journey of Ruby Standard library at RubyKaigi 2024Long journey of Ruby Standard library at RubyKaigi 2024
Long journey of Ruby Standard library at RubyKaigi 2024
 
Easier, Faster, and More Powerful – Alles Neu macht der Mai -Wir durchleuchte...
Easier, Faster, and More Powerful – Alles Neu macht der Mai -Wir durchleuchte...Easier, Faster, and More Powerful – Alles Neu macht der Mai -Wir durchleuchte...
Easier, Faster, and More Powerful – Alles Neu macht der Mai -Wir durchleuchte...
 
Where to Learn More About FDO _ Richard at FIDO Alliance.pdf
Where to Learn More About FDO _ Richard at FIDO Alliance.pdfWhere to Learn More About FDO _ Richard at FIDO Alliance.pdf
Where to Learn More About FDO _ Richard at FIDO Alliance.pdf
 
Harnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptx
Harnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptxHarnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptx
Harnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptx
 
FDO for Camera, Sensor and Networking Device – Commercial Solutions from VinC...
FDO for Camera, Sensor and Networking Device – Commercial Solutions from VinC...FDO for Camera, Sensor and Networking Device – Commercial Solutions from VinC...
FDO for Camera, Sensor and Networking Device – Commercial Solutions from VinC...
 
Continuing Bonds Through AI: A Hermeneutic Reflection on Thanabots
Continuing Bonds Through AI: A Hermeneutic Reflection on ThanabotsContinuing Bonds Through AI: A Hermeneutic Reflection on Thanabots
Continuing Bonds Through AI: A Hermeneutic Reflection on Thanabots
 
Choosing the Right FDO Deployment Model for Your Application _ Geoffrey at In...
Choosing the Right FDO Deployment Model for Your Application _ Geoffrey at In...Choosing the Right FDO Deployment Model for Your Application _ Geoffrey at In...
Choosing the Right FDO Deployment Model for Your Application _ Geoffrey at In...
 

Mesics lecture 7 iteration and repetitive executions

  • 1. Iteration and Repetitive Executions www.eshikshak.co.in
  • 2. Introduction • It is a tedious process perform such kind of tasks repeatedly with pen/pencil and paper. • Computer Programming language and packages, the task becomes easy, accurate and fast.
  • 3. What is a “Loop” ? • A loop is defined as a block of statements, which are repeatedly executed for a certain number of times. • The loops are of two types – Counter Controlled Repetition – Sentinel Controlled Repetition
  • 4. Counter Controlled Repetition • It is also called the definite repetition action i.e. the number of iteration to be performed is defined in advance in the program itself. • Steps in this type of loop – Loop Variable • Variable used in the loop – Initialization • It is first step in which starting and final values are assigned to the loop variable. • Each time the value updated is checked by the loop itself. – Increment and Decrement • It is the numerical value added or subtracted to the variable in each round of the loop • The updated value is compared with the final value and it is found less than the final value the steps in the loop are executed
  • 5. Sentinel-Controlled Repetition • It is also called the indefinite repetition action i.e. One cannot estimate how many iteration are to be performed. • In this type, Loop termination happens on the basis of certain condition using decision- making statement.
  • 6. Types of ‘C’ loops for while do-while for(expression 1; expression 2 expression 1; expression 1; ; expression 3) while(expression 2) do { { { statement 1; statement 1; statement 1; statement 2; statement 2; statement 2; . . . statement N; statement N; statement N; } expression 3; } while(expression 3); }
  • 7. for Loop • for loop allows to execute a set of instruction until certain condition is satisfied. • Condition may be predefined or open-ended. for(initialize counter; test condition; re-evaluation parameter) { statement 1; statement 2; . statement N; }
  • 8. for Loop • The initialize counter sets to an initial value. This statement is executed only once. • The test condition is a relational expression that determines the number of iterations desired or determines when to exit from the loop. • The “for” loop continues to execute as long as conditional test is satisfied. • When condition becomes false the control of program exists from the body of the “for” loop and executes next statement after the body of the loop.
  • 9. for Loop • The re-evaluation parameter decides how to make changes int the loop (increment or decrement operations are to be used quite often). • The body of loop may contain either a single statement or multiple statements.
  • 10. for Loop Various formats of ‘for’ loop Syntax Output Remarks for( ; ; ) Infinite loop No Arguments for( a=0; a<=20 ; ) Infinite loop ‘a’ is neither incremented nor decremented for( a=0; a<=10 ; a++) Displays value from 0 to 10 ‘a’ is increment from 0 to 10. printf(“%d”, a); Curly braces are not necessary. Default scope of the for loop is one statement after the for loop. for( a=10; a>=0 ; a--) Displays value from 10 to 0 ‘a’ is decremented from 10 to 0. printf(“%d”, a);
  • 11. Print the first five numbers starting from 1 together with their squares void main() { int I; clrscr(); for(i=1;i<=5;i++) { printf(“Number :%d it’s Square : %d”, i, i * i); } } OUTPUT : Number : 1 it’s Square: 1 Number : 2 it’s Square: 4 Number : 3 it’s Square : 9 Number : 4 it’s Square: 16 Number : 5 it’s Square: 25
  • 12. Print the first five numbers starting from 1 together with their squares void main() { int i; clrscr(); for(i=1;i<=5;i++) printf(“Number :%d it’s Square : %d”, i, i * i); } OUTPUT : Number : 1 it’s Square: 1 Number : 2 it’s Square: 4 Number : 3 it’s Square : 9 Number : 4 it’s Square: 16 Number : 5 it’s Square: 25
  • 13. Print the first five numbers starting from 1 together with their squares void main() { int i=1; clrscr(); for(;i<=5;i++) printf(“Number :%d it’s Square : %d”, i, i * i); } OUTPUT : Number : 1 it’s Square: 1 Number : 2 it’s Square: 4 Number : 3 it’s Square : 9 Number : 4 it’s Square: 16 Number : 5 it’s Square: 25
  • 14. Print the first five numbers starting from 1 together with their squares void main() { int i=1; clrscr(); for(;i<=5;) { printf(“Number :%d it’s Square : %d”, i, i * i); i++; } } OUTPUT : Number : 1 it’s Square: 1 Number : 2 it’s Square: 4 Number : 3 it’s Square : 9 Number : 4 it’s Square: 16 Number : 5 it’s Square: 25
  • 15. Print the first five numbers starting from 1 together with their squares void main() { int I; clrscr(); for(i=1;i<=5;i++); { printf(“Number :%d it’s Square : %d”, i, i * i); } printf(“Number :%d it’s Square : %d”, i, i * i); getch(); }
  • 16. Nested for loops • We can use loop inside loop, which is known as nested loop • In nested for loops one or more for statements are included in the body of the loop. • The numbers of iterations in this type of structure will be equal to the number of iteration in the outer loop multiplied by the number of iterations in the inner loop.
  • 17. Print the first five numbers starting from 1 together with their squares void main() { int a, b, sub; clrscr(); for(a=3;a>=1;a--); { for(b=1; b<=2; b++) { sub = a – b; printf(“a = %d b = %d a – b = %dn”, a, b, sub); } } getch(); } Output: a=3b=1a–b=2 a=3b=2a–b=1 a=2b=1a–b=1 a=2b=2a–b=0 a=2b=2a–b=0 a=1b=1a–b=0 a = 1 b = 1 a – b = -1
  • 18. while loop • The while loop is frequently used in programs for the repeated execution of statement in a loop. • The loop continues until a certain condition is satisfied.
  • 19. while loop The syntax of while loop while(test-condition) { body of the loop; }
  • 20. while loop • The test condition is indicated at the top and its tests the value of the expression before processing the body of the loop. • The loop statements will be executed till the condition is true. • When the condition becomes false the execution will be out of the loop. • The braces are needed only if the body of the loop contains more than one statement. • However, it is a good practice to use braces even if the body of the loop contains only one statement.
  • 21. • Steps of while loops are as Entry follows – The test condition is Initialize evaluated and if it is true, the body of the loop is executed. – On execution of the body, F Test test condition is repetitively Condition checked and if it is true the body is executed. Stop T – The process of execution of the body will be continued till Body of the loop the test condition becomes true. – The control is transferred out Update of the loop if test condition fails.
  • 22. Print “You are in MESICS” string for nine times OUTPUT : You are in MESICS You are in MESICS void main() You are in MESICS { You are in MESICS You are in MESICS int i=1; You are in MESICS clrscr(); You are in MESICS You are in MESICS while(i<=9) You are in MESICS { printf(“You are in MESICSn”); i++; } }
  • 23. do…while loop The syntax of do…while loop do { statements; } while(test-condition);
  • 24. do…while loop • The difference between the while and do-while loop is the place where the condition is to be tested. • In the while loops the condition is tested following the while statement, and then the body gets executed. • In the do…while the condition is checked at the end of the loop. • The do…while loop will execute at least one time even if the condition is false initially. • The do…while loop executes until the condition becomes false.
  • 25. Comparison of while and do..while Sr. No while loop do…while loop Condition is specified at the Condition is mentioned at the 1. top. bottom. Body statement/s is/are Body statement/s executes even 2. executed when condition is when the condition is false. satisfied. No brackets for a single Brackets are essential even when a 3. statement. single statement exists. 4. It is an entry-controlled loop. It is an exit-controlled loop.
  • 26. Print “You are in MESICS” string for nine times OUTPUT : You are in MESICS You are in MESICS void main() You are in MESICS { You are in MESICS You are in MESICS int i=1; clrscr(); do { printf(“You are in MESICSn”); i++; } while(i<=5); }
  • 27. Sr. No. Program 1.