You are on page 1of 3

2/8/2016

CControlStatements,if,elseif,while,do,forloop

Copyrighttutorialspoint.com

CFlowControlStatements

Advertisements

Cprovidestwosytlesofflowcontrol:
Branching
Looping
Branchingisdecidingwhatactionstotakeandloopingisdecidinghowmanytimestotakeacertainaction.

Branching:
Branchingissocalledbecausetheprogramchoosestofollowonebranchoranother.

ifstatement
Thisisthemostsimpleformofthebranchingstatements.
Ittakesanexpressioninparenthesisandanstatementorblockofstatements.iftheexpressionistruethenthestatementor
blockofstatementsgetsexecutedotherwisethesestatementsareskipped.
NOTE:Expressionwillbeassumedtobetrueifitsevaulatedvaluesisnonzero.
ifstatementstakethefollowingform:
ShowExample

if(expression)
statement
or
if(expression)
{
Blockofstatements
}
or
if(expression)
{
Blockofstatements
}
else
{
Blockofstatements
}
or
if(expression)
{
Blockofstatements
}
elseif(expression)
{
Blockofstatements
}
else
{
Blockofstatements
}

http://www.tutorialspoint.com/cgibin/printversion.cgi?tutorial=ansi_c&file=c_control_statements.htm

1/3

2/8/2016

CControlStatements,if,elseif,while,do,forloop

?:Operator
The?:operatorisjustlikeanif...elsestatementexceptthatbecauseitisanoperatoryoucanuseitwithinexpressions.
?:isaternaryoperatorinthatittakesthreevalues,thisistheonlyternaryoperatorChas.
?:takesthefollowingform:
ShowExample

ifconditionistrue?thenXreturnvalue:otherwiseYvalue

switchstatement:
Theswitchstatementismuchlikeanestedif..elsestatement.Itsmostlyamatterofpreferencewhichyouuse,switchstatement
canbeslightlymoreefficientandeasiertoread.
ShowExample

switch(expression)
{
caseconstantexpression1:
[caseconstantexpression2:
[caseconstantexpression3:
[default:statements4]
}

statements1
statements2]
statements3]

Usingbreakkeyword:
Ifaconditionismetinswitchcasethenexecutioncontinuesonintothenextcaseclausealsoifitisnotexplicitlyspecifiedthat
theexecutionshouldexittheswitchstatement.Thisisachievedbyusingbreakkeyword.
TryoutgivenexampleShowExample

Whatisdefaultcondition:
Ifnoneofthelistedconditionsismetthendefaultconditionexecuted.
TryoutgivenexampleShowExample

Looping
Loopsprovideawaytorepeatcommandsandcontrolhowmanytimestheyarerepeated.Cprovidesanumberofloopingway.

whileloop
ThemostbasicloopinCisthewhileloop.Awhilestatementislikearepeatingifstatement.LikeanIfstatement,ifthetest
conditionistrue:thestatmentsgetexecuted.Thedifferenceisthatafterthestatementshavebeenexecuted,thetestconditionis
checkedagain.Ifitisstilltruethestatementsgetexecutedagain.Thiscyclerepeatsuntilthetestconditionevaluatestofalse.
Basicsyntaxofwhileloopisasfollows:
ShowExample

while(expression)
{
Singlestatement
or
Blockofstatements
}

forloop
forloopissimilartowhile,it'sjustwrittendifferently.forstatementsareoftenusedtoproccesslistssucharangeofnumbers:
Basicsyntaxofforloopisasfollows:
ShowExample

for(expression1expression2expression3)
{
Singlestatement
or
Blockofstatements
http://www.tutorialspoint.com/cgibin/printversion.cgi?tutorial=ansi_c&file=c_control_statements.htm

2/3

2/8/2016

CControlStatements,if,elseif,while,do,forloop

Intheabovesyntax:
expression1Initialisesevariables.
expression2Condtionalexpression,aslongasthisconditionistrue,loopwillkeepexecuting.
expression3expression3isthemodifierwhichmaybesimpleincrementofavariable.

do...whileloop
do...whileisjustlikeawhileloopexceptthatthetestconditionischeckedattheendoftheloopratherthanthestart.Thishas
theeffectthatthecontentofthelooparealwaysexecutedatleastonce.
Basicsyntaxofdo...whileloopisasfollows:
ShowExample

do
{
Singlestatement
or
Blockofstatements
}while(expression)

breakandcontinuestatements
Cprovidestwocommandstocontrolhowweloop:
breakexitformlooporswitch.
continueskip1iterationofloop.
Youalreadyhaveseenexampleofusingbreakstatement.Hereisanexampleshowingusageofcontinuestatement.

#include
main()
{
inti
intj=10
for(i=0i<=ji++)
{
if(i==5)
{
continue
}
printf("Hello%d\n",i)
}
}
Thiswillproducefollowingoutput:

Hello0
Hello1
Hello2
Hello3
Hello4
Hello6
Hello7
Hello8
Hello9
Hello10

Copyrighttutorialspoint.com

http://www.tutorialspoint.com/cgibin/printversion.cgi?tutorial=ansi_c&file=c_control_statements.htm

3/3

You might also like