0% found this document useful (0 votes)
6 views2 pages

Java Conditional Statements Explained

The document covers conditional statements in Java, specifically 'if-else' and 'switch' statements, providing examples for each. It also includes homework problems that involve creating a simple calculator and a program to display the name of a month based on user input. The emphasis is on practicing programming concepts learned in the lecture.

Uploaded by

Anda
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)
6 views2 pages

Java Conditional Statements Explained

The document covers conditional statements in Java, specifically 'if-else' and 'switch' statements, providing examples for each. It also includes homework problems that involve creating a simple calculator and a program to display the name of a month based on user input. The emphasis is on practicing programming concepts learned in the lecture.

Uploaded by

Anda
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

Java - Introduction to Programming

Lecture 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) {
[Link]("This is an adult");
} else {
[Link]("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 :
[Link]("Monday");
break;
case 2 :
[Link]("Tuesday");
break;
case 3 :
[Link]("Wednesday");
break;
case 4 :
[Link]("Thursday");
break;
case 5:
[Link]("Friday");
break;
case 6 :
[Link]("Saturday");
break;
default :
[Link]("Sunday");
}

Apna College
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 :)

Apna College

You might also like