The bit of code you lack is surely meant to change or keep is_c_in_s3. Surely it is if(/*some condition*/) is_c_in_s3=false;. You really need to figure that condition out yourself. It's to understand the basics of loops.
Note, also, that the name is_c_in_s3 is misleading. In a union operation, you only need to add an element if it isn't present yet.