Вы находитесь на странице: 1из 9

Experiment No.

6 Programming In ‘C’

EXPERIMENT NO.6
1.0 TITLE :
To understand Loop control statements in ‘C’.

2.0 PRIOR CONCEPTS:


• Flow of program control should be known.
• Concepts of decision control statement.

3.0 NEW CONCEPTS:


Proposition 1:
Sometimes it is very necessary to execute some statements repeatedly, and also sometimes it
requires to skip execution of statement. For this purpose Loop control statements are used.
Various Loop control statements are:
1. for loop.
2. while loop.
3. do….while loop.
Proposition 2: For loop
This loop is used to execute a set of statement number of times .The for keyword is followed by
three statements enclosed within round braces ( ) and these three statements are separated by
semicolon;.
Syntax :
The general syntax of for loop is:
for ( counter initialization ; test counter condition; increment counter )
{
Statement 1;
Statement 2;
:
:
Statement n;
}

30 o MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION


Programming In ‘C’ Experiment No. 6

Concept Structure:

The initialization statement is executed only once prior to the statements within the for loop. The
counter condition is executed once before execution of the statements within the loop. If this condition
is true, the statements within the loop are executed. If it is false, the loop terminates and the control
of execution is then transferred to the statement following the for loop. The update counter statement
is executed once after every execution of the statements within the loop.
Proposition 3: while statement:
This loop executes the condition before executing any of the statements in its body. This loop is
used when the number of times the loop is to be executed is not known in advance
Syntax:
The general syntax for while loop is
Initialize loop counter
while ( test counter)
{
Statement 1;
Statement 2;
:
:
Statement n;
Increment loop counter;
}

MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION o 31


Experiment No. 6 Programming In ‘C’

Concept Structure:

The loop is repeated as long as the test expression is true and when it becomes false the while loop
is terminated and program execution proceeds with the next statement immediately after the while
loop.
Proposition 4: do…. while statement:
This loop executes the condition after execution of the statements in its body. This loop is used
when at least once the statements has to be execute even if the test condition of do … while
statement false first time itself.
Syntax:
The general syntax for do… while loop is
do
{
Statement 1;
Statement 2;
:
:
Statement n;
} while ( test expression);

32 o MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION


Programming In ‘C’ Experiment No. 6

Concept Structure:

The loop is repeated as long as the test expression is true and when it becomes false the do…
while loop is terminated and program execution proceeds with the next statement immediately after
the do.. while loop.

4.0 LEARNING OBJECTIVES:


Intellectual skills:
1. To understand the idea of control structure
2. To understand the idea of loop.
3. To understand the idea of execution of program.
4. To understand the idea of for loop.
5. To understand the idea of while loop.
6. To understand the idea of do… while loop.
Motor Skills:
1. To handle the keyboard properly.

5.0 SAMPLE PROGRAM:


Problem Statement:
Write a program to display output as follows.
1 2 3 4 5…………… 9

MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION o 33


Experiment No. 6 Programming In ‘C’

Using while loop.


Algorithm:
1. Start
2. Include all required header files.
3. Initialize counter. ( set it to one)
4. Display counter.
5. Increment counter.
6. Repeat step number four and five untill counter becomes Ten.
7. End.
Flowchart:

Program code:
# include < stdio.h >
# include < conio.h >
main ( )
{
int i;
clrscr ( );
i=1;
while ( i<10)
{
printf (“ %4d”, i);
i++;
}
}

34 o MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION


Programming In ‘C’ Experiment No. 6

Output:
1 2 3 4 5 6 7 8 9

Using Do.. while loop.


Algorithm:
1. Start.
2. Include all required header files.
3. Initialize counter. ( set it to one)
4. Display counter.
5. Increment counter.
6. Check whether counter is less than ten if yes repeat step number 4,5,6.
7. End.
Flowchart:

Program code:
# include < stdio.h >
# include < conio.h >
main ( )
{
int i;
clrscr ( );
i=1;
do {
printf (“ %4d”, i);
i++;
} while ( i<10);
}
Output:
1 2 3 4 5 6 7 8 9

MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION o 35


Experiment No. 6 Programming In ‘C’

6.0 EXERCISES:
A. Attempt Q… , Q… from the following. (Teacher shall allot the questions)
1. Write a program to calculate the sum of N element.
2. Write a program to print sum of the digits in the given number.
3. Write a program to calculate factorial of a given number.
4. Write a program to find sum of given series using while statement 1+3+5+7+…….
5. Write a program to find sum of given series using do while loop.
0+ 1 +3 +6 +10 +15 +22.

B. Attempt all questions and write the output of program in the space provided below.
1. # include <stdio.h>
main( )
{
int i=0;
for ( ; i ; )
printf( “ Hello………………..Welcome” );
}

2. # include<stdio.h>
main ( )
{
int j,j;
for ( j=0;j<4;j++)
{
for ( i=1;i<=4;i++)
{
printf( “ %4d”,i );
}
}
}

3. # include < stdio.h >


# include < conio.h >
main ( )
{
int number1,number2,fib,number;
printf( “ How many Fibonacci number:”);
scanf(“ %d ” , & number);
printf( “ The Fibonacci numbers are:”);
printf(“ \n 0 \n 1”);
number1=0;
number2=1;
for ( i=0;i<n-2;i++)
{
fib = number2;

36 o MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION


Programming In ‘C’ Experiment No. 6

number2= number1+number2;
printf(“ \n%d ”, number2);
number1=fib;
}
getch( );
}

C. Attempt all questions, Debug following programs and write only errors if any in the
space provided.
1. # include<stdio.h>
main( )
{
int i=1,j=1;
for ( ; ; )
{
if (i>5)
break;
else
j+=i;
printf( “\n%d”, j);
i+= j;
}
}
2. # include<stdio.h>
main ( )
{
char x;
while(x=0;x<=255;x++)
printf(“ \n The character is %c”, x);
}

(Space for answer)

MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION o 37


Experiment No. 6 Programming In ‘C’

(Space for answer)

Signature of Teacher

38 o MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION

Вам также может понравиться