Sunteți pe pagina 1din 7

9/26/2016 Cprogrammingwhileanddo...

whileLoop

Cprogrammingwhileanddo...whileLoop
Loops are used in programming to repeat a specific block of code. After reading this
tutorial, you will learn how to create a while and do...while loop in C programming.

Loopsareusedinprogrammingtorepeataspecificblockuntilsomeendconditionismet.There
arethreeloopsinCprogramming:

1.forloop
2.whileloop
3.do...whileloop

whileloop

http://www.programiz.com/cprogramming/cdowhileloops 1/7
9/26/2016 Cprogrammingwhileanddo...whileLoop

Thesyntaxofawhileloopis:

while(testExpression)
{
//codes
}

Howwhileloopworks?
Thewhileloopevaluatesthetestexpression.

Ifthetestexpressionistrue(nonzero),codesinsidethebodyofwhileloopisevaluated.Then,
againthetestexpressionisevaluated.Theprocessgoesonuntilthetestexpressionisfalse.

Whenthetestexpressionisfalse,thewhileloopisterminated.

Flowchartofwhileloop

http://www.programiz.com/cprogramming/cdowhileloops 2/7
9/26/2016 Cprogrammingwhileanddo...whileLoop

Example#1:whileloop

//Programtofindfactorialofanumber
//Forapositiveintegern,factorial=1*2*3...n

#include<stdio.h>
intmain()
{
intnumber;
longlongfactorial;

printf("Enteraninteger:");
scanf("%d",&number);

factorial=1;

//loopterminateswhennumberislessthanorequalto0
while(number>0)
{
factorial*=number;//factorial=factorial*number;
number;
}

printf("Factorial=%lld",factorial);

return0;
}

Output

Enteraninteger:5
Factorial=120

Tolearnmoreontestexpression(whentestexpressionisevaluatedtononzero(true)and0
(false)),checkoutrelationalandlogicaloperators.

do...whileloop
Thedo..whileloopissimilartothewhileloopwithoneimportantdifference.Thebodyof
do...whileloopisexecutedonce,beforecheckingthetestexpression.Hence,thedo...while
loopisexecutedatleastonce.

http://www.programiz.com/cprogramming/cdowhileloops 3/7
9/26/2016 Cprogrammingwhileanddo...whileLoop

do...whileloopSyntax

do
{
//codes
}
while(testExpression);

Howdo...whileloopworks?
Thecodeblock(loopbody)insidethebracesisexecutedonce.

Then,thetestexpressionisevaluated.Ifthetestexpressionistrue,theloopbodyisexecuted
again.Thisprocessgoesonuntilthetestexpressionisevaluatedto0(false).

Whenthetestexpressionisfalse(nonzero),thedo...whileloopisterminated.

Example#2:do...whileloop

http://www.programiz.com/cprogramming/cdowhileloops 4/7
9/26/2016 Cprogrammingwhileanddo...whileLoop

//Programtoaddnumbersuntiluserenterszero

#include<stdio.h>
intmain()
{
doublenumber,sum=0;

//loopbodyisexecutedatleastonce
do
{
printf("Enteranumber:");
scanf("%lf",&number);
sum+=number;
}
while(number!=0.0);

printf("Sum=%.2lf",sum);

return0;
}

Output

Enteranumber:1.5
Enteranumber:2.4
Enteranumber:3.4
Enteranumber:4.2
Enteranumber:0
Sum=4.70

Tolearnmoreontestexpression(whentestexpressionisevaluatedtononzero(true)and0
(false)),checkoutrelationalandlogicaloperators.

Checkouttheseexamplestolearnmore:

CProgramtoCheckWhetheraCharacterisVowelorConsonant
CProgramtoCalculatetheSumofNaturalNumbers
CProgramtoDisplayFibonacciSequence

PreviousPage NextPage

http://www.programiz.com/cprogramming/cdowhileloops 5/7
9/26/2016 Cprogrammingwhileanddo...whileLoop

Have.COMDomainin C++whileanddo...while DataAnalyticsCourses:R, CProgrammingCodeTo


Mind? Loop SASTaketheFree... CreatePyramidand...

Ad Bigrock programiz.com Ad jigsawacademy.com programiz.com

CProgrammingforLoop WebDeveloper? CProgramtoFind FlowchartIn


FactorialofaNumber Programming

programiz.com Ad Payoneer programiz.com programiz.com

CProgramming

CIntroduction
CFlowControl

Cif...else

C for Loop

C do...while Loop

C break andcontinue

Cswitch...case

C Programming goto

http://www.programiz.com/cprogramming/cdowhileloops 6/7
9/26/2016 Cprogrammingwhileanddo...whileLoop

Control Flow Examples

CFunctions
CProgrammingArrays
CProgrammingPointers
CProgrammingStrings
StructureAndUnion
CProgrammingFiles
AdditionalTopics

Receive the latest tutorial to improve your programming skills

Enter Your Email Join

CopyrightbyProgramiz|Allrightsreserved|PrivacyPolicy

http://www.programiz.com/cprogramming/cdowhileloops 7/7

S-ar putea să vă placă și