In the previous lesson, we discuss the while loop statement, on how it be executed plus we give you a program example in which you look at for your references. so now moving to our next lesson (for-loop) the "for-loop" statement is a shortcut for "while-loop" check out the program below.
in this case, the for-loop is executed because "i" is less than or equal to 10. the body of the "for-loop" is executed in each loop repetition/iteration and prints "i" After each repeat "i" is increased and Guess what the program prints?
it prints the numbers from 0 to 10. The same could be done with the "while-loop" How-ever the "for-loop" is shorter. Now we make the program print the number 0 to 20 look at the for-loop statement we've added above. The program should print the numbers "0" up to "20" you may also change the loop condition to check whether "i" is less than or equal to 20. if you got it right then the program should print from "0" to "20" now for your activity to make a program print the numbers from 20 to 40 then run it with your compiler. The program should print numbers 20 to 40 if you got it Hint: change the initial value of "i" as well as the loop condition. For your another activity write a (for-loop) statement that prints the numbers from 20 down to 1 if you got it right the program should print from "20" down to "1" congratulation we are done for this tutorial today see you on my next lesson.
in this case, the for-loop is executed because "i" is less than or equal to 10. the body of the "for-loop" is executed in each loop repetition/iteration and prints "i" After each repeat "i" is increased and Guess what the program prints?
it prints the numbers from 0 to 10. The same could be done with the "while-loop" How-ever the "for-loop" is shorter. Now we make the program print the number 0 to 20 look at the for-loop statement we've added above. The program should print the numbers "0" up to "20" you may also change the loop condition to check whether "i" is less than or equal to 20. if you got it right then the program should print from "0" to "20" now for your activity to make a program print the numbers from 20 to 40 then run it with your compiler. The program should print numbers 20 to 40 if you got it Hint: change the initial value of "i" as well as the loop condition. For your another activity write a (for-loop) statement that prints the numbers from 20 down to 1 if you got it right the program should print from "20" down to "1" congratulation we are done for this tutorial today see you on my next lesson.
0 Comments