0% found this document useful (0 votes)
42 views17 pages

Decision Making&Looping

The document discusses loops and decision making in C programming. It describes two types of loops: entry-controlled loops like for and while loops, where the condition is checked before the loop body, and exit-controlled loops like do-while loops, where the condition is checked after the loop body. The key components of a loop are initializing a counter, executing the body, testing the condition, and incrementing the counter. The three common loop constructs - while, do-while, and for loops - are explained with examples.

Uploaded by

Rishik Puneet m
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)
42 views17 pages

Decision Making&Looping

The document discusses loops and decision making in C programming. It describes two types of loops: entry-controlled loops like for and while loops, where the condition is checked before the loop body, and exit-controlled loops like do-while loops, where the condition is checked after the loop body. The key components of a loop are initializing a counter, executing the body, testing the condition, and incrementing the counter. The three common loop constructs - while, do-while, and for loops - are explained with examples.

Uploaded by

Rishik Puneet m
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/ 17

Introduction to C programming (ESC05) Decisions Making & Looping

Decisions Making and Looping


INTRODUCTION:

 Loops are required whenever a set of statements must be executed a number of times.
 Looping helps programmers to develop programs containing iterative or repetitive steps
(processes) which otherwise would require thousands of statements to be executed.
 A looping structure is one where a sequence of statements is executed repeatedly until some
condition for termination of the loop is satisfied.
 Thus, a loop consists of two parts: -
o Body of the loop
o The control statement
The control statement checks the given condition and then executes the set of statements in the
body or domain of the loop.
 Control structure can be classified into 2 categories depending on the position of the control
statement.
o Entry controlled loops.
o Exit-controlled loops.
 The loops in which the control conditions are tested before looping are called Entry Controlled
loops. Eg: for loop, while- loop.

Flowchart for Entry-controlled Loop: -

Entry

n False
Check Cond

True

Body of the Loop

Next Statement
 In such loops, the body (domain) of the loop will be executed only when the condition is true,
other wise the body of the loop will not be executed and control is transferred to the next
statement outside the domain or body of the looping structure.
 The loops in which the control conditions are tested at the end of the loop are known as Exit-
Controlled loops.

Dr. Bharathi P. T Department of CS&E


Introduction to C programming (ESC05) Decisions Making & Looping

Flowchart for Exit-controlled Loop: -

Entry

Body of the Loop

True False
Test
Condition

Next Statement

 In this case, the body of the loop will be executed at least once since the condition is tested at
the end of the loop. Thus the looping statements will be executed repeatedly until the control
condition becomes false.
 The test condition stated in the looping structure is very important. If the test condition never
becomes false, an infinite loop is set up and the statements within the domain are executed
repeatedly. There fore, care must be taken to avoid infinite loops.
In general, a looping structure consists of the following: -
1. Initializing the counter.
2. Executing the statements in the body of the loop.
3. Testing the condition in the control statement.
 Testing can be either to match the given condition of to determine whether the loop
has been executed the specified no. of times.
4. Incrementing the counter.
The three types of looping constructs available are: -
a. while() loop
b. do-while() loop
c. for() loop

a. while() loop: -

Dr. Bharathi P. T Department of CS&E


Introduction to C programming (ESC05) Decisions Making & Looping

It’s an entry-controlled looping structure.


General Format:-

while(test-expression)
{
Body of the loop;
}
Next statement;

 1st test expression/condition checked is true, the body of the loop is executed and control is
transferred to the test expression.
 When condition or test expression is false, the control is transferred to the next-statement
outside the loop.

Eg: - Program to generate 1st N natural numbers:

#include<stdio.h>
#include<conio.h>
void main()
{
int i, n;
i=1; /*Initialization*/
printf(“Enter the value of N : \n”);
scanf(“%d”, &n);
while(i<=n)
{
printf(“%d\n”, i);
i++; /*Incrementation*/
}
getch();
}

Sample output: -

Enter the value of N: 10


1 2 3 4 5 6 7 8 9 10

Dr. Bharathi P. T Department of CS&E


Introduction to C programming (ESC05) Decisions Making & Looping

b. Do-while() statement: -

It’s an exit-controlled looping structure.


General Syntax:

do
{
Body of loop
}
while (test-condition);

First the body of the loop is executed once and then the condition, if the condition is true; control is
transferred back to the beginning of the loop. It will continue until the given condition becomes false.
When the condition becomes false, the control is transferred to the next-statement outside the loop.
In the do-while loop, the body of the loop gets executed at least once. Since the condition is checked at
the end [exit part of the loop].

Eg: -

Program to validate your Input: -

#include<stdio.h>
void main()
{
int num;
do
{
printf (“Enter the positive number: \n”);
scanf (“%d”,&num);
}
while (num<=0);
}

c. The for() statement: -

One of the most popular looping structure in C is the for statement. It is an entry-controlled loop.

General form: - False

for (initialization; test-condition; increment)


{
True
Body of the loop
}
Next-statement;

Dr. Bharathi P. T Department of CS&E


Introduction to C programming (ESC05) Decisions Making & Looping

1st expression:
Initialization: It’s an assignment expression used to initialize the loop index or counter.
Eg: i=0, j=0, here, i and j are called loop control variables.

2nd expression is the test condition; it is checked. The control is entered into the loop only if the
condition is true; otherwise control is transferred to the next-statement outside the loop.

3rd expression: When the condition is true, the statements in the loop are executed and control is
transferred to the beginning of the loop to evaluate the 3rd expression. It is usually an assignment
statement used to increment or decrement the loop index.

The new value of the loop index is tested once again in the 2nd expression. If the condition is true, the
statements in the loop are executed. This process is repeated until the test condition becomes false.

Eg; - for(i=1;i<=10;i++)
printf(“%d\n”, i);

Output:
1
2
3
4
5
6
7
8
9
10

Eg: for (i=10;i>0;i-- --)


printf(“/n%d”, i);

Output:
10
9
8
7
6
5
4
3
2
1

Note:

Dr. Bharathi P. T Department of CS&E


Introduction to C programming (ESC05) Decisions Making & Looping

1. “;” should not be placed after the 3rd expression.


2. “;” is used to separate the 3 expressions in for statement.
3. Any one or all the expressions may be absent within for. i.e.., for( ; ; ) is a valid statement.

Eg: - for (i=3 ; ;)


printf (“%d\n”, i);
is same as
i=3;
for(;;)
printf(“%d\n”, );
Since there is no test condition, they form infinite loop.
4. The advantage of for(;;) loop is that all the 3 actions of initializing, checking and incrementing of
the loop index is done in the for(;;) statement itself.

Comparison of the 3 loops:

for(;;) while() do-while()

for(i=1;i<=10;i++) i=1; i=1;


{ while(i<=10) do
Statements; { {
----- Statements; Statements;
----- ---- -------
} ---- -------
i++; i++;
} }
while(i<=10);

Comma operator in for(;;)loop: -

1. It’s a binary operator that evaluates its 1st operand and discards the result and it then evaluates
the second operand and returns its value or type.
2. It has got the lowest precedence in C operators.
3. It also acts as a sequence point. i.e.., Comma acts as a separator as well as the operator.
4. a. Comma acts as separator whenever you declare variables in the declaration section.
i.e..,
void main()

Dr. Bharathi P. T Department of CS&E


Introduction to C programming (ESC05) Decisions Making & Looping

{
int i=0,j=1;
.
.
}

Eg : for(i=0,j=0; … i++,j++)

b. In for statement, we can initialize more than one variable at a time using comma operator.
In this section, it acts as a separator.
Eg:

X=2;
for(i=1;i<=10;i++)
{
.
.
.
X++;
}

The above code is same as:

for(x=2,i=1;i<=10;x++,i++)

Also, when used in the 3rd section, comma acts as a separator. i.e.., In 1st and 3rd section separator.

5. a) In the test-condition, we can have compound condition statement containing the control
variable and any other external variable.

Eg:
sum=0;
for(i=1; i<=10&&sum<=100; i++)
{
sum = sum + I;
printf(“\n%d%d”, i , sum);
}

b) But when we use comma in the 2nd section of the for loop i.e.., test expression section, it acts
as an operator.

for(i=0,j=0;i<10,j!=0;i++,j++)
{
printf(“%d%d”, i, j);
}

Dr. Bharathi P. T Department of CS&E


Introduction to C programming (ESC05) Decisions Making & Looping

The above code will not print any values because even though the condition (i<10) is true, but
(j!=0) is false.

a. In for statement,
i.e.., for(..; T, F;….) Since comma acts as an operator, it returns false value. Because, comma
implicitly acts as an operator.

i.e.., its return value will always be the right operand value or type.

Eg:
#include<stdio.h>
void main()
{
.
.
.
for (i=0, j=0; i!=0, j<10; i++, j++)

Will give the answer 0 0


. .
. .
10 10

b. In general, comma operator will always return its right operand value.
Suppose, you have, i, j as 2 variables.
void main()
{
int i, j, k;
printf(“Enter values of ‘i’ and ‘j’ “);
scanf(“%d%d”, &i, &j);
k=(i,j);
printf(“%d\n”, k);
}

Output : K value will be of ‘j’.

6. for(;;) loop can be used as delay loop using the null statement.
Eg: - for(x=1;x<=1000;x++);

This loop causes a time delay since it is repeated 1000 times without executing any
statements. This statement is known as null statement.(wherein we have semicolon for the
for statement)

Dr. Bharathi P. T Department of CS&E


Introduction to C programming (ESC05) Decisions Making & Looping

C compiler will not give any error message and we should avoid such kind of statements in
C.

Examples of Comma operator:-

#include<stdio.h>
void main()
{
int i, j, k, l;
printf(“Enter the values of i j and k : \n”);
scanf(“%d%d”,&i, & j,&k);
l=(i,j,k);
printf(“%d”,k);
}

Output:-
Enter the values of i, j and k : 12 14 20
20

In any program: -

1. int a=10, b=20, c=30; it’s a separator.


2. i=(a, b);  stores b to i.(operator).
3. i=a, b  stores a to i. = higher priority.
4. i=(a+=2, a+b);  adds 2 to a, then a+b result stored in i
5. i=a, b, c; stores a to i.
6. i=(a, b, c)  stores c to i.

Program to check whether a given number is prime or not: -

#include<stdio.h>
void main()
{
int a, n;
printf(“Enter a positive number : \n”);
scanf(“%d”, &n);
if(n<=1)
{
printf(“%d is not prime\n”, n);
getch();
exit();

Dr. Bharathi P. T Department of CS&E


Introduction to C programming (ESC05) Decisions Making & Looping

}
for(a=2;a<=n/2;a++)
{
if(n%i==0)
{
printf(“%d is not a prime number \n”, n);
getch();
exit();
}
}
printf(“%d is prime \n”, n);
getch();
}

Nested Loops:-

1. When a for() statement is enclosed in the domain of another for() statement, it is called as
nested.
2. Inner loop is executed for every index value of the outer loop.

i.e.., Eg:
printf(“i j”);
for(i=1;i<=5;i++)
{
for(j=0;j<=9;j++)
{
printf(“%d\n%d”,i, j);
}
}

Output: -

i j

1 0
1 1
1 2
. .
. .
1 9

2 0
2 1

Dr. Bharathi P. T Department of CS&E


Introduction to C programming (ESC05) Decisions Making & Looping

2 2
. .
. .
2 9

3 0
3 1
3 2
. .
. .
. .
3 9

4 0
4 1
4 2
. .
. .
. .
4 9

5 0
5 1
5 2
. .
.
. .
5 9

Break statement: -

 Not only used in switch case but also in while, do-while and for loops.
 It is used to exit from a switch statement or a loop.
 It cannot exit from nested loops, since it can exit only from the loop containing it.

General Form:

break;

Dr. Bharathi P. T Department of CS&E


Introduction to C programming (ESC05) Decisions Making & Looping

Eg: -

while(condition)
{
if(condition)
break;
….. (Exit from loop)
….
}

do
{
….
….
….
if(condition)
break;
…. (Exit from loop)
….
….
}
while(condition-1);

for(;;)
{
for(;;)
{
….

….
if(condition)
break;
….
…. (Exit from inner loop)
}
….
}

Dr. Bharathi P. T Department of CS&E


Introduction to C programming (ESC05) Decisions Making & Looping

Continue statement: -

 Used to bypass the rest of the statements in a loop.


 The loop does not terminate when a continue statement is encountered.
 General form: -
continue;

 It can be used within a for(), while() or a do-while() loop.


 It transfers control to the test-condition in the do-while and while loops.
 In for() loop, the continue statement redirects control to the increment expression, and then
tests the condition in the 2nd expression.
Usages of continue statement within loops.

while(condition-1)
{
….
….
if(expression-2)
continue;
….
….
}

do
{



if(expression-1)
continue;



}while(condition-1);

for(;;)
{

Dr. Bharathi P. T Department of CS&E


Introduction to C programming (ESC05) Decisions Making & Looping


if(expression-1)
continue;



}

Exit() function: -

 It is used to terminate a program immediately.


 General form: exit(n);

 It closes all open files, flushes memory buffers and returns the value of ‘n’ to the function that
called it.
 n represents the exit status of the program,
o n = 0, represents, error free termination,
o n != 0 represents, error.
 The value of ‘n’ can be used to diagnose the error.
 The default value of ‘n’ is taken as 0(zero), which need not be specified.
 Thus, the default form of exit is: Exit();

Points to be noted before using for(;;) statement: -

 Loop index can appear in a statement in the body of the loop, but its value must not be altered.
 The loop will not be executed when the initial value is greater than the final value.
 Control can be transferred out of the loop but not inside it.
 In the case of nested loops, the outer loop and the inner loop cannot have the same index
variable.
 Each inner(nested ) loop must be completely embedded within an outer loop,
 Control can be transferred from an inner loop(nested) to a statement in the outer loop or to a
statement outside the entire nest.
But, control cannot be transferred to a statement within a nest from a point outside the
nest.

Dr. Bharathi P. T Department of CS&E


Introduction to C programming (ESC05) Decisions Making & Looping

Comparison between while() and do-while(): -

do-while() while()

1. The condition is tested at the end of the 1. Condition is examined at the beginning of
loop. It is an exit-controlled loop. the loop. It is an entry-controlled loop.
2. The while() loop continues to operate only
2. do-while() operates as long as the if the condition is true.
condition is true. 3. The while () loop may not be executed at
3. The do-while is executed at least once. all.

Comparison between continue and break statements: -

Continue Break

1. General form : continue; 1. General form: break;

2. It cannot be used in a switch statement. 2. It is used to transfer control outside a


3. It is used to skip the statements following switch statement.
it and proceed to the next iteration of the 3. It is used to exit from a loop.
loop.
4. In nested loops, continue cannot proceed
to the next iteration of the outer loop. 4. break cannot exit from nested loops.

Example for continue statement.

(Continue)

#include<stdio.h>
void main()
{
int i=0;
while(i<5)
{
i++;
if(i ==4)
continue;
printf(“%3d”,i);
}
}
Output: -
1 2 3 5

Dr. Bharathi P. T Department of CS&E


Introduction to C programming (ESC05) Decisions Making & Looping

Example for break statement.

(break)

#include<stdio.h>
void main()
{
int i = 0;
while(i < 5)
{
i++;
if(i ==4)
break;
printf(“%3d”, i);
}
}

Output: -
1 2 3

Example for continue and break within do-while loop.

#include<stdio.h>
void main()
{
int x;
do
{
printf(“Input number : \n”);
scanf(“%d”, &x);
if(x<0)
{
printf(“Invalid”);
break;
}
if(x > 100)
{
printf(“Skip the value \n”);
continue; /* jumps to 2nd (next) iteration */
}
printf(“Given number is %d\n”, x);
} while(x != 0);
}

Dr. Bharathi P. T Department of CS&E


Introduction to C programming (ESC05) Decisions Making & Looping

Sample output: -

Input number : 4
Given number is : 4

Input number :
..

..
..
..

The program keeps executing until the value which we enter is ‘0’ (Zero) or negative value, then it will
terminate.

If the value entered is above 100, it will skip and ask for another input.

Dr. Bharathi P. T Department of CS&E

You might also like