11- Scope of code blocks and variables

table of Contents

  • The braces are the code block
  • Variable scope
  • Understand scope and namespace
  • for loop nesting

1. The code block enclosed in braces

  • Example of code block
  • Named code block-if-else code block, for loop code block, main method code block
  • Code block is also called body, such as for loop body, main method body
  • Code block to nest

2. The scope of variables

  • Create and use variables in code blocks
    • Variables in the outer code block can be used in the code block
    • Create variables in code blocks
    • The variables of the inner code block cannot be used in the outer code block. Whether variables can be used is also called the visibility of variables in a certain code block. In other words, the variables created by the outer code block are visible to the inner code block. Variables created in the inner code block are not visible to the outer code block.
    • The inner namespace cannot repeatedly define the variables of the outer code block, but the variables of the outer code block can be used
    • No matter how many levels the code block is nested, it will comply with the above variable visibility
public class CodeBlock {
    
    
    public static void main(String[] args) {
    
    
        int outer = 100;

        {
    
    
            int inner = 10;
            System.out.println("outer变量的值是" + outer + "。inner变量的值是" + inner + "。");
        }

        // System.out.println(inner); // 找不到 inner

        int a1 = 1;
        {
    
    
            int a2 = a1 + 1;
            {
    
    
                int a3 = a2 + 1;
            }
            {
    
    
                int a3 = a2 + 10;
            }
            System.out.println(a2);
        }
    }
}
outer变量的值是100。inner变量的值是102

3. Understanding scope and namespace

  • Scope and namespace
    • Variables in the same namespace cannot have the same name
    • In order to avoid variable name conflicts, there must be a namespace

4. For loop nesting

  • Calculate the multiplication table
    • Multiply two numbers, the outer loop represents the multiplier, and the inner layer is the multiplicand.
    • Loop nesting, variable names cannot be repeated.
    • Use the break statement to make the output multiplication table more concise.
    • Use String variable to do String addition.
public class MultiTable {
    
    

    public static void main(String[] args) {
    
    
        for (int i = 1; i <= 9; i++) {
    
    
            String line = "";
            for (int j = 1; j <= 9; j++) {
    
    
                if (i < j) {
    
    
                    break;
                }
                line = line + j + "*" + i + "=" + (j * i) + "\t";
            }
            System.out.println(line);
        }
    }
}
1*1=1	
1*2=2	2*2=4	
1*3=3	2*3=6	3*3=9	
1*4=4	2*4=8	3*4=12	4*4=16	
1*5=5	2*5=10	3*5=15	4*5=20	5*5=25	
1*6=6	2*6=12	3*6=18	4*6=24	5*6=30	6*6=36	
1*7=7	2*7=14	3*7=21	4*7=28	5*7=35	6*7=42	7*7=49	
1*8=8	2*8=16	3*8=24	4*8=32	5*8=40	6*8=48	7*8=56	8*8=64	
1*9=9	2*9=18	3*9=27	4*9=36	5*9=45	6*9=54	7*9=63	8*9=72	9*9=81	

Extracurricular supplement:

Python code implements the nine-nine multiplication table.

for i in range(1, 10):
	line = ""
	for j in range(1, 10):
		if i < j:
			break
		line = line + f"{j} * {i} = {i * j}\t"
	print(line)

Guess you like

Origin blog.csdn.net/qq_33254766/article/details/108911047