1 / 6

Chapter 4 Practice

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.

sbrookins
Télécharger la présentation

Chapter 4 Practice

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 4 Practice

  2. 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);

  3. 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);

  4. 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++;}

  5. 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.

  6. 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.

More Related