0% found this document useful (0 votes)
23 views18 pages

Unit3 Chapter1

Cs

Uploaded by

Mishitha Ravoof
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)
23 views18 pages

Unit3 Chapter1

Cs

Uploaded by

Mishitha Ravoof
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/ 18

UNIT 3

CHAPTER 1
Decision making and looping
You may encounter situations, when a block of code needs to be
executed several number of times.
Advantage with looping statement
• Reduce length of Code
• Take less memory space.
• Burden on the developer is reducing.
• Time consuming process to execute the program is reduced.
• There are three type of Loops available in 'C' programming language.
1. while loop
2. do..while
3. for loop

There are mainly two types of loops:


1. Entry Controlled loops: In this type of loops the test condition is
tested before entering the loop body. For Loop and While Loop are
entry controlled loops.
2. Exit Controlled Loops: In this type of loops the test condition is
tested or evaluated at the end of loop body. Therefore, the loop
body will execute atleast once, irrespective of whether the test
condition is true or false. do – while loop is exit controlled loop.
While Loop
The syntax of a while loop is:
while (testExpression)
{
Statement block
}
The while loop evaluates the test expression. If the test expression is
true (nonzero), codes inside the body of while loop are executed. The
test expression is evaluated again. The process goes on until the test
expression is false. .When the test expression is false, the while loop is
terminated. While loop is entry controlled loop.
/* Programm to print numbers from 1 to 9*/:
#include <stdio.h>
Void main()
{
int i=1;
while(i<10)
{
printf("%d\n",i);
i++;
}
}
Do ..while loop
• do...while loop in C programming checks its condition at the bottom of the
loop.
• A do...while loop is similar to a while loop, except the fact that it is
guaranteed to execute at least one time.
Syntax :
do
{
statement(s);
}
while(condition );
if the condition is true, the flow of control jumps back up to do, and the
statement(s) in the loop executes again. This process repeats until the given
condition becomes false.
Example
Void main ()
{
int a = 10;
/* do loop execution */
do
{
printf(" %d\n", a);
a = a + 1;
}
while( a < 20 );
}
for loop
• A for loop is a repetition control structure that allows you to
efficiently write a loop that needs to execute a specific number of
times.
• Syntax
for ( init_exp; test_exp; update_exp )
{
statement(s);
}
• The init_exp step is executed first, and only once. This step allows you
to initialize loop control variables.
• Next, the test_exp is evaluated. If it is true, the body of the loop is
executed. If it is false, the body of the loop does not execute and the
flow of control jumps to the next statement just after the 'for' loop.
• After the body of the 'for' loop executes, the flow of control jumps
back up to the update_exp.
• This statement allows you to update any loop control variable .
• The condition is now evaluated again. If it is true, the loop executes
and the process repeats itself. After the condition becomes false, the
'for' loop terminates
Example
#include <stdio.h>
Void main ()
{
int i;
/* for loop execution */
for( i = 1;i < 10;i++ )
{
printf("%d\n",,i);
}
}
Omitting expressions in for loop
#include<stdio.h>
main()
{
int i = 1, sum = 0;
/*expression 1 is omitted*/
for( ; i < 10 ; i++)
{
sum =sum+ i;
}
printf("Sum = %d", sum);
}
Example 2:
2nd variation: Where expression2 is omitted
#include<stdio.h>
main()
{
int i, sum = 0;
for(i = 1 ; ; i++)
{
if(i > 1000)
{
break;
}
sum =sum+ i;
}
printf("Sum = %d", sum);
}
Example 3:
3rd variation: Where expression3 is omitted
#include<stdio.h>
main()
{
int i, sum = 0;
for(i = 1 ; i < 10; )
{
sum += i;
i++; /* update expression*/
}
printf("Sum = %d", sum);
}
Nesting of Loops
• C programming allows to use one loop inside another loop.
for ( init_exp; test_exp; update_exp )
{
for(init_exp; test_exp; update_exp)
{
statement(s);
}
statement(s);
}
The Infinite Loop
• A loop becomes an infinite loop if a condition never becomes false.
Loop will not terminate
int i = 1
while(i<10)
{
printf("%d\n", i);
}
Here we are not updating the value of i. So after each iteration value of
i remains same. As a result, the condition (i<10) will always be true. For
the loop to work correctly add i++
break Statement
• The break statement terminates the loop (for, while and do...while
loop) immediately when it is encountered.
Void main ()
{
int a = 1 ;
/* while loop execution */
while( a< 10 )
{
printf("value of a: %d\n", a);
a++;
if( a> 5)
{
/* terminate the loop using break statement */
break;
}
}
continue Statement
• The continue statement skips some statements inside the loop and
goes back to beginning of loop for executing next iteration..

You might also like