220 likes | 494 Views
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;
E N D
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; is equivalent to number -= 1; or can rewrite as number--;
Postfix and Prefix Modes • Examine the difference number = 4; System.out.println (number++); System.out.println (number number=number + 1 Output 4
Postfix and Prefix Modes • Examine the difference number = 4; System.out.println (number++); System.out.println (number); • Output 4 5
Postfix and Prefix Modes • Examine the difference number = 4; System.out.println (++number); • Output 5 number=number + 1 System.out.println (number
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;
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
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”);
Smarter way int count=1; while (count <=5){ System.out.println(“Assalamu’alaikum”); count++; } Set initial counter no Increase counter Stop? print yes
Check for validity int pinNo = 1234; boolean notValid = true; while ( notValid) { pin = inputDevice.typeInt(); if (pin == pinNo) notValid = false; }
What will happen??? int n = 5; while (n <10){ System.out.println(“Please stop”); n++; }
What will happen??? int n = 5; while (n <10); { System.out.println(“Please stop”); n++; }
do … while loop Structure Do { …. } while (non-stopping criteria) Loop body Boolean expression true false
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);
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;
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
Example for (int count =1;count <=5;count++) System.out.println(“Assalamu’alaikum”);
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.
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));
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(); }
Time delay for (int n=1; n < 10000;n++);