Day1-6 Java
Day1-6 Java
In Java, a variable is a named storage for data. It has a specific type (like int or
double) and must be declared before use. Variables store different kinds of values,
allowing data manipulation in programs.
// Variable initialization
myInteger = 42; // Initialize the integer variable
A data type is an attribute associated with a piece of data that tells a computer system
how to interpret its value.
3.2.1. Data Types Program
3.3. Operators
An operator is a symbol that operates on a value to perform specific mathematical or
logical computations.
// Arithmetic operators
int num2 = 5;
// Comparison operators
// Logical operators
3.4. Input
In Java, you can take user input from the command line using the Scanner
class, which is part of the java.util package. Here's a basic example of how
to take user input in Java:
Input Program
import java.util.Scanner;
scanner.close();
}
Lecture 2 : Flow of Control
1. Conditional Statements
A conditional statement in programming refers to a construct that allows you to execute
different blocks of code based on the evaluation of a condition. Conditional statements enable
you to make decisions in your programs, making them dynamic and responsive to different
situations.
1.1. if Statement:
The if statement evaluates a condition. If the condition is true, the code inside the if
block is executed.
} else {
}
1.3. else-if Statement:
The else-if statement allows you to evaluate multiple conditions in sequence. If the
previous conditions are false, it checks the next condition.
} else if (condition2) {
} else {
1.4. Switch-Case
The switch statement in Java is another way to make decisions based on the value of a
variable or expression. It provides an alternative to the if-else if-else ladder for
handling multiple conditions. Here's the basic syntax of the switch statement:
case value1:
break;
case value2:
break;
switch (number) {
case 1:
break;
case 2:
break;
case 3:
break;
case 4:
System.out.println("You entered 'Four'.");
break;
case 5:
break;
default:
switch (number) {
case 1:
case 2:
break;
case 3:
break;
case 4:
break;
case 5:
break;
default:
}}
The continue statement is used inside a loop to skip the rest of the loop's body for the
current iteration and move to the next iteration of the loop. When a continue
statement is encountered, the program control jumps to the loop's
increment/decrement expression (in for loops) or the loop condition check (in while
and do-while loops).
// Use a loop to find and display the first even number in a range
if (i % 2 == 0) {
break; // Exit the loop when the first even number is found
if (i % 3 == 0) {
System.out.println(i);
}}}
2. Loops
In Java, loops are used to execute a block of code repeatedly as long as a specified condition
is met. There are several types of loops in Java: for, while, and do-while loops. Here's an
overview of each:
// Code to be executed
// Code to be executed
int i = 1;
System.out.println(i);
}}
// Code to be executed
} while (condition);
do {
System.out.println(i);
i++;