User login

  

Statements

Return statements used for early function exit have the following general syntax:

return(Expression);  //for functions that return a value
return void;         //for void functions; means that function does not return value
return value;

where Expression is an expression evaluating to a value of the same time as the function returns.

The If Statement

The If conditional statement has the following general syntax:

if(Condition)
{
     Statement
};

where Condition is a logical expression evaluating to true or false

         Statement is one or more PTL statements

Example:

If statement is very useful for check ranges. The next code fragment check age for fixed range:
if( age > 21 and age < 65 )    // condition is correct

But be careful! The math notation is wrong. For example above, next condition is illegal:
if( 21 < age < 65 )        // condition is wrong!

The If-Else Statement

The If-Else conditional statement has the following general syntax:

if(Condition)
                 Statement
else
               Statement;

where Condition is a logical expression evaluating to true or false
         Statement is one or more PTL statements

 

 

Composite If-Else Statement

Example, you want to have any values of string str for different temperature value (in degrees centigrade):

if( n <= 10 )
     str = "cold"
else if( (n >= 10) and (n <= 18) )
     str = "fresh"
else if( (n > 18) and (n <= 28) )
     str = "hot"
else str = "very hot";       // for values where n > 28

So, when n equal to 25, the str gives value "hot".

 

The Switch-Case Statement

The Switch-Case conditional statement has the following general syntax:

switch(Expression)

case Constant1:
        Statement;
        break;
case Constant2:
       Statement;
       break;
      ...
end;

where Expression is a logical expression evaluating to Constant1, Constant2 etc. Also, result of expression must be int, string or char

         Statement is one or more PTL statements

Example:

Traffic lights has three states: red, yellow and green. And only "green" we can movie. Next fragment consider this situation:

bool go;                                   // when go equal to true, we can move across road crossing.
switch( getTrafficLightColor() )    // function, that return current color as string
    case "red":
    case "yellow":
           go = false;
           break;
    case "green":
           go = true;
           break;
end;

The While Statement

The While loop has the following general syntax:

while(Expression)
{Statement};

where Expression is a logical expression evaluating to true or false

         Statement is one or more PTL statements

If Expression equal to true, the loop body executed and Expression check again.

Example:

The next code fragment calc sum of integer numerics from the 1st to the 9th inclusive:

int sum = 0;
int i = 1;
while(i < 10)
{
{    sum += i;
    i += 1;
};

 

The Do-While Statement

The Do-While loop has the following general syntax:

do
{
Statement
}while(Expression);

where Expression is a logical expression evaluating to true or false

         Statement is one or more PTL statements

The loop body executed and than check Expression. If Expression equal to true, the loop repeat.

 

The Break and Continue statements

break; and continue; statements can be used to break out of a loop or skip the next loop body and start another iteration, respectively.

The next code fragment calc sum of integer numerics  from the 1st to the 10th inclusive without 5:

int sum = 0;
int i = 0;
do
{
    i += 1;
  if( i == 5 ) continue;
    sum += i;
}while(i <= 10);

 

The Goto-Label Statement

The Goto-Label loop has the following general syntax:    

goto wordLabel;
...
label wordLabel;

Example:

The next code fragment calc sum of integer numerics from the 1st to the 10th inclusive:

int sum = 0;
int i = 1;
label LOOP;
sum += i;
i += 1;
if( i < 10) goto LOOP;

12345

Comments