1.2.3 Statement Flow Control

Origin C supports all ANSI C flow control statements including the if, if-else, switch, for, while, do-while, goto, break and continue statements. In addition, Origin C supports the C# foreach for looping through a collection of objects.

The if Statement

The if statement is used for testing a condition and then executing a block of statements if the test results are true. The if-else statement is similar to the if statement except the if-else statement will execute an alternative block of statements when the test results are false.

The following are examples of if statements in Origin C, using different input types:

bool bb = true;    // boolean type	
if( bb ) 
{
	out_str("bb is true");
}

int nn = 5;
if( nn )           // integer type, 0 = false, non-zero = true
{
	out_str("nn not 0");
}

double* pData = NULL;
if( NULL == pData ) // check if pointer is NULL
{
	out_str("Pointer pData is NULL");
}

The following is a simple if-else block in Origin C. Note that the if-block and the else-block are enclosed in separate sets of curly braces, {}.

if( bRet )
{
	out_str("Valid input");      // when bRet is true
}
else
{
	out_str("INVALID input");    // when bRet is false
}

The curly braces are optional if the block contains only one statement. This means the above code can also be written without the braces.

if( bRet )
	out_str("Valid input");      // when bRet is true
else
	out_str("INVALID input");    // when bRet is false

The switch Statement

The switch statement is used when you need to execute a different block of statements dependent on a set of mutually exclusive choices.

Cases are executed by ascending integer, starting with the number given in the integer argument to the switch statement. Note that the break command will exit the switch-block from any of the cases.

switch( nType ) // integer type value as condition
{   
case 1:
case 2:
    out_str("Case 1 or 2");
    break;
    
case 3:
    out_str("Case 3");
    // no break keyword here, so fall through to case 4
    
case 4:
	out_str("Case 4");
	break;
    
default:
    out_str("Other cases");
    break;
}

The for Statement

The for statement is often used to execute one or more statements a fixed number of times or for stepping through an array of data wherein each element is referenced by an index.

char str[] = "This is a string";
for( int index = 0; index < strlen(str); index++ )
{
	printf("char at %2d is %c\n", index, str[index]);
}

The while Statement

The while and do-while statements execute a block of statements until a condition has been met. The while statement tests the condition at the beginning of the loop and the do-while statement tests the condition at the end of the loop.

int count = 0;
while( count < 10 ) // execute statements if condition is true
{
    out_int("count = ", count);
    count++;
}
int count = 0;
do
{
	out_int("count = ", count);
	count++;
} while( count < 10 ); // execute statements if condition is true

Jump Statements

Jump statements are used to unconditionally jump to another statement within a function. The break, continue, and goto statements are considered jump statements. The following examples demonstrate these jump statements.

break

for( int index = 0; index < 10; index++ )
{
	if( pow(index, 2) > 10 )
		break; // terminate for loop
	
	out_int("index = ", index);
}

continue

printf("The odd numbers from 1 to 10 are:");
for( int index = 1; index <= 10; index++ )
{
	if( mod(index, 2) == 0 )
		continue; // next index
	
	printf("%d\n", index);
}

goto

    out_str("Begin");
    goto Mark1;
		
    out_str("Skipped statement");
	
Mark1:
    out_str("First statement after Mark1");

The foreach Statement

The foreach statement is used for looping through a collection of objects. The following code loops through all the pages in the project and outputs their name and type.

foreach(PageBase pg in Project.Pages)
{
    printf("%s is of type %d\n", pg.GetName(), pg.GetType());
}

Refer to the Collections section for a list of all the Collection based classes in Origin C.