1. Lab Report
Course ID: TE-3702
Course Title:-Computer Application and Programming
(Sessional)
Experiment No. 04
Name:-Introduction to C control flow (for, while, do while,
break and continue).
Submitted by
Name :
ID :
Year and Semester :
Session :
Department :
Mahbubay Rabbani
175035
3/1
2018-19
Textile Engineering
Submitted to
Khawja Imran Masud
Lecturer
Department of CSE
Dhaka University of Engineering & Technology (DUET), Gazipur
2. Introduction:-
In programming, the process of execution of the same program is over
and over is called looping. A loop is used to repeat a statement until the
specified condition is satisfied. C programming has three types of
loops:-
1. for loop.
2. while loop.
3. do while loop.
Jump Statements in C
continue.
break.
goto.
Objectives:-
To know about for loop.
To know about while loop.
To know about do while loop.
To know about break statement.
To know about continue statement.
3. for loop:-
It works with 3 steps:-
In the 1st
step initialization happens and the variable gets initialized.
In the 2nd
step the condition is checked. If the condition is true then the
C statements inside the body of for loop is executed, if the condition is
false then the for loop gets terminated.
In 3rd
step after successful execution of statements inside the body of
loop, the variable is incremented or decremented and again check the
condition till termination.
5. while loop:-
The variable is initialized with value and then it has been tested for the
condition.
If the condition is true then the statements inside the body of while loop
are executed else the loop is terminated.
The value is incremented using increment ++ operator then it has been
tested again for the loop condition.
Example
#include <stdio.h>
int main()
{
int i=1;
while (i<=10)
{
printf("%dn",i);
i++;
}
}
7. Infinite while loop:- Inside the body of loop if the condition will be true
always then the loop never terminate.
Example
#include <stdio.h>
int main()
{
int i = 1;
while (i >=1)
{
printf("%d", i);
i++;
}
return 0;
}
9. do while loop:-
do while loop is similar to while loop but it executes the statements
inside the body of do-while before checking the condition.
On the other hand in the while loop, first the condition is checked and
then the statements are executed.
So if a condition is false at the first place then the do while would
execute once but the while loop would not execute.
Example
#include <stdio.h>
int main()
{
int mim=0;
do
{
printf("mim is=%dn",mim);
mim++;
}while (mim<=3);
return 0;
}
11. Difference between between while and do while loop:-
Program executed for do while program not executed for while
12. continue statement:-
The continue statement is used inside loops.
When the condition is true under the continue statement then it skips the
print of statement and again go to the condition check for the next.
Example
#include <stdio.h>
int main()
{
int i;
for(i=0; i<=8; i++)
{
if (i==4)
{
continue;
}
printf("%dn",i);
}
return 0;
}
13. Output
As here i=4 is true for continue statement and so skip it for
execution that’s why it is not printed.
14. break statement:-
It is used to terminate the loop instantly.
When the condition is true under the break statement then it terminates
the print of statement.
Example
#include <stdio.h>
int main()
{
int i;
for (i=1;i<=10;i++)
{
printf("%dn",i);
if(i==3)
{
break;
}
}
return 0;
}
15. Output
As the condition i=3 is true under the break statement and that’s
why not printed up to 10 here and the condition is break after
printed 3
16. Home task:-
1. Write a proram and display the output
1+3+5+7+----+n
2+4+6+8+----+n
Ans:- (Summation of odd numbers)
#include<stdio.h>
int main()
{
int i,n,sum=0;
printf("enter the range of numbers=");
scanf("%d",&n);
for(i=1; i<=n; i=i+2)
{
sum=sum+i;
}
printf("the result of all odd numbers=%d",sum);
return 0;
}
18. Ans:- (Summation of even numbers)
#include<stdio.h>
int main()
{
int i,n,sum=0;
printf("enter the range of numbers=");
scanf("%d",&n);
for(i=2; i<=n; i=i+2)
{
sum=sum+i;
}
printf("the result of all even numbers=%d",sum);
return 0;
}
20. 2. Write a c program that find the factorial of a number.
Ans:-
#include<stdio.h>
int main()
{
int i,n,f=1;
printf("enter the range of number=");
scanf("%d",&n);
for(i=1; i<=n; i++)
{
f=f*i;
}
printf("the factorial result of the number=%d",f);
return 0;
}
22. Conclusion:- From this experiment we have learn about C control
flow and jump statements and also learn their working principle which is
very important for programming. I think it will help me a lot in practical
life