001-006-017
Scope in Nested Blocks
Medium
Problem Description
Scope in Nested Blocks
In this problem, you will create a program that verifies the difference in variable scope inside and outside a nested block (if block), calculates the sum of an outer and inner variable, and displays the results to standard output.
Learning Objective: Understand variable scope in nested blocks
Overview
Variables declared in an outer block can be used in inner blocks, but variables declared in inner blocks cannot be used outside. Verify this behavior.
Specifications
- Initialize variable
outerwith 100 in main method - Inside an if block, initialize variable
innerwith 50 and calculate the sum ofouterandinner - Output the sum inside the if block
- Output
outer's value outside the if block
Output Format
Sum: 150
Outer: 100
Ready to Try Running Code?
Log in to access the code editor and execute your solutions for this problem.
Don't have an account?
