Data handling and conditionals
Operators, if, switch and loops.
Operators
Operator Meaning Example
= Assign a value int 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
= 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