0% found this document useful (0 votes)
37 views

Name: Sun Shine B. Samoranos 3Bsact/B DATE: 03/13/18 Assignment

While loops and for loops are control flow statements that allow code to be repeated. A while loop repeats code based on a Boolean condition, and can be thought of as a repeating if statement. A for loop specifies iteration, allowing code to be executed repeatedly according to its conditions. Both loops were demonstrated with Java code examples that calculated a running sum by repeatedly prompting the user for integer input until a zero was entered.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views

Name: Sun Shine B. Samoranos 3Bsact/B DATE: 03/13/18 Assignment

While loops and for loops are control flow statements that allow code to be repeated. A while loop repeats code based on a Boolean condition, and can be thought of as a repeating if statement. A for loop specifies iteration, allowing code to be executed repeatedly according to its conditions. Both loops were demonstrated with Java code examples that calculated a running sum by repeatedly prompting the user for integer input until a zero was entered.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

NAME: SUN SHINE B.

SAMORANOS 3BSAcT/B DATE: 03/13/18


ASSIGNMENT

WHILE LOOP - is a control flow statement that allows code to be executed repeatedly based
on a given Boolean condition. The while loop can be thought of as a repeating if statement.

import javax.swing.JOptionPane;
public static void main(String[] args) {
String dataString = JOptionPane.showInputDialog( "Enter an int
value:\n(the program exits if the input is 0)"); int data =
Integer.parseInt(dataString);
int sum = 0;
while (data != 0) {
sum += data;
dataString = JOptionPane.showInputDialog( "Enter an int value:\n(the
program exits if the input is 0)");
JOptionPane.showMessageDialog(null, "The sum is " + sum);
}
}
FOR-LOOP - a for-loop (or simply for loop) is a control flow statement for specifying
iteration, which allows code to be executed repeatedly.

import javax.swing.JOptionPane;
public static void main(String[] args) {
String dataString = JOptionPane.showInputDialog( "Enter an int
value:\n(the program exits if the input is 0)"); int data =
Integer.parseInt(dataString);
int sum = 0;
while ( ;data != 0; ) {
sum += data;
dataString = JOptionPane.showInputDialog( "Enter an int value:\n(the
program exits if the input is 0)");
JOptionPane.showMessageDialog(null, "The sum is " + sum);
}
}

You might also like