0% found this document useful (0 votes)
19 views

L4-Control Statement in C - Part 2.ppt

C lecture pdf 4

Uploaded by

Ishan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

L4-Control Statement in C - Part 2.ppt

C lecture pdf 4

Uploaded by

Ishan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 32

C Programming

Module-4: Control Statements in C

IMT and IMG 1st Year

Instructor : Dr. Santosh Singh Rathore


ABV-IIITM Gwalior
Disclaimer

This is NOT A COPYRIGHT MATERIAL

Content for the slides have been taken from the various sources and
from the reference books.

Students need to follow reference books to get the thorough details


of the concepts discussed in slides
Loops in C
• Loops are used when we want to execute a part of the program or a block of
statements several times.
• Suppose we want to print “C is the best" 10 times.
• One -way to get the desired output, we write 10 printf statements, which is not
preferable.
• Other way out is to use loop.
• Using loop we can write one loop statement and only one printf statement, and
this approach is definitely better the first one.
• With the help of loop we can execute a part of the program repeatedly till
some condition is true.
• There are three loop statements in C-
• while
• do while
• for
While Loop
• We want to do something a fixed number of times. Perhaps you want to
calculate gross salaries of ten different persons, or you want to convert
temperatures from centigrade to fahrenheit for 15 different cities.
• The While loop is suited in such cases.
While Loop
While Loop
• Like if-else statement here also we can have either a single statement or a
block of statements, a here it is known as the body of loop.
While Loop
While Loop
While Loop
Do…While Loop
• The 'do...while' statement is also used for looping. The body of this loop may
contain a single statement or a block of statements.
Do…While Loop
Do…While Loop
For Loop
• The 'for' statement is very useful while programming in C. It has three
expressions and semicolons are used for separating these expressions.

• expressionl is an initialization expression, expression2 is a test expression or condition


and expression3 is an update expression.
• expression1 is executed only once when the loop starts and is used to initialize the loop
variables. This expression is generally an assignment expression.
• expression2 is a condition and is tested before each iteration of the loop. This condition
generally uses relational and logical operators.
• expression3 is an update expression and is executed each time after the body of the loop is
executed.
For Loop
For Loop
For Loop
For Loop
• All the three expressions of the for
loop are optional.
• We can omit anyone or all the three
expressions in the for loop but in any
case the two separating semicolons
should always be present.
• expression1 is omitted when the
initialization work is done before
entering the loop.
expression2 is a condition and if
omitted, it is always assumed to be
true and so. this type of loop will
never stop executing.
• This type of loop is infinite and to
avoid it there should be a statement
inside the loop that takes the control
out of the loop
For Loop
• We can also have any number of expressions separated by commas.
• For example, we may want to initialize more than one variable or take more than one
variable as loop variable.
Nesting of Loops
• When a loop is written inside the body of another loop, then it is known as nesting of
loops.
• Any type of loop can be nested inside any other type of loop. For example a for loop may
be nested inside another for loop or inside a while or do while loop.

Output:
i=1 j=l j=2 j=3 j=4 i=2 j=l j=2 J=3 j=4 i=3 j = 1 j = 2 j = 3 j = 4
Nesting of Loops
Infinite loop
• The loops that go on executing infinitely and never terminate are called infinite loops.
Sometimes we write these loops by mistake while sometimes we deliberately make use
of these loops in our programs.
Infinite loop
• A common mistake made by beginners is to use the assignment operator(=)
where equality operator(==) should have been used.
• If this mistake is made in the loop condition then it may cause the loop to
execute infinitely. For example consider this loop:
while(n=2)
{
……
}
• Here we wanted the loop to execute till the value of n is equal to 2. So we
should have written n= =2 but mistakenly we have written n = 2.
• Now n = 2 is an assignment expression and the value of this expression is 2,
which is a nonzero(true) value and hence the loop condition is always true.
Infinite loop
Break Statement
• break statement is used inside ,loops and switch statements. Sometimes it becomes
necessary to Coming out of the loop even before the loop condition becomes false.
• In such a situation, break statement is used to terminate the loop.
• This statement causes an immediate exit from that loop in which this statement appears.
Break Statement
Break Statement
continue statement
• The continue statement is used when we want to go to the next iteration of the
loop after skipping some statements of the loop.
• This continue statement can be written simply as continue;
• It is generally used with a condition. When continue statement is encountered
all the remaining statements in the current iteration are not executed and the
loop continues with the next iteration.
• The difference between break and continue is that when break is. encountered
the loop terminates and the control is transferred to the next statement
following the loop, but when a continue statement is encountered the loop is
not terminated and the control is transferred to the beginning of the loop.
• In while and do-while loops, after continue statement the control. is transferred
to the test condition and then the loop continues, whereas in for loop after
continue statement the control is transferred to update expression and then the
condition is tested.
continue statement
continue statement
goto statement
goto statement
Thank you for your attention!

< Questions?

You might also like