"if the amount is at least .... then do ...."
(X < Y)
is a Boolean expression
which evaluates to true if X
is
less than Y
, and evaluates to false
otherwise
This is done using a variety of logical operators (AND, OR, NOT,...). For example
&&
means logical AND.
X < Y
and Y < Z
we can create a combined expression using
the &&
operator: (X < Y) && (Y < Z)
if (Boolean expression evaluates to true) { // do something } else { // do something else }Suppose we wanted to see if the user had entered the character C to continue, or Q to quit. We could use something like this:
// prompt the user to choose between quitting/continuing, // read and store their response printf( "Enter C to continue or Q to quit:\n"); char userChoice; scanf("%c", &userChoice); // act on the user's choice if (userChoice == 'Q') { // here, insert whatever code is needed // in order to shutdown/quit } else if (userChoice == 'C') { // here, insert whatever code is needed // in order to continue } else { // here, insert code to handle the fact // that the user made an invalid choice }
bool
and make use of the identifiers true
and false
#include <cstdio> int main() { bool bVar1, bVar2; bVar1 = true; bVar2 = false; ... }
X < Y
would return true if X is less than
Y, and would return false if X is greater than or equal to Y
(X = Y)
instead of the correct
(X == Y)
// normal instructions if (<Boolean expression>) { // set of statements to execute // if expression evaluates to true } else { // set of statements to execute // if expression evaluates to false } // more normal instructions
printf( "Please enter a positive number\n"); scanf("%f", &iInputValue); if (iInputValue <= 0) { printf( "Sorry, but %f", iInputValue); printf( " is not a positive number"); } else { printf( "The square root of "; printf("%f is %f", iInputValue, sqrt(iInputValue)); } printf("\n");
For instance, the expression
(iData < 3) && (iData > 1)
is true if and only if iData = 2
For instance, the expression
(iData < 1) || (iData > 3)
is true as long as iData
is not one in the range
1..3
For instance, the expression !(iData == 1)
is true
if and only if the expression iData == 1
is false
<cctype>
library are:
islower(cData)
returns true if cData is a lower
case letter,
and returns false otherwise
isspace(cData)
returns true if cData is a white
space (tab, endl, blank etc)
and returns false otherwise
#include <cstdio> bool IsIntegerEven(int iData); int main() { int iUserInput; printf( "Please enter an integer\n"); scanf("%d", &iUserInput); if (IsXEven(userint)) { printf( "%d is even", iUserInput); } else { printf( "%d is odd", iUserInput); } printf("\n"); } bool IsIntegerEven(int iData) { if ((iData % 2) == 0) { return true; } else { return false; } }
&&
and ||
(and, or)
logical operators have one other
special feature: they return a value as soon as they know what
an expression must evaluate to
if ((iCount > 0) && (iCount < 100)) { iCount = iCount + 1; } else { printf( "count is %d", iCount); }First we look at
count > 0
, and we realise this evaluates
to false, so the whole expression must evaluate to false
regardless of the results of (count < 100)
int iDataX = 13; int iDataY = 7; int iDataZ = -3; char cData = 'd';Evaluate the following expressions as true or false:
(iDataX <= iDataY) && (iDataY > iDataZ))
(isdigit('9') || (iDataY == -3))
(iDataX > iDataY) && (iDataY > iDataZ) && (iDataX != iDataZ)
(iDataX % iDataY) > iDataZ
(cData != 'e') && (int(cData) > iDataY)
pow(iDataY * iDataZ, 2) < pow(iDataX, 2)
(iDataX < iDataY) || (iDataY < iDataZ) || (isspace(cData) || true
#include <cstdio> int main() { int iUserInput; printf( "Please enter an integer\n"); scanf("%d", &iUserInput); if (iUserInput < 0) { printf( "You entered a negative number\n"); } if (iUserInput > 0) { printf( "You entered a positive number\n"); } if (iUserInput == 0) { printf( "You entered 0\n"); } }
#include <cstdio> int Get_positive(); int main() { int iEnteredNumber; iEnteredNumber = Get_positive(); printf( "The positive value you entered"); printf( " was %d\n", iEnteredNumber); } int Get_positive() { int iEnteredNumber; // user's entry value printf( "Please enter a positive integer\n"); scanf("%d", &iEnteredNumber); if (iEnteredNumber >= 0) { printf( "Sorry, that was an invalid value\n"); iEnteredNumber = Get_positive(); return iEnteredNumber ; } else { return iEnteredNumber; } }
if (<Boolean expression>) { // statement set A } else { // statement set B }
scanf("%d", &iData); if ((iData > 0) && (iData < MAXINT)) { printf( "sqrt(%d) = ", iData); printf("%f\n", sqrt(iData)); } else { printf( "Sorry, cannot take sqrt("); printf( "%d)\n", iData); }
scanf("%c", &cData); if ((cData == 'd') || (cData == 'D')) { printf( "Did you know that such great names as Dave"); printf( " begin with %c\n", cData); }
if (iData < iMaxAllowable) iData = iData + 1; else iData = iMaxAllowable;
if (iData < iMaxAllowable); iData = iData + 1;You can get very strange behaviour from this kind of error, as it will get translated the same as:
if (iData < iMaxAllowable) { } iData = iData + 1;
if (iData == 1) { // statement set 1 } else if (iData == 2) { // statement set 2 } else if (iData == 3) { // statement set 3 } else if (iData == 4) { // statement set 4 } else { // default statements }
if (iDataX < 0) { // do this stuff if iDataX is negative if (iDataY < 0) { // iDataX and iDataY are both negative total = iDataX * iDataY; } else { // iDataX is negative, iDataY is positive total = - (iDataX * iDataY); } } else { // do this stuff if iDataX is positive if (iDataY < 0) { // iDataX is positive, iDataY is negative total = - (iDataX * iDataY); } else { // iDataX and iDataY are both positive total = iDataX * iDataY; } }
// check that we won't perform divide-by-zero if (iDenominator == 0) { printf( "Cannot divide %d", iNumerator); printf( " by %d\n", iDenominator); } else result = iNumerator / iDenominator;
int get_1_to_10() { int iUserValue; printf( "Please enter a value from 1 to 10\n"); scanf("%d", &iUserValue); if ((iUserValue < 1) || (iUserValue > 10)) { printf( "%d is invalid", iUserValue); iUserValue = get_1_to_10(); } return iUserValue ; }
void get_command() { bool bQuit = false; char cCmd; // prompt user and get command printf( "Enter S to calculate square roots\n"); printf( " X to translate hexadecimal values\n"); printf( " or Q to quit\n"); scanf("%c", &cCmd); // handle the current command if (cCmd == 'S') { square_root_routine(); } else if (cCmd == 'X') { translate_routine(); } else if (cCmd == 'Q') { bQuit = true; } else { printf( "Invalid command\n"); } // if user hasn't asked to quit, // go through whole cycle again if (!quit) { get_command(); } }
switch (<selector>) { case <label 1>: <statements 1> break; case <label 2>: <statements 2> break; ... case <label n>: <statements n> break; default: <default statements> }
switch (iData % 3) { case 0: printf("%d is divisible by 3", iData); break; case 1: printf("%d-1 is divisible by 3", iData); break; case 2: printf("%d-2 is divisible by 3", iData); break; default: printf( "ERROR: it should not be possible)"; printf( " for (%d %% 3) to be anything but ", iData); printf( " 0, 1, or 2"); }
// course numbers from JCU's old CS program switch (iCourseNumber) { case 1010: printf( "Intro to Multimedia"); break; case 1030: printf( "Intro to Information Technology"); break; case 1200: printf( "Intro to Computer Science I"); break; case 1300: printf( "Intro to Computer Science II"); break; case 1500: printf( "Information Systems"); break; default: printf( "Invalid subject number"); }
x == 2
switch (iData) { case 1: printf( "the value entered is one\n"); case 2: printf( "the value entered is two\n"); case 3: printf( "the value entered is three\n"); default: printf( "the value entered is not in 1..3"); }The output will be:
the value entered is two the value entered is three the value entered is not in 1..3
bool bQuit = false; char cCmd; printf( "Please enter your next command\n"); printf( " A to add new entry\n"); printf( " L to look up an entry\n"); printf( " Q to quit\n"); scanf("%c", &cCmd); switch (cCmd) { case 'a': case 'A': add_entry(); break; case 'l': case 'L': lookup_entry(); break; case 'q': case 'Q': bQuit = true; break; default: printf( "Invalid command entered"); }Note how we left out the break statements to allow upper and lower case commands to be treated the same