For your LinearSearch function did you consider making the return type a nullable int? Then you return the position of where the item was found and if not found it returns null.
@chadgregory90372 жыл бұрын
would people in production prefer a for loop like you show in the video, or how would people feel about this? LinearSearch(array, key) { int i = 0: while (i < array.Length) { array[i] == key ? return true : i++; } return false; }
@TeddySmithDev2 жыл бұрын
That’s fancy. I would be impressed because I think most lean toward for loops.
@CRBarchager2 жыл бұрын
@@TeddySmithDev for-loop I if needed to something with the index or just run through the whole thing with a foreach instead. Under the covers it's all converted to a while loop.
@g_man9662 Жыл бұрын
This actually won't compile. a ternary operator is trying to return some sort of value. This is kinda like trying to put a return on the right side of an = sign.
@Fork0 Жыл бұрын
I think using a while loop for this exact situation is just plain bad. A for loop in this specific situation has everything you need and does everything automatically. A while loop forces you to be very careful with the incrementation. If you suddenly need to add a "continue" somewhere, you have to make sure you're adding the "i++" or your loop will be bugged. There's a time and place for "while" loops and sometimes you need to handle multiple varibles and their increments/decrements, but honestly in this example, I'd always use a for loop.