HP C A.06.05 Reference Manual

Expressions and Operators
Logical Operators (&&, ||, !)
Chapter 5116
Example
/* Program name is "logical_ops_example". This program */
/* shows how logical operators are used. */
#include <stdio.h>
int main(void)
{
int won_lottery, enough_vacation, money_saved;
char answer;
won_lottery = enough_vacation = money_saved = 0;
printf("\nThis program determines whether you can ");
printf("take your next vacation in Europe.\n");
printf("Have you won the lottery? y or n: ");
fflush(stdin);
scanf("%c", &answer);
if (answer == 'y')
won_lottery = 1;
printf("Do you have enough vacation days saved? \
y or n: ");
fflush(stdin);
scanf ("%c", &answer);
if (answer == 'y')
enough_vacation = 1;
printf("Have you saved enough money for the trip? \
y or n: ");
fflush(stdin);
scanf("%c", &answer);
if (answer == 'y')
money_saved = 1;
printf("\n");
if (won_lottery)
{
printf("Why do you need a program to decide if you");
printf(" can afford a trip to Europe?\n");
} /* end if */
if (won_lottery || (enough_vacation &&money_saved))