0% found this document useful (0 votes)
10 views4 pages

Java Programming Basics: Control Structures

The document contains 7 sections that demonstrate different Java programming concepts: 1) If/else and multiple selection statements 2) Switch multiple selection 3) Do/while loop 4) Nested for loops to print a pattern 5) Creating and calling methods 6) Creating and accessing an array 7) Initializing an array with values and printing them

Uploaded by

MohdHarith
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views4 pages

Java Programming Basics: Control Structures

The document contains 7 sections that demonstrate different Java programming concepts: 1) If/else and multiple selection statements 2) Switch multiple selection 3) Do/while loop 4) Nested for loops to print a pattern 5) Creating and calling methods 6) Creating and accessing an array 7) Initializing an array with values and printing them

Uploaded by

MohdHarith
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

1) If else multiple selection

public class test1{

public static void main(String []args){

int age;

age = 15;

if ((age >=10) && (age <= 20))

[Link]("you are qualified");

else if ((age >20) && (age <=30))

[Link]("You are not qualified");

else

[Link]("Input error");

2. Switch Multiple Selection

public class test1{

public static void main(String []args){

char raceCode;

raceCode = 'M';

switch (raceCode){

case 'M': [Link]("Malay"); break;

case 'C': [Link]("Chinese"); break;

case 'I': [Link]("Indian"); break;

default: [Link]("Others");

}
}

3. Belajar do while

public class belajar {

public static void main(String []args){

int i;

i=0;

do {

[Link](i);

i++;

while (i<5);

4. nested loops

public class test1 {

public static void main(String []args){

for ( int line =1; line <= 5; line ++)

for (int column = 1; column <= line; column++)

[Link]("*");

[Link]();
}

5. Method

//this code is to create method for an object

public class obj {

public static void main(String []args){

//declare and initiate value

int a=67;

int b=80;

int max = getMax (a,b);

[Link]("Maximum is "+ max);

//this code is a method to find max number

public static int getMax (int no1, int no2)

int max;

if (no1 > no2)

max = no1;

else

max = no2;

return max;

6. Array

//this code is to create array

public class arry {


public static void main(String []args){

String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};

for (int i = 0; i < [Link]; i++) {

[Link](cars[i]);

7. Assignment array

Soalan 1

//this code is to create array

public class Assignment {

public static void main(String []args)

//coding untuk initiate array valu

int[] value = {1,2,3,4,5,4,4,3};

//loop untuk baca dman display array value

for (int i = 0; i < [Link]; i++) {

[Link](value[i]);

Common questions

Powered by AI

'Do-while' loops execute the code block at least once before checking the condition at the end of the loop, ensuring the loop body runs even if the condition is initially false. In contrast, 'for' loops check the condition before each iteration, making it possible for the loop body to not execute if the condition is false at the beginning .

'If-else' conditionals evaluate Boolean expressions and execute the code block based on whether the condition is true, whereas 'switch' statements work with a variable to match against known constant values, executing the corresponding code block. The 'if-else' can handle complex expressions, while 'switch' is useful for checking the same expression against different constant values .

'Break' is necessary in 'switch' statements to terminate a case block, preventing fall-through to subsequent cases inadvertently. If omitted, the program will continue executing subsequent case blocks until reaching a 'break' or the end of the 'switch', which may lead to unintended behavior and logic errors .

A 'do-while' loop can be more efficient than a 'while' loop in scenarios requiring immediate execution of the loop body before checking conditions, as it guarantees the body executes at least once. This can simplify logic when initialization or setup needs to occur before condition evaluation .

A method returning the maximum of two numbers centralizes the logic for comparison, reducing redundancy and potential errors. It enhances code reusability by providing a single point of modification if the comparison logic needs to change and allows this functionality to be used multiple times across a program without duplicating code .

Nested loops facilitate pattern generation by iterating over multiple dimensions, allowing the program to construct complex output such as patterns or grids. The outer loop can handle rows while the inner loop handles columns, enabling the generation of structured layouts, like creating a triangle of asterisks .

Arrays provide a structured way to store multiple items of the same type, allowing indexed access and manipulation. They enable efficient storage, easy iteration, and bulk operations on elements, which facilitates orderly management and retrieval of data. Arrays simplify representation of lists and tables where elements need to be accessed systematically .

A 'switch' statement is preferred when dealing with multiple discrete known values for a variable, offering clearer syntax and potentially better performance than an 'if-else' chain, which is more suited for complex conditions and ranges. This is especially true for a large number of options with simple comparisons .

Loops enable systematic iteration over array elements, allowing operations like searching, sorting, or applying functions to individual elements. It enhances functionality by providing the means to traverse array elements efficiently, supporting tasks like logging, updating, or checking values without cumbersome manual access .

Methods promote modularity by encapsulating functionality within discrete units that can be independently debugged and updated. They improve maintainability, as changes can be isolated to specific methods without affecting the entire codebase. Additionally, methods simplify complex operations by abstracting details, leading to clearer and more organized code .

You might also like