0% found this document useful (0 votes)
16 views11 pages

CSE 2108 Lab4

This document outlines an experiment focused on conditional statements and loops in Java, detailing their syntax, use cases, and implementation through various programming problems. It includes examples using if-else and switch-case for decision-making, as well as for, while, and do-while loops for iterative tasks. The document emphasizes the importance of mastering these concepts for effective programming.

Uploaded by

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

CSE 2108 Lab4

This document outlines an experiment focused on conditional statements and loops in Java, detailing their syntax, use cases, and implementation through various programming problems. It includes examples using if-else and switch-case for decision-making, as well as for, while, and do-while loops for iterative tasks. The document emphasizes the importance of mastering these concepts for effective programming.

Uploaded by

sindidas90
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

1.

Experiment No: 04

2. Experiment Name: Introduction to Conditional Statements and Loops in Java.

3. Objectives:
●​ To understand the syntax and use cases of conditional statements (if, else, switch) in Java.
●​ To learn how to implement loops (for, while, do-while) for iterative tasks.
●​ To solve basic programming problems using control flow structures.

4. Introduction:
Conditional statements and loops are fundamental concepts in programming that allow
developers to control the execution flow of their code. Conditional statements enable the
program to make decisions based on certain conditions, while loops allow for repeated execution
of a block of code as long as a specified condition is true.
Conditional Statements:
1.​ If Statement: Executes a block of code if a specified condition is true.
2.​ Else If Statement: Provides an additional condition to check if the initial if condition is
false.
3.​ Else Statement: Executes a block of code if none of the preceding conditions are true.
4.​ Switch Statement: A multi-way branch statement that allows the variable to be tested for
equality against a list of values.
Loops:
1.​ For Loop: Used when the number of iterations is known beforehand.
2.​ While Loop: Repeats a block of code as long as a specified condition is true.
3.​ Do-While Loop: Similar to the ‘while’ loop, but guarantees that the block of code will
execute at least once.
4.​ For each loop: In Java, the for-each loop is used to iterate through elements of arrays and
collections (like ArrayList). It is also known as the enhanced for loop.

5. Procedures:
●​ if Statement
Purpose: Execute code conditionally based on a boolean expression.​
Syntax:
if (condition) {
// code block
} else if (condition2) {
// code block
} else {
// default code block
}
Steps:
Evaluate the first condition (if).
If true, execute the if code block and exit the entire structure.

1
If false, check the next else if condition (if any).
If no else if/if is true, execute the else block (if present).

●​ switch Statement
Purpose: Execute code based on matching a value from multiple options.​
Syntax:
switch (expression) {
case value1:
// code
break;
case value2:
// code
break;
default:
// code
}
Steps:
Evaluate the expression.
Compare the result with each case value top to bottom.
If a match is found, execute its code block until break or the end of switch.
If no match, execute the default block (if present).

●​ for Loop
Purpose: Iterate a fixed number of times.​
Syntax:
for (initialization; condition; update) {
// code block
}
Steps:
Initialize the loop variable (e.g., int i = 0).
Check the condition (e.g., i < 5).
If true, execute the loop body.
If false, exit the loop.
Execute the loop body.
Update the loop variable (e.g., i++).
Repeat from step 2.

●​ while Loop
Purpose: Repeat code while a condition is true.​
Syntax:
while (condition) {
// code block
}
Steps:
Check the condition.
If true, execute the loop body.

2
If false, exit the loop.
Repeat step 1 after the loop body runs.
●​ do-while Loop
Purpose: Execute code at least once, then repeat while a condition is true.​
Syntax:
do {
// code block
} while (condition);
Steps:
Execute the loop body.
Check the condition.
If true, repeat the loop.
If false, exit.

6. Experiment Implementations:

Experiment 1: Odd even using if-else

Code: import java.util.Scanner;


public class JavaApplication1 {
public JavaApplication1() {
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("enter n:");
int n = scanner.nextInt();
if (n % 2 == 0) {
System.out.println(n + "is even");
} else {
System.out.println(n + "is odd");
}
scanner.close();
}
}

Output:

Experiment 2: odd even using switch case

Code: package javaapplication1;


import java.util.Scanner;
public class switch2 {
public switch2() {

3
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("enter n:");
int n = scanner.nextInt();
switch (n % 2) {
case 0:
System.out.println(n + "is even");
break;
case 1:
System.out.println(n + "is odd");
}
scanner.close();
}
}

Output:

Experiment 3: A program to check switch case

Code:
public class NewClass {
public NewClass() {
}

public static void main(String[] args) {


int n = 2;
switch (n) {
case 1:
System.out.println(n + "is even");
case 2:
System.out.println(n + "is ok");
case 3:
System.out.println(n + "isl");
default:
}
}
}

Output:

Experiment 4: leap year using if-else

Code: package javaapplication1;

4
import java.util.Scanner;
public class leap {
public leap() {
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("enter n:");
int n = scanner.nextInt();
if (n % 4 == 0) {
if (n % 100 == 0 && n % 400 == 0) {
System.out.println(n + "is leapyear");
}
} else {
System.out.println(n + "is not leapyear");
}
scanner.close();
}
}

Output:

Experiment 5: program to check max & min (m,n,l).

Code: package javaapplication1;


import java.util.Scanner;
public class NewClass1 {
public NewClass1() {
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("enter n:");
int n = scanner.nextInt();
System.out.println("enter m:");
int m = scanner.nextInt();
System.out.println("enter l:");
int l = scanner.nextInt();
int max = n;
int min = n;
if (m > n) {
max = m;
}
if (l > max) {
max = l;
}
if (m < n) {
min = m;
}
if (l < min) {
min = l;
}
System.out.println(max + "is maximum");
System.out.println(min + "is minimum");
scanner.close();

5
}
}

Output:

Experiment 6: printing weekdays using switch case.

Code: package javaapplication1;


import java.util.Scanner;
public class switch1 {
public switch1() {
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("enter n:");
int n = scanner.nextInt();
switch (n) {
case 1:
System.out.println("Saturday");
break;
case 2:
System.out.println("Sunday");
break;
case 3:
System.out.println("Monday");
break;
case 4:
System.out.println("Tuesday");
break;
case 5:
System.out.println("wednesday");
break;
case 6:
System.out.println("Thursday");
break;
case 7:
System.out.println("Friday");
}
}
}

6
Output:

Experiment 7: checking grades using switch case.

Code: package javaapplication1;


import java.util.Scanner;
public class switch3 {
public switch3() {
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("enter n:");
int n = scanner.nextInt();
switch (n / 5) {
case 10:
System.out.println("F");
break;
case 11:
System.out.println("D");
break;
case 12:
System.out.println("C");
break;
case 13:
System.out.println("B");
break;
case 14:
System.out.println("A-");
break;
case 15:
System.out.println("A");
break;
case 16:
case 17:
case 18:
case 19:
case 20:
System.out.println("A+");
}
scanner.close();
}
}

Output:

7
7. Discussion:

●​ Conditional Statements:
if-else is ideal for binary decisions.
switch simplifies multi-way branching but requires break to prevent fall-through.
●​ Loops:
for loops are best for fixed iterations (e.g., arrays).
while loops are useful when the number of iterations is unknown.
●​ Common Errors:
Infinite loops due to incorrect termination conditions.
Missing curly braces {} in multi-line code blocks.

8. Conclusion:
Conditional statements and loops are foundational for controlling program flow in Java. This lab
demonstrated their practical use in solving problems like decision-making and iterative tasks.
Mastery of these concepts is critical for advanced programming.

8
Practice Problem 1: Check Vowel or Consonant using Switch.

Code: import java.util.Scanner;


public class VowelChecker {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a character: ");
char ch = sc.next().toLowerCase().charAt(0); // Convert to lowercase
switch (ch) {
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
System.out.println(ch + " is a vowel.");
break;
default:
System.out.println(ch + " is a consonant.");
}
}
}

Output:

Practice Problem 2: Check Even/Odd using Switch.

Code: package lab4;


import java.util.Scanner;
public class EvenOddChecker {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = sc.nextInt();
// Use absolute value to handle negative numbers
switch (Math.abs(num % 2)) {
case 0:
System.out.println(num + " is even.");
break;
default:
System.out.println(num + " is odd.");
}
}
}
Output:

9
Practice Problem 3: Sum First N Even Numbers using While Loop.

Code: import java.util.Scanner;


public class SumEvenNumbers {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter n: ");
int n = sc.nextInt();
int sum = 0, count = 1, even = 2;
while (count <= n) {
sum += even;
even += 2;
count++;
}
System.out.println("Sum of first " + n + " even numbers: " + sum);
}
}

Output:

Practice Problem 4: Multiplication Table using For Loop.

Code: import java.util.Scanner;


public class MultiplicationTable {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int n = sc.nextInt();
System.out.println("Multiplication Table of " + n + ":");
for (int i = 1; i <= 10; i++) {
System.out.println(n + " x " + i + " = " + (n * i));
}
}
}

Output:

10
Practice Problem 5: Sum of Array Elements using For-Each Loop.

Code: public class ArraySum {


public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5};
int sum = 0;
for (int num : arr) {
sum += num;
}
System.out.println("Sum of array elements: " + sum);
}
}

Output:

Practice Problem 6: Print Fibonacci Series.

Code: import java.util.Scanner;


public class FibonacciSeries {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter number of terms: ");
int terms = sc.nextInt();
int first = 0, second = 1;
System.out.println("Fibonacci Series:");
for (int i = 1; i <= terms; i++) {
System.out.print(first + " ");
int next = first + second;
first = second;
second = next;
}
}
}

Output:

11

You might also like