0% found this document useful (0 votes)
20 views

Java Lecture 3

This document provides an introduction to conditional statements in Java programming. It explains the use of if-else statements to execute different code blocks based on boolean conditions. An example checks if a variable is greater than 18 and prints different messages accordingly. The document also covers switch statements as an alternative to long if-else chains for comparing a variable against multiple values. A switch example prints the day of the week corresponding to a number variable. Finally, it lists two homework problems - to create a basic calculator and print the name of the month for a given number.

Uploaded by

Muhammad Ahmad
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)
20 views

Java Lecture 3

This document provides an introduction to conditional statements in Java programming. It explains the use of if-else statements to execute different code blocks based on boolean conditions. An example checks if a variable is greater than 18 and prints different messages accordingly. The document also covers switch statements as an alternative to long if-else chains for comparing a variable against multiple values. A switch example prints the day of the week corresponding to a number variable. Finally, it lists two homework problems - to create a basic calculator and print the name of the month for a given number.

Uploaded by

Muhammad Ahmad
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
You are on page 1/ 2

Java - Introduction to Programming

Lab 3

1. Conditional Statements ‘if-else’


The if block is used to specify the code to be executed if the condition specified in if is
true, the else block is executed otherwise.

int age = 30;


if(age > 18) {
System.out.println("This is an adult");
} else {
System.out.println("This is not an adult");
}

2. Conditional Statements ‘switch’


Switch case statements are a substitute for long if statements that compare a
variable to multiple values. After a match is found, it executes the
corresponding code of that value case.

The following example is to print days of the week:

int n = 1;
switch(n) {
case 1 :
System.out.println("Monday");
break;
case 2 :
System.out.println("Tuesday");
break;
case 3 :
System.out.println("Wednesday");
break;
case 4 :
System.out.println("Thursday");
break;
case 5:
System.out.println("Friday");
break;
case 6 :
System.out.println("Saturday");
break;
default :
System.out.println("Sunday");
}

Homework Problems
1. Make a Calculator. Take 2 numbers (a & b) from the user and an operation
as follows :
1 : + (Addition) a + b
● 2 : - (Subtraction) a - b
● 3 : * (Multiplication) a * b
● 4 : / (Division) a / b
● 5 : % (Modulo or remainder) a % b
Calculate the result according to the operation given and display it
to the user.
2. Ask the user to enter the number of the month & print the name of the
month. For eg - For ‘1’ print ‘January’, ‘2’ print ‘February’ & so on.

KEEP LEARNING & KEEP PRACTICING :)

You might also like