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

DAE CIt 244 Electronics 2ndyear Past Paper 2019

 

Chapter No 6 Interface Java

  Chapter 6                      Interfaces   6.1 Introduction  6.2. Defining and Implementing Interfaces   6.3. Advantages of using Interface    6.1 Introduction   An interface in Java is a collection of abstract methods. It provides a way to achieve abstraction and multiple inheritance in Java. An interface is a blueprint for a class and allows the definition of methods without specifying the implementation. Classes implementing an interface must provide concrete implementations for all its methods.   Syntax   interface MyInterface{ {      // Declare methods (implicitly public and abstract)      void method1();      void method2(); }   Explanation   An interface is declared using the interface keyword.  Methods declared in the interface ar...

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...