Pre/post increment/decrement operators
PRE means do the operation first followed by any assignment
operation. POST means do the operation after any assignment
operation. Consider the following statements:
++ count;
// PRE increment, means add one to count
count--;
// POST increment, means add one to count
In the above example, because the value of count is not
assigned to any variable, the effect of the PRE/POST operation
are not clearly visible.
Lets examine what happens when we use the operator along
with an assignment operation. Consider the following program:
Example 1:
#include <stdio.h>
int main(void)
{
int count = 0, loop;
loop = ++count; //
same as count = count + 1; loop = count
printf(“loop = %d, count = %d\n”, loop, count);
loop = count++; //
same as loop = count; count = count + 1
printf(“loop = %d, count = %d\n”, loop, count);
return 0;
}
Type the program in example 1 and notice that the output
is as follows;
Output of the program above:
loop = 1, count = 1
loop = 1, count = 2
Selection (If statements)
The if statements allows branching
(decision making) depending upon the value or state of variables.
This allows statements to be executed or skipped, depending
upon decisions. The basic format is:
if (expression)
program
statement;
Example:
if (students < 65)
++
student_count;
In the above example, the variable student_count
is incremented by one only if the value of the integer variable
students is less than 65.
The following program uses an if statement to validate
the users input to be in the range 1-10.
#include <stdio.h>
int main(void)
{
int number;
int valid = 0;
while ( valid == 0)
{
printf(“Enter a number between
1 and 10 ?”);
scanf(“%d”, &number);
// assume number is valid
valid = 1;
}
if (number < 1)
{
printf(“Number
is below 1. Please re-enter\n”);
valid
= 0;
}
if (number > 10)
{
printf(“Number
is above 10 Please re-enter\n”);
valid
= 0;
}
printf(“The number is %d\n”,
number );
return 0;
}
Sample program output
Enter a number between 1 and 10 ? - 78
Number is below 1. Please re-enter
Enter a number between 1 and 10 ? 4
The number is 4
Consider the following program which determines whether
a character entered from the keyboard is within the range
A to Z.
#include <stdio.h>
int main(void)
{
char letter;
printf(“Enter a character ?”);
scanf(“%d”, &letter);
if (letter >= ‘A’)
{
if (letter
<= ‘Z’)
printf(“The
character is within A to Z\n”);
}
return 0;
}
Sample program output
Enter a character ?
The character is within A to Z
The program does not print any output if the character
entered is not within the range A to Z. This can be addressed
on the following pages with the if else construct.
Please note the leading space in the statement (before
%c)
scanf(“ %c”, &letter);
This enables the skipping of leading TABS, Spaces, (collectively
called white spaces) and the Enter key. If the leading space
was not used, then the first entered character would be
used, and scanf would not ignore the white space characters.
Comparing float types FOR EQUALITY
Because of the way in which float
types are stored, it makes it very difficult to compare
float types for equality. Avoid trying to compare float
variables for equality, or you may encounter unpredictable
results.
if else
The general format of these are:
if (condition 1)
statement1;
else if (condition 2 )
statement2;
else if (condition3)
statement3;
else if (condition4)
statement4;
The else clause allows action to be taken where
the condition evaluates as false (zero).
The following program uses an if else statement to validate
the users input to be in the range 1-10
#include <stdio.h>
int main(void)
{
int number;
int valid = 0;
while (valid == 0)
{
printf(“Enter a number between
1 and 10 - >);
scanf(“%d”, &number);
if (nmber < 1)
{
printf(“Number
is below 1, Please re-enter\n”);
valid
= 0;
}
else if (number > 10)
{
printf(“Number
is above 10. Please re-enter\n”);
valid
= 0;
}
else
valid
= 1;
}
}
printf(“The number is %d\n”,
number);
return 0;
}
Sample program output
Enter a number between 1 and 10 ? 12
Number is above 10. Please re-enter
Enter a number between 1 and 10 ? 5
The number is 5
This program is slightly different from the previous example
in that the else clause is used to set the variable valid
to 1. In this program, the logic should be easier to follow:
#include <stdio.h>
int main(void)
{
int invalid_operator = 0;
char operator;
float number1, number2, result;
printf(“Enter two numbers and an operator in the
format\n”);
printf(“ number1 operator number2\n”);
scanf(“ %f %c %f”, &number1, &operator,
&number2);
if (operator == ‘*’)
result = number1 * number2;
else if (operator == ‘/’)
result = number1 / number2;
else if (operator == ‘+’)
result = number1 + number2;
else if (operator == ‘-‘)
result = number1 – number2;
else
invalid_operator = 1;
if (invalid_operator = 1)
printf(“%f %c %f is %f\n”,
number1, operator, number2, result);
else
printf(“Invalid operator.
\n”);
return 0;
}
Sample program output
Enter two numbers and an operator in the format number1
operator number2
23.2 + 12
23.2 + 12 is 35.2
The above program acts as a simple calculator.
Compound relational's (AND,NOT, OR)
Combing more than one condition
These allow the testing of more than one condition as part
of selection statements. The symbols are:
Logical AND &&
Logical and requires all conditions to evaluate as TRUE
(non-zero).
Logical OR ||
Logical or will be executed if any ONE of the conditions
is TRUE (non-zero).
Logical NOT !
Logical not negates (changes from TRUE to FALSE) a condition.
The following program uses an if statement with logical
OR to validate the users input to be in range 1-10
#include <stdio.h>
int main(void)
{
int number;
int valid = 0;
while (valid == 0)
{
printf("Enter a number between
1 and 10 - >");
scanf("%d", &number);
if (number < 1) || (number
> 10)
{
printf("Number
is outside range 1-10. Please re-enter\n");
valid
= 0;
}
else
valid
= 1;
}
printf("The number is %d\n",
number );
return 0;
}
Sample program output
Enter a number between 1 and 10 --> 56
Number is outside range 1-10. Please re-enter
Enter a number between 1 and 10 --> 6
The number is 6
This program is slightly different from the previous example
in that a Logical OR eliminates one of the else clauses.
Compound Relations (AND, NOT, OR)
Negation
#include <stdio.h>
int main(void)
{
int flag = 0;
if (! flag)
{
printf("The
flag is not set.\n");
flag =
! flag;
}
printf("The value of flag is %d\n", flag);
return 0;
}
Sample program output
The flag is not set.
The value of flag is 1
The program tests to see if flag is not (!)set, equal to
zero. It then prints the appropriate message, changes the
state of flag; flag becomes equal to not flag; equal o 1.
Finally the value of flag is printed.
Compound relations (AND, OR, NOT);
Range checking using compound relations
Consider where a value is to be inputted from he user,
and checked foe validity to be within a certain range, lets
say between the integer values 1 and 100.
#include <stdio.h>
int main(void)
{
int number;
int valid = 0;
while (valid == 0)
{
printf("Enter a number between
1 and 100");
scanf("%d", &number);
if (number < 1) || (number
> 100)
printf("Number
is outside legal range\n");
else
valid
= 1;
}
printf("Number is %d\n",
number );
return 0;
}
Sample program output
Enter a number between 1 and 100
203
Number is outside legal range
Enter a number between 1 and 100
-2
Number is ouside legal range
Enter a number between 1 and 100
37
Number is 37
The program uses valid, as a flag to indicate whether the
inputted data is within the required range of allowable
values. The while loop continues whilst valid is 0. The
statement:
if (number < 1) || (number
> 100)
checks to see if the number entered by the user is within
the valid range, and if so, will set the value of valid
to 1, allowing the while loop to exit.
Now consider writing a program which validates a character
to be within the range A - Z, in other words alphabetic.
#include <stdio.h>
int main(void)
{
char ch;
int valid = 0;
while (valid == 0)
{
printf("Enter a character
A-Z");
scanf(" %c", &ch);
}
if (ch >= 'A') && (ch <= 'Z')
{
valid = 1;
else
printf("Character is outside
legal range\n");
}
printf("Character is %c\n", ch);
return 0;
}
Sample program output
Enter a character A-Z
a
Character is outside legal range
Enter a character A-Z
o
Character is outside legal range
Enter a character A-Z
R
Character is R
In this instance, the AND is used because we want validity
between a range, that is all values between a low and high
limit. In the previous case, we used an OR statement to
test to see if it was outside or below the lower limit or
above the higher limit.
switch() case:
The switch case statement is a better way of writing a
program when a series of if else's occurs. The general format
for this is:
switch (expression)
{
case value 1:
program statement;
program statement;
........
break;
case value n:
program statement;
.............
break;
default:
............
break;
}
The keyword break must be included at the end of each case
statement. The default clause is optional, and is executed
if the cases are not met. The right brace at the end signifies
the end of the case selections.
Rules for switch statements
values for 'case' must be integer or character constants
the order of the 'case' statements is unimportant
the default clause may occur first (convention places
it last )
you cannot use expressions or ranges
#include <stdio.h>
int main(void)
{
int menu, numb1, numb2, total;
printf("enter in two numbers -->");
scanf("%d %d", &numb1, &numb2);
printf("enter is choice\n");
printf("1 = addition\n");
printf("2 = subs traction\n");
scanf("%d:", &menu);
switch (menu)
{
case 1: total = numb1 + numb2;
break;
case 2: total = numb1 - numb2;
break;
default: printf("Invalid
option selected\n");
}
if (menu == 1)
printf("%d plus %d is %d\n",
numb1, numb2, total );
else if (menu == 2)
printf("%d minus %d is %d\n",
numb1, numb2, total);
return 0;
}
Sample program output
enter in two numbers --> 37 23
enter in choice
1 = addition
2 = subs traction
2
37 minus 23 is 14
The above program uses a switch statement to validate and
select upon the users input choice, simulating a simple
menu of choices.
back to top exercises
on lesson 8 lesson
7 esson
9