can somebody help me understand a nested loop using both while and for loop
A nested loop is just two or more "counters" going at the same time with one dependent on the other. In a NASCAR race, the driver is only concerned with getting through a lap, his pit coordinator is concerned with getting through the entire race. So the iteration starts, the pit starts the count, lap1, and waits for the driver to go through his conditions to finish that lap. Once the driver is done he resets for the next lap and the pit increments their race count and then waits on the driver to perform the next lap, and so on until the race is done. A programming example would be character recognition in an array. You have a char array with [h][e][l][l][o][\0] and you want to verify the chars within. for (i=0; i<5; i++){ // We start the outer loop to keep track of the index for (j=97; j<=122; j++) { // Step through each ascii value for lower case if (array[i] == j) { /* Do some stuff like print the char */ } } } So now your outside loop is stepping through the array index and your nested loop is evaluating that index against all the lower case characters. A "while" example is when your heating/cooling system is operating, while it is below 70 degrees, run the heat, meanwhile you are always checking the current temp to see if it is too hot, then while it is over 70, turn on the AC. A programming example in pseudo code: do { start game { while (user has not clicked exit) { present game menu { while (user is playing) { display graphics } } } } } while (user has not confirmed exit)
Join our real-time social learning platform and learn together with your friends!