SlideShare una empresa de Scribd logo
1 de 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.

Más contenido relacionado

La actualidad más candente (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
 

Destacado

Html phrase tags
Html phrase tagsHtml phrase tags
Html phrase tagseShikshak
 
Lecture21 categoriesof userdefinedfunctions.ppt
Lecture21 categoriesof userdefinedfunctions.pptLecture21 categoriesof userdefinedfunctions.ppt
Lecture21 categoriesof userdefinedfunctions.ppteShikshak
 
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
 
Lecture15 comparisonoftheloopcontrolstructures.ppt
Lecture15 comparisonoftheloopcontrolstructures.pptLecture15 comparisonoftheloopcontrolstructures.ppt
Lecture15 comparisonoftheloopcontrolstructures.ppteShikshak
 
Lecture 7 relational_and_logical_operators
Lecture 7 relational_and_logical_operatorsLecture 7 relational_and_logical_operators
Lecture 7 relational_and_logical_operatorseShikshak
 
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 variableseShikshak
 
Lecture7relationalandlogicaloperators 110823181038-phpapp02
Lecture7relationalandlogicaloperators 110823181038-phpapp02Lecture7relationalandlogicaloperators 110823181038-phpapp02
Lecture7relationalandlogicaloperators 110823181038-phpapp02eShikshak
 
Mesics lecture files in 'c'
Mesics lecture   files in 'c'Mesics lecture   files in 'c'
Mesics lecture files in 'c'eShikshak
 
Mesics lecture 8 arrays in 'c'
Mesics lecture 8   arrays in 'c'Mesics lecture 8   arrays in 'c'
Mesics lecture 8 arrays in 'c'eShikshak
 
Unit 1.3 types of cloud
Unit 1.3 types of cloudUnit 1.3 types of cloud
Unit 1.3 types of cloudeShikshak
 
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 computingeShikshak
 
Algorithm chapter 11
Algorithm chapter 11Algorithm chapter 11
Algorithm chapter 11chidabdu
 
Platform A Mobile July 2008
Platform A Mobile July 2008Platform A Mobile July 2008
Platform A Mobile July 2008space150
 
Lecture19 unionsin c.ppt
Lecture19 unionsin c.pptLecture19 unionsin c.ppt
Lecture19 unionsin c.ppteShikshak
 
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 computingeShikshak
 
Html text and formatting
Html text and formattingHtml text and formatting
Html text and formattingeShikshak
 

Destacado (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
 

Similar a Mesics lecture 7 iteration and repetitive executions

Similar a 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
 

Más de eShikshak

Modelling and evaluation
Modelling and evaluationModelling and evaluation
Modelling and evaluationeShikshak
 
Operators in python
Operators in pythonOperators in python
Operators in pythoneShikshak
 
Datatypes in python
Datatypes in pythonDatatypes in python
Datatypes in pythoneShikshak
 
Introduction to python
Introduction to pythonIntroduction to python
Introduction to pythoneShikshak
 
Introduction to e commerce
Introduction to e commerceIntroduction to e commerce
Introduction to e commerceeShikshak
 
Chapeter 2 introduction to cloud computing
Chapeter 2   introduction to cloud computingChapeter 2   introduction to cloud computing
Chapeter 2 introduction to cloud computingeShikshak
 
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 computingeShikshak
 
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 experssionseShikshak
 
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
 
Lecture20 user definedfunctions.ppt
Lecture20 user definedfunctions.pptLecture20 user definedfunctions.ppt
Lecture20 user definedfunctions.ppteShikshak
 
Lecture18 structurein c.ppt
Lecture18 structurein c.pptLecture18 structurein c.ppt
Lecture18 structurein c.ppteShikshak
 
Lecture17 arrays.ppt
Lecture17 arrays.pptLecture17 arrays.ppt
Lecture17 arrays.ppteShikshak
 
Lecture13 control statementswitch.ppt
Lecture13 control statementswitch.pptLecture13 control statementswitch.ppt
Lecture13 control statementswitch.ppteShikshak
 
Lecturer23 pointersin c.ppt
Lecturer23 pointersin c.pptLecturer23 pointersin c.ppt
Lecturer23 pointersin c.ppteShikshak
 
Program development cyle
Program development cyleProgram development cyle
Program development cyleeShikshak
 
Language processors
Language processorsLanguage processors
Language processorseShikshak
 
Computer programming programming_langugages
Computer programming programming_langugagesComputer programming programming_langugages
Computer programming programming_langugageseShikshak
 

Más 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
 

Último

The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr LapshynFwdays
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraDeakin University
 
APIForce Zurich 5 April Automation LPDG
APIForce Zurich 5 April  Automation LPDGAPIForce Zurich 5 April  Automation LPDG
APIForce Zurich 5 April Automation LPDGMarianaLemus7
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 

Último (20)

The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptxVulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping Elbows
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning era
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
APIForce Zurich 5 April Automation LPDG
APIForce Zurich 5 April  Automation LPDGAPIForce Zurich 5 April  Automation LPDG
APIForce Zurich 5 April Automation LPDG
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 

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.