Welcome fellow students, register yourself and dive into asking questions, giving answers and helping the community learn with you.

does: if(!swaps)/break, mean if theres no swaps break?

+1 vote
asked Mar 3, 2015 in Pset3 by bezerk01 (530 points)
This is for the bubble swap week 3 section. She says if there's a swap, swaps++ (swaps increases by 1) but does: if(!swap) {break;} mean that once it goes through an iteration and there's no swaps to be made that it will break? Just making sure.

 

Thanks, as always.

1 Answer

0 votes
answered Mar 4, 2015 by Faïza Harbi (11,960 points)
selected Mar 8, 2015 by bezerk01
 
Best answer

If there is no swap to be made you have two options:

  1. You are trying to  sort an empty or legnth  list/array (which swap usually takes care of, as theey are corner cases)
  2. Or you are trying to sort an already sorted list/array.
  3. This one being, something seriously screwe up int he process, but quite unlikely. I'd go for 1 or 2.

So yeah, if swap is one way or another not needed or not useful , breaking out is the good thing to do: You get out of that if.

Hope I helped a bit. ^______^

commented Mar 5, 2015 by bezerk01 (530 points)
It took me a little time to comprehend but I get it!
Thanks :)
commented Mar 6, 2015 by Faïza Harbi (11,960 points)
You're welcome  ^__^
Welcome to CS50xHelpers Q&A, where you can ask questions and receive answers from other members of the community.

190 questions

248 answers

217 comments

3,602 users

1 Online
0 Member And 1 Guest
...