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

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