Skip to main content

Chapter No 4 Control Statements Java Notes

 

Chapter N0 4 

                       Control Statements  

4.1. Introduction and Control Statement Types  

4.2. Conditional Statements  

4.3. Loops  

4.4. Break / Continue 

 

4.1. Control Statement Types:  

- Control statements are used in programming to control the flow of a program. There are primarily  

three types:  

1. Selection Statements:These are used to make decisions in the code. Common selection statements  

include `if`, `else if`, and `else`.  

2. Iteration Statements (Loops):These statements are used for repetitive tasks. Common iteration  

statements include `for`, `while`, `do-while`, and `foreach`.  

3. Jump Statements: These are used to alter the normal flow of the program. Common jump  

statements include `break` and `continue`.  

4.2. Expression Statements:  

- Expression statements are used to perform a task or evaluate an expression. They don’t control the  

flow of a program but can be part of control statements.  

- Example: `x = 5;` where `x` is assigned the value 5.  

4.3. Compound Statements:  

- Compound statements, also known as code blocks, group multiple statements into a single unit.  

- They are often used in control statements to execute multiple statements together.  

- Common examples include code blocks enclosed in curly braces `{ ... }`.  

4.4. If/Else Statements:  

- `if` statements are used to make decisions in code based on a condition. If the condition is true, a  

block of code is executed.  

- `else` can be used to specify what to do if the condition is false.  

- `else if` allows for multiple conditions to be checked in sequence.  

- Example:  

Public class Main {  

Public static void main(String[] args) {  

Int percentage = 85;  

If (percentage > 90) {  

System.out.println(“Grade A”);  

} else if (percentage > 75) {  

System.out.println(“Grade B”); } else if (percentage > 65) {  

System.out.println(“Grade C”);  

} else {  

System.out.println(“Grade D”);  

} } }  

4.5. Switch Statement:  

- The `switch` statement is used to select one of many code blocks to be executed based on the value of an expression.  

- It’s a more efficient way of handling multiple conditions than a series of `if` statements.  

- Example:  

Public class DayOfWeek {  

Public static void main(String[] args) {  

Int dayNumber = 3; StringdayOfWeek=getDayOfWeek(dayNumber);  

System.out.println(“Day: “ + DayOfWeek); }  

Private static String getDayOfWeek(int dayNumber) {  

String dayOfWeek;  

Switch (dayNumber) {  

Case 1:  

dayOfWeek = “Sunday”;  

break;  

case 2:  

dayOfWeek = “Monday”;  

break;  

default:  

dayOfWeek = “Invalid day number”;  

break; }  

Return dayOfWeek;  

} }  

4.6. Loops (DO, WHILE, FOR, FOREACH):  

- Loops are used for repetitive execution of a block of code until a specific condition is met.  

- Common loop types:  

1. for loop:  

Iterates a fixed number of times.  

Public class Main {  

Public static void main(String[] args) {  

For (int i = 0; i < 5; i++) {  

// Code to execute in each iteration  

} } }  

2. `while` loop:  

Repeats as long as a condition is true.  

Public class Main {  

Public static void main(String[] args) {  

Int i = 0;  

While (i < 5) {  

I++;  

} } }  

3. `do-while` loop:  

Similar to a `while` loop but guaranteed to execute at least once.  

Public class Main {  

Public static void main(String[] args) { Int i = 0;  

Do {  

I++;  

} while (i < 5);  

}  }  

4. `foreach` (or equivalent) loop:  

Used for iterating over elements in collections or arrays.  

Public class Main {  

Public static void main(String[] args) {  

Int[] array = {0, 1, 2, 3, 4};  

For (int i : array) {  

// Code to execute in each iteration  

} } }  

4.7. BREAK/CONTINUE Statements:  

- `break` is used to exit a loop prematurely, breaking out of the loop’s execution.  

- `continue` is used to skip the current iteration and move to the next iteration in a loop.  

- These statements are especially useful for fine-grained control in loops.  

- Example:  

Public class Main {  

Public static void main(String[] args) {  

For (int i = 0; i < 10; i++) {  

If (i == 5) {  

Break; // Exit the loop when i equals 5  

}  

// Code here won’t execute when i equals 5 }  }  

Comments

Popular posts from this blog

Semantic Data Model Chapter No 3 RDBMS

  Chapter No 3 ⦁ Semantic Data Model ⦁ Relational Model ⦁ Database Models and Internet Semantic Data Model: "A semantic data model is a method of organizing data in a logical and meaningful way. " It provides a conceptual representation of data and the relationships between them, adding a layer of semantic information that gives data a basic meaning. Key Elements: 1.    - Entities: Represent objects or concepts (e.g., Person, Product). 2.    - Attributes: Characteristics or properties of entities. 3.    - Relationships: Connections between entities, defining associations through the foreign key. A semantic data model describes data about its real-world interpretation and usage.  For example, the object "Person" can be generalized to include "Employee," "Applicant," and "Customer," and is related to "Project" and "Task." A person can own multiple projects, and a specific task can be associated with differe...

Short Q&A about constructors

  Q1: What is a constructor in Java, and when is it called? A1: A constructor in Java is a special method used to initialize objects. It is called when an instance of a class is created, allocating memory for the object in RAM. Q2: What happens if a class in Java does not have any constructor defined? A2: If a class in Java doesn't have any constructor defined, the Java compiler provides a default constructor by default. Q3: What are the two types of constructors in Java? A3: There are two types of constructors in Java:  1. No-arg constructor (Default constructor) 2. Parameterized constructor Q4: What are the rules for creating a Java constructor? A4: The rules for creating a Java constructor are as follows: - Constructor name must be the same as its class name. - A constructor must have no explicit return type. - A Java constructor cannot be abstract, static, final, or synchronized. Q5: What are the different access modifiers that can be used while declaring a constructor in ...

Chemistry Past papers of DAE CIT 1st year

1. **What are Derived units? Give examples.**    - Derived units are units of measurement that are derived from the base units of a system. They express quantities in terms of fundamental units. Examples include:      - Newton (N) for force (kg·m/s²)      - Joule (J) for energy (N·m)      - Watt (W) for power (J/s) 2. **Define Radical, Valency, Formula, and Chemical Equation.**    - Radical: A radical is a group of atoms bonded together that behaves as a single unit in chemical reactions.    - Valency: Valency is the number of chemical bonds an atom can form when it combines with other atoms in a compound.    - Formula: A formula is a representation of a chemical compound using symbols and subscripts to show the ratio of atoms in the compound.    - Chemical Equation: A chemical equation is a symbolic representation of a chemical reaction, showing the reactants and products with their respectiv...