COM01 Week11
COM01 Week11
With a do-while statement, the body of the loop is executed first and
the logical expression is checked after the loop body is executed.
The do-while statement always executes the loop body at least once
WHILE
do
{
Statement 1;
} while (condition);
WHILE
Example 1
public class name {
public static void main (String [] arg){
int num = 0;
do
{
System.out.println(num);
num++
} while (num < 0 )
}
}
NESTED LOOP
Example 1
public class name {
public static void main (String [] arg){
for (int ctr = 1; ctr < 3; ctr++)
{
int count = 1;
while(count < 2)
{
System.out.println(“Hi”);
count++;
}
}
System.out.println(“Hello”);
}
}
SAMPLE PROBLEM
1. Write a program to print numbers from 1 to 10.
2. Write a program to calculate the sum of first 10 natural number.
3. Write a program that print the first 10 even integer.
4. Write a program that print the first 20 odd integer.
5. Write a program that prompts the user to input a positive integer. It
should then print the multiplication table of that number.
Ex: 1 x 1 = 1
1x2=2
1x3=3…
(for and while)