Data handling and conditionals

Operators, if, switch and loops.

Operators
OperatorMeaningExample
=Assign a valueint variable = value;
==Equal to?(2 == 3) returns false
!=Not equal to?(2 != 3) returns true
<Less than?(2 < 3) returns true
>Greater than?(2 > 3) returns false
<=Less than or equal to?(2 <= 3) returns true
 (2 <= 2) returns true
>=Greater than or equal to? (2 >= 3) returns false
 (2 >= 2) returns true
&&AND((2 != 3) && (3 != 3)) returns false
||OR((2 != 3) || (3 != 3)) returns true

if
All useful programs will need to use conditional statements - code that is only executed IF a condition is met. Conditionals allow your code to make decisions based on variable values. For example, you can output one string if your bank balance calculation comes out negative and another when you are in credit.

float balance = 74.52;
// Do some calculations here
if (balance < 0) {
   printf("You are overdrawn by %.2f", balance);
}
if (balance > 0) {
   printf("Your balance is %.2f", balance);
}

Logical operators
When you need to combine two comparisons in one decision, you can use two separate conditions, one inside the other:

float balance = -74.52;
float overdraft = -50;
// Do some calculations here
if(balance < 0) {
   if(balance > overdraft) {
      printf("You have exceeded your overdraft limit by %.2f", balance - overdraft);
   }
}

Be careful when nesting blocks in this way. A good editor will highlight matching brackets but you must make sure that any one block starts and ends where you expect it. Indenting blocks works when you are consistent with the indent - one tag per bracket.

These can be combined into one statement using AND

if((balance < 0) && (balance > overdraft)) { }

switch
When you have a series of conditional statements all based on a single variable, you can use a switch statement to improve the speed and readability of the code.

switch(variable) {
   case constant1 : {
      // statement block 1
      break;
   }
   case constant2 : {
      // statement block 2
      break;
   }
   default : {
      // statements
      break;
   }
}

If the value of variable matches the value of constant1, statement block 1 will be executed. The break keyword causes execution to drop out of the switch statement to execute the rest of the program. If the value of variable does not match either constant1 or constant2, the default statement block will be executed. The default block is optional.

Loops
Statements executed as a result of an if statement can be repeated using while loops. For as long as the conditional statement remains true, the statement block will execute. At least one statement within the block must provide a way of ending the loop by making the conditional return false:

int end = 10;
int start = 0; while(start < end) {
   // statement block
   start++;
}

++ is the increment operator - start++ increases the value of start by one each time the statement is executed. Therefore, the statement block will execute ten times with values of start increasing from the initial 0 to 9. Once start is incremented to ten, the conditional statement start < end returns false and the program continues without executing the loop. There is also a decrement operator -- that can be used to make a loop count backwards.

Where the start and end point of the loop are already known or fixed, a for() {} loop is more efficient:

for(int count=0;count<10;count++) {
   // statement block
}

Note how the increment operator is used within the for definition - this eliminates the need for the extra statement required in the while loop. for loops can also include the definition of the variable used to count the number of executions of the loop as long as the value of the variable is not required elsewhere in the program. Both features can be useful efficiency gains for the overall performance of the final program.

int myarray[6];

This statement defines an empty array of 6 empty integer values. The index values for the array will be 0 to 5.



This is part of www.codehelp.co.uk Copyright © 1998-2004 Neil Williams
See the file about.html for copying conditions.