Yes. The scope of a variable is determined by where it is declared. If it is only valid inside the loop it was declared in.
What you probably want to do is to declare the variable outside of the loop. I don't see where you are declaring sumOf2 or sumOf3 at all, but if they are declared before the while loop they should be able to be used in other parts of the code.
2007-11-27 03:59:56
·
answer #1
·
answered by Bobby 3
·
0⤊
0⤋
To answer your question, you cannot use a counter type variable declared for a 'for' loop outside of the loop, but you can use it inside, in a nested loop, if necessary. Generally, if you have variables that need to be accessed in mutiple parts of a program, you declare them 'globally', at the top of your program in the main declarations section. Or, if they will only be used within a particular procedure, you can declare them at the beginning of the procedure. It is best to avoid using too many global variables, and instead use either local ones, or create properties that can be accessed by using the 'get' and 'set' statements - that way it is easier to maintain the integrity of the data they hold. Good luck.
2007-11-27 04:12:22
·
answer #2
·
answered by Anonymous
·
0⤊
0⤋
You cannot use the variables declared in a for loop outside the loop. This is true with all loops. If you need it value to be available outside then declare outside the loop.
Why you need to declare it inside?? any reasons???
2007-11-27 04:01:00
·
answer #3
·
answered by http://www.itgalary.com 3
·
0⤊
0⤋
What you need to consider is the "scope" of variables (what you termed "invisible"). If you declare a variable inside that block of code, it won't be visible when you leave that block of code.
If you need to keep the variable around, consider something like:
// Initialize variable outside
int myInt = 0;
String myStr = null;
for (int i = 0; i < 11; i ++) {
// This badVar will through a NullPointerException if you try to reference it outside this code block
int badVar = 123;
// Changing the variable
myInt = i;
// repeatedly initiallizing myStr
myStr = new String("Hello #" + myInt);
System.out.println(myStr);
}
// Will print out "Hello #11" as myStr is still in scope and has a value.
System.out.println(myStr);
// Will through a NullPointerException
System.out.println("Bad variable: " + badVar);
2007-11-27 04:05:48
·
answer #4
·
answered by Jim Maryland 7
·
0⤊
0⤋
Make a loop that iterates by each and every character on your string (convert your string into an array and examine each and every fee right into a char). If the letter is a consonant, increment the consonants integer through a million. Do the comparable for vowels and words. For areas, count style the style of areas and upload one.
2016-10-18 05:43:39
·
answer #5
·
answered by sander 4
·
0⤊
0⤋
What is the concept and aim of your program??
2007-11-27 04:00:14
·
answer #6
·
answered by Chirag Agarwal 1
·
0⤊
0⤋
complex problem. browse onto the search engines. this might help!
2015-04-29 17:05:45
·
answer #7
·
answered by Kristi 2
·
0⤊
0⤋