1 / 22

Looping

Looping. Lecture Week 5. Topic. Increment Operator while loop for loop Do …. While loop. Increment Operator. ++ and – are operators that add and subtract 1 For example number = number + 1; is equivalent to number += 1; or can rewrite as number++; Or number = number - 1;

emily
Download Presentation

Looping

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. Looping Lecture Week 5

  2. Topic • Increment Operator • while loop • for loop • Do …. While loop

  3. Increment Operator • ++ and – are operators that add and subtract 1 • For example number = number + 1; is equivalent to number += 1; or can rewrite as number++; • Or number = number - 1; is equivalent to number -= 1; or can rewrite as number--;

  4. Postfix and Prefix Modes • Examine the difference number = 4; System.out.println (number++); System.out.println (number number=number + 1 Output 4

  5. Postfix and Prefix Modes • Examine the difference number = 4; System.out.println (number++); System.out.println (number); • Output 4 5

  6. Postfix and Prefix Modes • Examine the difference number = 4; System.out.println (++number); • Output 5 number=number + 1 System.out.println (number

  7. What is the value of x and y? a. int x = 1,y; y = x++; // y=x; x=x+1; b. int x = 1, y; y = ++x; // x=x+1; y=x;

  8. while loop • Loop makes a program seem powerful. • Loop is a structure that allows repeated actions. • General format : while (boolean expression) statement; Boolean expression Loop body true false

  9. Example • Print salam 5 times System.out.println(“Assalamu’alaikum”); System.out.println(“Assalamu’alaikum”); System.out.println(“Assalamu’alaikum”); System.out.println(“Assalamu’alaikum”); System.out.println(“Assalamu’alaikum”);

  10. Smarter way int count=1; while (count <=5){ System.out.println(“Assalamu’alaikum”); count++; } Set initial counter no Increase counter Stop? print yes

  11. Check for validity int pinNo = 1234; boolean notValid = true; while ( notValid) { pin = inputDevice.typeInt(); if (pin == pinNo) notValid = false; }

  12. What will happen??? int n = 5; while (n <10){ System.out.println(“Please stop”); n++; }

  13. What will happen??? int n = 5; while (n <10); { System.out.println(“Please stop”); n++; }

  14. do … while loop Structure Do { …. } while (non-stopping criteria) Loop body Boolean expression true false

  15. Example int count=1; while (count <=5){ System.out.println(“Assalamu’alaikum”); count++; } int count=1; do { System.out.println(“Assalamu’alaikum”); count++; } while (count <5);

  16. Example int pinNo = 1234; boolean notValid = true; while ( notValid) { pin = inputDevice.typeInt(); if (pin == pinNo) notValid = false; } int pinNo = 1234; do { pin = inputDevice.typeInt(); } while pin != pinNo;

  17. for loop • structure when definite number of iterations is required. for (start counter; stopping criteria; increment counter) loop body initialization test update Set initial counter no Loop body Increase counter Stop? yes

  18. Example for (int count =1;count <=5;count++) System.out.println(“Assalamu’alaikum”);

  19. write the code to read a selection from a food menu • 1. nasi lemak • 2. roti canai • 3. lontong • 4. nasi dagang • Sila masukkan pilihan anda.

  20. int choice; do { System.out.println(“1.Nasi lemak”); System.out.println(“2.Roti canai”); System.out.println(“3. Lontong”); System.out.println(“4. Nasi dagang”); System.out.println(“Sila masukkan pilihan anda”); choice = inputDevice.nextInt(); } while ((choice <1) II (choice >4));

  21. int choice = 999; while ((choice <1) II (choice >4)) { System.out.println(“1.Nasi lemak”); System.out.println(“2.Roti canai”); System.out.println(“3. Lontong”); System.out.println(“4. Nasi dagang”); System.out.println(“Sila masukkan pilihan anda”); choice = inputDevice.nextInt(); }

  22. Time delay for (int n=1; n < 10000;n++);

More Related