CS1102 Unit 2 Discussion sample 2 PDF

Title CS1102 Unit 2 Discussion sample 2
Author Kute Face
Course Programming 1
Institution University of the People
Pages 6
File Size 137.9 KB
File Type PDF
Total Downloads 147
Total Views 191

Summary

While LoopA "While" Loop is used to repeat a specific block of code an unknown number of times, untila condition is met. For example, if we want to ask a user for a number between 1 and 10,we don't know how many times the user may enter a larger number, so we keep asking"w...


Description

While Loop A "While" Loop is used to repeat a specific block of code an unknown number of times, until a condition is met. For example, if we want to ask a user for a number between 1 and 10, we don't know how many times the user may enter a larger number, so we keep asking "while the number is not between 1 and 10".

Example of a “while” loop: package com.cs1102unit2; public class Main { public static void main(String[] args) { // example of "while loop" int a = 0; while (a < 10) System.out.println(a = a + 1); System.out.println("Operation Completed Successfully"); } }

Example of “For” loop performing the function of the “while” loop above. package com.cs1102unit2; public class Main { public static void main(String[] args) { // Example of “For” loop performing the function of the “while” loop. for (int a = 0; a < 10; a = a +1 ){ System.out.println(a); } System.out.println("Operation Completed Successfully"); } }

Example of “do while” loop performing the function of the “while” loop. package com.cs1102unit2; public class Main { public static void main(String[] args) { // Example of “do while” loop performing the function of the “while” loop. int a = 0; do { System.out.println(a = a +1); } while (a < 10);

System.out.println("Operation Completed Successfully"); } }

Do While Loop A “do while” loop is a control flow statement that executes a block of code at least once, and then repeatedly executes the block, or not, depending on a given Boolean condition at the end of the block.

So, the only difference between While loop and Do while loop is that the while loop can end without executing any statement and Do while loop will end only after executing one statement.

Example of “do while” loop. package com.cs1102unit2; public class Main { public static void main(String[] args) { // Example of “do while” loop. int count = 1; do { System.out.println("Count is: " + count); count++; } while (count < 11); System.out.println("Operation Completed Successfully"); } }

Example of “while” loop performing the function of "do while" loop. package com.cs1102unit2; public class Main { public static void main(String[] args) { // Example of “while” loop performing the function of "do while" loop. int count = 1; while (count < 11) System.out.println("Count is: " + count++);

System.out.println("Operation Completed Successfully"); } }

Example of “for” loop performing the function of "do while" loop. package com.cs1102unit2; public class Main { public static void main(String[] args) { // Example of “for” loop performing the function of "do while" loop. for (int count = 1; count < 11; count++) System.out.println("Count is: " + count );

System.out.println("Operation Completed Successfully"); } }

For Loop In computer science, a for-loop (or simply for loop) is a control flow statement for specifying iteration, which allows code to be executed repeatedly if we (or the computer) knows exactly how many times to execute a section of code (such as shuffling a deck of cards) we use a for loop.

Example of “For” loop package com.cs1102unit2; public class Main { public static void main(String[] args) { // Example of “for” loop. //for loop for(int x = 1; x 1, decrease is i— With this example the loop will execute by an increment of -1 from 6 to 1. Loops can be controlled on entry or exit depending on the loop chosen by the programmer. A for

Reference(s): Anup (2018), What are the advantages and disadvantages of while loop, do while loop, for loop? Retrieved from: https://www.quora.com/What-are-the-advantages-anddisadvantages-of-while-loop-do-while-loop-for-loop Loops in Java, Retrieved from: https://www.javatpoint.com/java-for-loop...


Similar Free PDFs