Question:
Can somebody explain to me how a loop works?
Justin
2010-10-16 16:55:53 UTC
In this program

import javax.swing.JOptionPane;
import java.util.Arrays;


public class Security
{

public static void main( String[] args)
{
int Msigma = 8;
int Nsigma = 7;
int Osigma = 10;
double[] force = {21,25,27,45,52,71,78,96}; //Number of nodes on network connected directly to server
double[] consequence = {32,26,34,22,92,51,65};//Number of intrusions
double[] reference = {21,25,32,41,26,46,56,35,17,12};//Number of internetworked connections
double fsum = 0;
double csum = 0;
double rsum = 0;
int alpha = 0;
int beta = 0;
double wpi;
String AnswerString;
String AlphaString;
String BetaString;

AlphaString = JOptionPane.showInputDialog("Enter number between 10 and 25");
BetaString = JOptionPane.showInputDialog("Enter number between 0 and 10");

alpha = Integer.parseInt( AlphaString );
beta = Integer.parseInt( BetaString );

//Summations

for (int i = 0; i < Msigma; i++)
{
fsum += force[i];
}
for (int j = 0; j < Nsigma; j++)
{
csum += consequence[j];
}
for (int k = 0; k < Osigma; k++)
{
rsum += reference[k];
}

fsum += alpha;
csum += beta;

wpi = (fsum + csum) / rsum;

AnswerString = "Wpi is " + wpi;

JOptionPane.showMessageDialog(null, AnswerString);
}
}

How does a loop works...I haven't studied it yet. This is a code for a assignment my group was supposed to do. Can somebody explain to me please?
Three answers:
Hannah
2010-10-16 17:04:15 UTC
You need to study it. Basically, loops are a method of selection, as opposed to sequence (where the code runs from line 1 downward). With selection (i.e. loops) you can stop the program at a certain line and make it rerun a series of lines either until a condition is no longer true (while, do...while loop) or for a fixed amount of times (for loop).



But you need to read up on this yourself. It's quite a large topic to answer on.
?
2010-10-16 19:17:55 UTC
It looks like the arrays are implemented simply to calculate the sum of a few lists of integers. All three loops perform the same summation function with different input. For example:



for (int i = 0; i < Msigma; i++)

{

fsum += force[i];

}





this loop adds each element in the array "force" that has an index less than Msigma to "fsum", which starts at zero . it does this by looping through the list and adding each element individually to the accumulated sum so far, which is stored in fsum.



loops are an essential tool in most programming languages. You should really learn exactly how they work in the (near) future.
greenwald
2016-12-15 11:51:59 UTC
Your question would not make lots sense different than convert to java. for the reason that i do no longer understand java, i assume i can't assist you to. C code noticeably lots is going from suitable to backside while it includes looping. So the 1st loop on the suitable starts to loop first, even nevertheless it finishes final, the indoors loops initiate and comprehensive n cases.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...