if statement - c# nested if/else in a for loop -
i have nested if/else statement in loop determine whether or a value valid array value. returns values fine, if if correct, still else additional 3 times. thought once equal 1 time, stop, suppose missing here.
any thoughts?
string sectionchoice; int ticketquantity; double ticketprice, totalcost; string[] section = { "orchestra", "mezzanine", "balcony", "general" }; double[] price = { 125.25, 62.00, 35.75, 55.50 }; bool isvalidsection = false; sectionchoice = getsection(); ticketquantity = getquantity(); (int x = 0; x < section.length; ++x) { if (sectionchoice == section[x]) { isvalidsection = true; ticketprice = price[x]; totalcost = calcticketcost(ticketprice, ticketquantity); console.write("\n\ntotal cost tickets are: {0:c2}", totalcost); } else console.write("\n\ninvalid entry, {0} not exsist", sectionchoice); }
when valid, returns this:
your price 32.50. invalid entry, x not exsist invalid entry, x not exsist invalid entry, x not exsist
the keyword looking break;
break
stop execution of loop inside. if inside nested loops work on innermost.
the opposite of continue
. continue
stops iteration , moves onto next.
here msdn article explaining more in depth
for (int x = 0; x < section.length; ++x) { if (sectionchoice == section[x]) { isvalidsection = true; ticketprice = price[x]; totalcost = calcticketcost(ticketprice, ticketquantity); console.write("\n\ntotal cost tickets are: {0:c2}", totalcost); break; } else console.write("\n\ninvalid entry, {0} not exsist", sectionchoice); }
Comments
Post a Comment