Is there a way I could make this pseudocode/semicode work? for (int x = 0; if (width % 2) {x < (width-1)/2)}; else {x < width/2}; x++) Essentially, I want to stick an if-else statement within the conditions of a for loop (do different things for odd and even lengths). (The language is C++)
(oops, to clarify, I want to do this without putting the if-else statement out of the conditions of the for loop, so I don't have to type repeat code for the inside of the for loop <-- that's the last resort)
I figured out a solution, though not quite what I originally planned: for (int x = 0; x < (width-(width % 2))/2; x++) If anyone knows if there's a way I can still stick one within another, I'd still be interested.
void main() { int width = 0, x = 0; cout << "Enter the width!" cin >> width; cout << "Enter the x" cin >> x; int size = width - (width % 2) do { x++ } while(x < size/ 2) } This does the same as you did, only this way is more comprehendsive and readable :) You just wanted to iterate your code, when you don't exactly know how many times is goint to itearte, we use a while loop, in this case a do while, since we need to test first and then check the condition. You avoid repeating code, using variables and functions, if needed. Since your code didn't had any other purpose other than iterate when x is < the width minus the same modulus, and if you use pair numbers, it will always give 0 modulus. Give names to variables that makes sense, that tells from forst sight what is it, give name to fucntions that make sense too, like verbs. Hope this code helps ya :P
for (int x = 0; {if (width % 2) {x < (width-1)/2)}; else {x < width/2}}; x++) try this
o for (int x = 0; (if (width % 2) {x < (width-1)/2)}; else {x < width/2}); x++)
remember always to make things with a purpose, and as long as you them, please be precise, and indent so others as well you can see normally what the code says more clearly, I got a book for you so you write cleaner code: Clean_Code_-_A_Handbook_of_Agile_Software_Craftsmanship by Robert C. Martin When I saw your code, even if something simple, by you qriting in such way, it appeared to be more complex, because you cramp everything there and didn't had clearly the purpose fof such code... here is a representation of my perspective to your code... Remember the cleaner the code you write, the better for you and your peers to see what's behind the code :)
Join our real-time social learning platform and learn together with your friends!