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

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

Chapter No 2 Java DAE CIT

  . Variable and Operators 1.1. Variable: Define: A variable is a named storage location in a program that holds data, and its value can be changed during the execution of the program. Syntax: datatype variableName; Explanation: Variables are used to store and manipulate data in a program. They have a data type that defines the kind of data they can hold, such as int, float, char, etc. Code int age; // Declaration of an integer variable age = 25; // Initialization of the variable with a value 1.2. Create Instance of a Variable: Define: Creating an instance of a variable involves declaring and optionally initializing a variable in a program. Syntax: datatype variableName = value; Explanation: This creates a variable and assigns an initial value to it at the time of declaration. score = 95.5  # Declaration and initialization of a floating-point variable 1.3. Use Preemptive Data Types: Define: Preemptive data types are data types that are predefined and provided by...

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