PRESANTATION ON
“Looping control structure”
GUPTA HARSH
13ME030
Introduction of 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 a the given condition. If it did,
the loop stops. If not, the loop moves on to
the next iteration.
CONTINUE…
• Loop: a control structure that repeats a group
of steps in a program
• C loop control statements
• Two types of control statement….. They are:-
Exit control loop and
Entry control loop
Exit Control loop
• In exit control loop, the
body is executed first and
then the condition is
checked.
• In exit control loop the
body is executed
at least once.
Entry Control loop
• In entry control loop,
the condition is checked
first and then body is
executed.
• In Entry control loop
the body is not
executed at all if the
condition is false.
Looping…
• ‘C’ language provides following looping
structures:-
1. WHILE
2. DO-WHILE
3. FOR
WHILE LOOP
– Of the three different loop structures offered in C, while-
loops are conceptually the simplest.
– Resembles if-statement, but does not have an else.
– Conceptually, keep executing the loop body again and
again till the condition is true.
– Loop terminates when the loop condition becomes false.
– Note that the truth of the loop condition is not checked
constantly between every statement in the loop body, but
only at the beginning of the loop, and then again between
the repetitions of the loop body
– Condition may be temporarily false during the loop, but
become true again before it is checked
DO-WHILE LOOP
– The second type of loop offered in C is the do-while.
– Clearly the least commonly used of the three: one famous
study of real world programs revealed that out of all loops
in them, only 1% are do-while.
– Do-while behaves exactly the same way as the while-loop,
but is guaranteed to execute its body at least once before
it starts looking at the loop condition.
– The possibility of executing the loop body zero times
does not exist, even if the condition is initially false
– do-while is most useful in situations where testing
the condition simply does not make any sense until
the loop body has been executed once
FOR LOOP
– The third type of loop structure in C is suitable for
the common task of iterating through the values of
a range of integers, one value at the time.
– For example, go through the numbers from 1 to 100.
– The for-loop header defines this range, and the loop
body contains the statements you execute for each
value.
– To define an integer range, you need to define three
things: where it begins (1), where it ends (100), and
the step size between the consecutive values of the
range (1)
CONTINUE…
– It is not a coincidence that the header of a for-loop
consists of three parts, for the three things that
define a range.
– We can still use while like in countdown, but using a
for-loop makes it clear that we are going through a
range.
– It is not a coincidence that the header of a for-loop
consists of three parts, for the three things that
define a range.
– We can still use while like in countdown, but using a
for-loop makes it clear that we are going through a
range.
Flowchart of FOR LOOP
for( initializing list;condition; updation)
BREAK STATEMENT
• Remember the break keyword that we used
to stop a switch statement from executing
more than one statement?
• break can also be used to exit an infinite
loop
• But it is almost always best to use a well-
written while loop.
CONTINUE STATEMENT
– Continue is a statement similar to break, but
instead of jumping out of the loop altogether, it
jumps to the next round of the loop, skipping the
rest of the body
– Most commonly used to skip some of the values
inside the range for which the loop doesn't need
to do anything
Introduction to loops  cpu

Introduction to loops cpu

  • 1.
    PRESANTATION ON “Looping controlstructure” GUPTA HARSH 13ME030
  • 2.
    Introduction of 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 a the given condition. If it did, the loop stops. If not, the loop moves on to the next iteration.
  • 3.
    CONTINUE… • Loop: acontrol structure that repeats a group of steps in a program • C loop control statements • Two types of control statement….. They are:- Exit control loop and Entry control loop
  • 4.
    Exit Control loop •In exit control loop, the body is executed first and then the condition is checked. • In exit control loop the body is executed at least once.
  • 5.
    Entry Control loop •In entry control loop, the condition is checked first and then body is executed. • In Entry control loop the body is not executed at all if the condition is false.
  • 6.
    Looping… • ‘C’ languageprovides following looping structures:- 1. WHILE 2. DO-WHILE 3. FOR
  • 7.
    WHILE LOOP – Ofthe three different loop structures offered in C, while- loops are conceptually the simplest. – Resembles if-statement, but does not have an else. – Conceptually, keep executing the loop body again and again till the condition is true. – Loop terminates when the loop condition becomes false. – Note that the truth of the loop condition is not checked constantly between every statement in the loop body, but only at the beginning of the loop, and then again between the repetitions of the loop body – Condition may be temporarily false during the loop, but become true again before it is checked
  • 9.
    DO-WHILE LOOP – Thesecond type of loop offered in C is the do-while. – Clearly the least commonly used of the three: one famous study of real world programs revealed that out of all loops in them, only 1% are do-while. – Do-while behaves exactly the same way as the while-loop, but is guaranteed to execute its body at least once before it starts looking at the loop condition. – The possibility of executing the loop body zero times does not exist, even if the condition is initially false – do-while is most useful in situations where testing the condition simply does not make any sense until the loop body has been executed once
  • 11.
    FOR LOOP – Thethird type of loop structure in C is suitable for the common task of iterating through the values of a range of integers, one value at the time. – For example, go through the numbers from 1 to 100. – The for-loop header defines this range, and the loop body contains the statements you execute for each value. – To define an integer range, you need to define three things: where it begins (1), where it ends (100), and the step size between the consecutive values of the range (1)
  • 12.
    CONTINUE… – It isnot a coincidence that the header of a for-loop consists of three parts, for the three things that define a range. – We can still use while like in countdown, but using a for-loop makes it clear that we are going through a range. – It is not a coincidence that the header of a for-loop consists of three parts, for the three things that define a range. – We can still use while like in countdown, but using a for-loop makes it clear that we are going through a range.
  • 13.
    Flowchart of FORLOOP for( initializing list;condition; updation)
  • 14.
    BREAK STATEMENT • Rememberthe break keyword that we used to stop a switch statement from executing more than one statement? • break can also be used to exit an infinite loop • But it is almost always best to use a well- written while loop.
  • 15.
    CONTINUE STATEMENT – Continueis a statement similar to break, but instead of jumping out of the loop altogether, it jumps to the next round of the loop, skipping the rest of the body – Most commonly used to skip some of the values inside the range for which the loop doesn't need to do anything