who wrote it?
BC Jean and Toby Gad
If I Were a Boy
"If I Were a Boy" was written by BC Jean and Toby Gad. It is the opening ballad of the "I Am ..." disc of the double album "I Am... Sasha Fierce". 
when did if i were a boy come out October 13, 2008
wiki:11143069
If I Were a Boy [SEP] Writing and production
"If I Were a Boy" was written by BC Jean and Toby Gad. It is the opening ballad of the "I Am ..." disc of the double album "I Am... Sasha Fierce". It is the only song on either disc that Beyoncé did not co-write. Most of the lyrics were written by Jean, who was inspired by the break-up of a romantic relationship. One day Jean and Gad visited a pizzeria in Times Square in New York City. Although tempted, Jean refrained from eating pizza because she was dieting. She then thought that if she were a boy, she would have eaten without regret.