Page 87 - Viva ICSE Computer Studies 8 : E-book
P. 87
Example: Write a program to print fi rst 10 natural numbers.
Do and Learn
Write a program to print the odd numbers between 1 and 10.
Steps to be Followed
public class natural
{
public static void main(String args[])
{
int a;
for (a = 1; a <=10; a = a + 2)
{
System.out.println(a);
}
}
}
Do While Loop
Do While loop is similar to the While loop, except that Do While loop is guaranteed to execute
at least one time. The test condition appears at the end of the loop, so the statements in the
loop execute once before the test condition is tested. If the test condition is ‘True’, the control
jumps back up to the Do statement, and the statements in the loop execute again. This process
repeats until the test condition is ‘False’.
Let’s understand the While loop with the syntax and an example.
75