Conditional expressions

Conditional expression: Print negative or positive.

Create a conditional expression that evaluates to string “negative” if userVal is less than 0, and “non-negative” otherwise. Example output when userVal = -9 for the below sample program:

-9 is negative.
#include <stdio.h>
#include <string.h>

int main(void) {
 char condStr[50];
 int userVal;

userVal = -9;

strcpy(condStr, userVal>=0 ? "non-negative" : "negative");

printf("%d is %s.\n", userVal, condStr);

return 0;
}

Conditional assignment.

Using a conditional expression, write a statement that increments numUsers if updateDirection is 1, otherwise decrements numUsers. Ex: if numUsers is 8 and updateDirection is 1, numUsers becomes 9; if updateDirection is 0, numUsers becomes 7. Hint: Start with “numUsers = …”.

#include <stdio.h>

int main(void) {
 int numUsers;
 int updateDirection;

numUsers = 8;
 updateDirection = 1;


 if(updateDirection==1){
 numUsers=numUsers+updateDirection; 
 }
 if(updateDirection==0){
 numUsers=numUsers-1; 
 }


 printf("New value is: %d\n", numUsers);

return 0;
}