60 likes | 80 Views
Chapter 4 Practice. Practice with ++, -- operator. What will be the output of the following program segment: x = 2; y = x++; System.out.println(y); x = 2; System.out.println(x++); x = 8; y = x--; System.out.println(y);. Practice with ++, -- operator.
E N D
Practice with ++, -- operator • What will be the output of the following program segment: • x = 2;y = x++;System.out.println(y); • x = 2;System.out.println(x++); • x = 8;y = x--;System.out.println(y);
Practice with ++, -- operator • What will be the output of the following program segment: • x = 2;y = 2*--x + x++;System.out.println(y); • x = 2;y = 2*++x + x--;System.out.println(y);
Practice with while statement • How many times will “Hello” be printed in the following program segment? int count = 10;while (count < 1) { System.out.println(“Hello”); count++; } • How many times will “Hello” be printed in the following program segment? int count = 0;while (count < 10) { System.out.println(“Hello”);} • How many times will “Hello” be printed in the following program segment? int count = 0;while (count < 10) { System.out.println(“Hello”); count++;}
Practice with while/do-while statement • Write an input validation loop that asks the user to enter a number representing the age of a teenager (in the range of 13 through 19). • Write an input validation loop that asks the user to enter letter ‘Y’, ‘y’, ‘N’, or ‘n’. • Write an input validation loop that asks the user to enter a string “Yes” or “No”. • Write an input validation loop that asks the user to enter a number representing a leaf year. • Write an input validation loop that asks the user to enter a string having the length of at least 6 characters.
Practice with for statement • What will the following program segment display? • for (int c = 0; c < 6; c++) System.out.println(c + c); • for (int c = -5; c < 5; c++) System.out.println(c); • int c;for (c = 5; c <=14 ; c+=3) System.out.println(c); System.out.println(c); • Write a for loop that displays your name 10 times. • Write a for loop that displays all numbers between 1 and 100, and ending with 0 or 5. • Write a for loop that displays all powers of 2 between a and b, where a and b are natural numbers entered from keyboard, a < b.