Online Reference

 

 

C REFERENCE

 

RAPID TABLES

 

 

 

 

 

 

    Home > Programming > C Reference > Program Flow

C Reference - Program flow keywords

NameDescription
if-elsedo if condition is true
switch-case-defaultbranch to const integer case that match switch expression or to default when no match. then falls through.
fordo loop when condition is true
whiledo loop when condition is true
do-whiledo loop when condition is true, do at least once
breakexit from switch, for, while, do
continuebranch to next loop iteration of for, while , do
gotobranch to label
returnexit to calling function and return value

 


if-else

Syntax:

if( condition )

   statement1

else

   statement2

Description:
If condition is true do statement1.

If condition is not true do statement2.

Example:

if( a>0 )

   x = x+5;

else

   x = x-5;

 

if( a>0 )

   x += 10;

else if( a<0 )

   x -= 10;

else

   x = 0;

 


switch-case-default

Syntax:

switch( variable )

{

   case const1:

      statement1

      break;

   case const2:

   case const3:

      statement2

      break;

   .

   .

   default:

      statement3

}

Description:
If variable is equal to const1 - do statement1

If variable is equal to const2 or const3 - do statement2

Otherwise do statement3

Example:

int state;

 

switch( state )

{

   case 0:

   case 1:

      x = x+5;

      break;

 

   case 2:

      x = x-5;

      break;

 

   default:

      x = 0;

}

 


for

Syntax:

for(init_statement; stop_condition; expression)

   statement

Description:
Do statement while stop condition is not met.
Example:

/* print x values from 0 to 99 */

for( x=0; x<100; x++ )

{

   printf("x=%d\n",x);

}

 


while

Syntax:

while( condition )

   statement

Description:
Do statement while condition is true.
Example:

/* print x values from 0 to 99 */

x = 0;

while( x<100 )

{

   printf("x=%d\n",x);

   x++;

}

 


do-while

Syntax:

do

   statement

while( condition );

Description:
Do statement while condition is true.

Do at least once.

Example:

/* print x values from 0 to 99 */

x = 0;

do {

   printf("x=%d\n",x);

   x++;

} while( x<100 )


break

Syntax:

break;

Description:
Break from current block.
Example1:

Switch( x ) {

   case 0:

      y = x;

      break;

   case 1:

      y = 2*x;

}

Example2:

while(1) {

   if( x>y )

      break;

}


continue

Syntax:

continue;

Description:
Continue execution in next loop iteration.
Example:

for(i=0; i<N; i++)

{

   if( a[i] > 0 )

       continue;

   sum = sum+a[i];

}


goto

Syntax:

goto label;

Description:
Jump to specified label.
Example:

goto MYLABEL;

:

:

MY_LABEL:

 

 


return

Syntax:

return expression;

Description:
Return from function with return value.
Examples:

/* return with no value (void) */

return;

 

return 0;

 

return (x*y);

 

© 2006-2008 RapidTables.com