Class 12 – Information Technology (Java Basics to Loops)
1. Introduction to Java
Java is a high-level, object-oriented programming language. It is platform-independent because of the JVM (Java
Virtual Machine). Famous for the principle: Write Once, Run Anywhere (WORA).
2. Structure of a Java Program
class HelloWorld {
public static void main(String[] args) {
[Link]("Hello, World!");
}
}
Explanation: Every program has a class, a main() method, and statements inside it.
3. Variables and Data Types
int age = 17; // integer
double marks = 92.5; // decimal
char grade = 'A'; // single character
String name = "Aman"; // text
boolean pass = true; // true/false
Example of Input using Scanner:
import [Link];
class InputExample {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter your name: ");
String name = [Link]();
[Link]("Hello " + name);
}
}
4. Decision Making (if-else)
int marks = 75;
if (marks >= 33) {
[Link]("Pass");
} else {
[Link]("Fail");
}
5. Loops in Java
Loops are used to repeat tasks.
While Loop:
int i = 1;
while (i <= 5) {
[Link]("Number: " + i);
i++;
}
Do-While Loop:
int i = 1;
do {
[Link]("Number: " + i);
i++;
} while (i <= 5);
For Loop:
for (int i = 1; i <= 5; i++) {
[Link]("Square of " + i + " = " + (i*i));
}
6. Nested Loops
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 5; j++) {
[Link]("* ");
}
[Link]();
}
7. Break and Continue
Break Example:
for (int i = 1; i <= 10; i++) {
if (i == 5) break;
[Link](i);
}
Continue Example:
for (int i = 1; i <= 5; i++) {
if (i == 3) continue;
[Link](i);
}
8. Practice Questions
Q1. Sum of first 10 natural numbers
int sum = 0;
for (int i = 1; i <= 10; i++) {
sum += i;
}
[Link]("Sum = " + sum);
Q2. Check Prime Number
Scanner sc = new Scanner([Link]);
int n = [Link]();
boolean prime = true;
for (int i = 2; i <= n/2; i++) {
if (n % i == 0) {
prime = false;
break;
}
}
if (prime && n > 1)
[Link]("Prime");
else
[Link]("Not Prime");
Q3. Multiplication Tables (2 to 5)
for (int t = 2; t <= 5; t++) {
[Link]("Table of " + t);
for (int i = 1; i <= 10; i++) {
[Link](t + " x " + i + " = " + (t*i));
}
[Link]();
}