This program displays certain messages when some of the elements are 'y' or if all of the elements are 'n'.
My question is about this line: someElements |= array[i] == 'y'.
I understand that it can be written in this way: someElements = someElements | array[i] == 'y'.
I'm just asking for an explanation why does it work?
#include <stdio.h>
int main()
{
char array[3] = { 0 };
int i;
for (i = 0; i < 3; i++) {
do {
printf("\nElement No.%d [y/n]: ", i + 1);
scanf(" %c", &array[i]);
if (array[i] != 'y' && array[i] != 'n') {
printf("Must be a lowercase 'y' or 'n'\n");
}
} while (array[i] != 'y' && array[i] != 'n');
}
int someElements = 0;
for (i = 0; i < 3; i++) {
someElements |= array[i] == 'y';
}
if (someElements) {
printf("\nSOME of the elements = y.\n");
}
else{
printf("\nNONE of the elements = y.\n");
}
return 0;
}