Discussion Forum Unit 2 Progragramming 1 (CS 1102) PDF

Title Discussion Forum Unit 2 Progragramming 1 (CS 1102)
Author Collins Simwanza
Course Programming 1
Institution University of the People
Pages 8
File Size 87.8 KB
File Type PDF
Total Downloads 35
Total Views 145

Summary

Discussion Forum Unit 2 Progragramming 1 (CS 1102). The solution to the discussion unit 2 CS 1102...


Description

Discussion Forum Unity 2 CS 1102

Loop A loop is a control structure that is used to run a statement or statements repeatedly or many times. “In computer science, a loop is a programming structure that repeats a sequence of instructions until a specific condition is met. Programmers use loops to cycle through values, add sums of numbers, repeat functions, and many other things.” (TechTerms, 2016).

Advantages of loops 1 Loops in programming languages help us avoid repetitions thereby saving us time of writing the same thing repeatedly. 2 They make our code more readable thereby reducing o the complexity of the code.

Disadvantage 1 The disadvantage of looping is that they may cause a program to crash if not implemented correctly.

Having understood what a loop is, let us now explore the three (3) loops that Java uses to satisfy a particular given condition. Java uses three (3) types of loops, these are: while loop, do while loop and for loop.

While loop “Repeats a statement or group of statements while a given condition is true. It tests the condition before executing the loop body.” (Tutorialspoint, n.d.). This means that before any given statement is excuted, it will be checked first for its validity. Below is the example of the while loop and its equivalent do while loop and for loop.

public class AssignmentWhileLoop { public static void main(String[] args) { int collins = 1;

while(collins < 21) {

System.out.println(collins); System.out.println("collins"); collins++; } }

}

OUTPUT --- exec-maven-plugin:3.0.0:exec (default-cli) @ MyProject1 --1 collins 2 collins 3 collins 4 collins 5 collins 6 collins 7 collins 8 collins 9 collins 10

collins 11 collins 12 collins 13 collins 14 collins 15 collins 16 collins 17 collins 18 collins 19 collins 20 collins -----------------------------------------------------------------------BUILD SUCCESS

Equivalent(do while loop)

public class EquivalentDoWhileLoop { public static void main(String[] args) {

int collins = 1; // initialization

// The value for collins will be printed before checking the condition do { System.out.println(collins); System.out.println("collins"); collins++; // post increament

}while(collins < 21); } }

OUTPUT --- exec-maven-plugin:3.0.0:exec (default-cli) @ MyProject1 --1 collins 2 collins 3 collins 4 collins 5 collins 6 collins 7

collins 8 collins 9 collins 10 collins 11 collins 12 collins 13 collins 14 collins 15 collins 16 collins 17 collins 18 collins 19 collins 20 collins -----------------------------------------------------------------------BUILD SUCCESS

Equivalent(for loop)

public class EquivalentForLoop { public static void main(String[] args) { for(int collins=1;collins...


Similar Free PDFs