Write test programs in java to determine the scope of a variable declared in a for statement. Specifically, the code must determine whether such a variable is visible after the body of the for statement

Respuesta :

Answer:

  1. public class Main {
  2.    public static void main (String [] args) {
  3.        for(int i = 1; i < 10; i++){
  4.            int num = 0;
  5.            num += i;
  6.        }  
  7.        System.out.println(num);
  8.    }
  9. }

Explanation:

In programming each variable has its scope. For example, a variable defined within the for loop has no longer exist outside the loop body. To illustrate this we can write a short program as presented above.

Firstly, create a for loop that traverse the number 1 - 10 (Line 5 - 8). Within the loop create a variable num and initialize it with zero (Line 6) and increment it with i value for each iteration (Line 7).

Outside the loop, we try to print the num (Line 9). When we run the program this will result in an error as the num which is declared inside the for loop will no longer exist outside the loop body.  

In this exercise we have to use the knowledge of computational language in JAVA  to describe a code, like this:

The code can be found in the attached image.

To make it easier the code can be found below as:

public class Main {

  public static void main (String [] args) {

      for(int i = 1; i < 10; i++){

          int num = 0;

          num += i;

      }  

      System.out.println(num);

  }

}

See more about JAVA at brainly.com/question/26104476

Ver imagen lhmarianateixeira