Page 213 - ComputerScience_Class_11
P. 213
8.6 GROUPING STATEMENTS IN BLOCKS
A grouping statement in programming is used to combine several statements into a single logical block. This is commonly
used in control structures like loops, conditionals or functions to execute multiple statements together under a single
condition. In most programming languages, grouping is done using curly braces { } to define a block of code.
For example,
if (x > 10) {
System.out.println("X is greater than 10");
x = x + 1;
}
Here, the two statements inside the curly braces are grouped together and will be executed if the condition x > 10 is
true. Without the grouping, only the first statement would execute.
8.7 SCOPE AND VISIBILITY OF A VARIABLE
The scope of a variable refers to the region of the code where the variable is accessible. The scope is usually defined
by where the variable is declared.
• Local Scope: A variable declared inside a function or block is said to have local scope. It is only accessible within that
function or block.
• Global Scope: A variable declared outside of any function or block is said to have global scope. It can be accessed
by any function or block in the program.
8.7.1 Visibility of a Variable
Visibility refers to whether a variable can be accessed from other parts of the program, based on its scope.
• Public Visibility: If a variable is declared as public (in object-oriented languages like Java), it can be accessed from
anywhere in the program. However, this is generally not recommended for encapsulation reasons.
• Private Visibility: If a variable is declared as private, it is only accessible within the class where it is defined. This
ensures better data security and encapsulation.
• Protected Visibility: A variable declared as protected can be accessed within the same package or by subclasses,
providing more restricted access than public but more open than private.
Some More Programs #Creativity & Innovativeness
#Interdisciplinary
Program 1 Write a program in Java to input three numbers and print the second greatest number.
1 import java.util.*;
2 class secondgreatest_number
3 {
4 public static void main(String args[])
5 {
6 Scanner sc= new Scanner(System.in);
7 double a,b,c;
8 System.out.print("Enter three numbers :");
9 a= sc.nextDouble();
Statements and Scope 211

