0% found this document useful (0 votes)
22 views16 pages

1 5 1 P 603c5d760c516 File

The document discusses different types of decision making statements in Java including if, if-else, if-else-if ladder, nested if, and switch case statements. It provides syntax and examples of each type of statement.
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)
22 views16 pages

1 5 1 P 603c5d760c516 File

The document discusses different types of decision making statements in Java including if, if-else, if-else-if ladder, nested if, and switch case statements. It provides syntax and examples of each type of statement.
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/ 16

CO_IF_22412_CO1

Ms. Yogita Jore, Head of Information Technology, Vidyalankar Polytechnic

Date: 13th February 2021

Learning at your doorstep


Unit 1:
Basic Syntactical constructs in Java

Written by

Yogita Jore
Head, Department of Information Technology (NBA Accredited),
Vidyalankar Polytechnic, Mumbai
Unit Outcome 5:
Develop Programs using relevant
control structure to solve the given
problem.
Learning Outcome 5a:
Students should understand the use of
decision making statements.
What we will learn today-Decision Making Statements

Key takeaways
1. if statement
Where to use decision making statements in given problem?
2. if-else statement

3. if-else-if ladder

4. nested if statement
Yogita Jore
5. Switch case Head, Department of Information Technology (NBA Accredited), Vidyalankar Polytechnic,
Mumbai

Page 5 Maharashtra State Board of Technical Education


Concept Map

Page 6 Maharashtra State Board of Technical Education


Learning Objective/ Key learning

► if statement
► if-else statement
► if-else-if ladder
► nested if statement
► Switch case

Page 7 Maharashtra State Board of Technical Education


Java if Statement

 The Java if statement tests the condition. Syntax:


 It executes the if block if condition is true. if(condition)
 Example: {
public class if_demo //code to be executed
{ }
public static void main(String[] args)
{
int age=20;
if (age > 18)
{
System.out.println("age is greater than 18");
}
}
}

}
}
Page 8 Maharashtra State Board of Technical Education
Java if-else Statement

 Java if-else statement also tests the condition.


 It executes the if block if condition is true otherwise else block is executed.
 Example: Syntax:
class demo_if_else_ex if(condition)
{
{
public static void main(String[] args)
{ //code if condition is true
int number = -10; }
if (number > 0) else
{ {
System.out.println("The number is positive."); //code if condition is false
} }
else
{
System.out.println("The number is not positive.");
}
}
}
}
}
Page 9 Maharashtra State Board of Technical Education
Java if-else –if Statement

 The if-else-if ladder statement executes one condition from Syntax:


multiple statements. if(condition1)
{
 Example: //code to be executed if condition1 is true
class if_else_if_demo } else if(condition2)
{ {
public static void main(String[] args) //code to be executed if condition2 is true
{
int number = 0; }
if (number > 0) else if(condition3)
{ {
//code to be executed if condition3 is true
System.out.println("The number is positive.");
} }
else if (number < 0) ...
{ else
System.out.println("The number is negative."); {
//code to be executed if all the conditions
} are false
else }
{
System.out.println("The number is 0.");
}
}
}}
} Page 10 Maharashtra State Board of Technical Education
Java nested if Statement

 The nested if statement represents the if block within another if block.


 Here, the inner if block condition executes only when outer if block condition is true.
}
Syntax:
if(condition){
//code to be executed
if(condition){
//code to be executed
}
}

Page 11 Maharashtra State Board of Technical Education


Java nested if Statement-Example

class nested_if else


{ {
public static void main(String[] args) if (n2 >= n3)
{ {
Double n1 = -1.0, n2 = 4.5, n3 = -5.3, largest; largest = n2;
if (n1 >= n2) }
{ else {
if (n1 >= n3) largest = n3;
{ }
largest = n1; }
} System.out.println("Largest Number: " + largest);
else }
{ }
largest = n3;
}
}

Page 12 Maharashtra State Board of Technical Education


Java switch Statement

 The Java switch statement executes one statement from multiple conditions.
 It is like if-else-if ladder statement.
 The switch statement works with byte, short, int, long, enum types, String and some wrapper types like Byte,
Short, Int, and Long.
 In other words, the switch statement tests the equality of a variable against multiple values.
Syntax:
switch(expression)
{
case value1:
//code to be executed;
break; //optional
case value2:
//code to be executed;
break; //optional
......
default:
code to be executed if all cases are not matched;
}
Page 13 Maharashtra State Board of Technical Education
Java switch Statement-Example

public class Switch_demo case 4:


{ System.out.println("Thursday");
public static void main(String[] args) break;
{ case 5:
int day = 6; System.out.println("Friday");
switch (day) { break;
case 1: case 6:
System.out.println("Monday"); System.out.println("Saturday");
break; break;
case 2: case 7:
System.out.println("Tuesday"); System.out.println("Sunday");
break; break;
case 3: }
System.out.println("Wednesday"); }
break; }

Page 14 Maharashtra State Board of Technical Education


break

The break statement in java is used to terminate from the loop immediately.
When a break statement is encountered inside a loop, the loop iteration stops there, and control returns from
the loop immediately to the first statement after the loop.
Example:
public class BreakDemo
{
public static void main(String[] args)
{
for(int i=1;i<=10;i++)
{ if(i==8)
{
break;
}
System.out.println(i);
}
}
}

Page 15 Maharashtra State Board of Technical Education


continue

The continue statement in Java is used to skip the current iteration of a loop.
We can use continue statement inside any types of loops such as for, while, and do-while loop.
Example:
public class ContinueDemo
{
public static void main(String[] args)
{
for(int i=1;i<=10;i++)
{
if(i==5)
{
continue;
}
System.out.println(i);
}
}
}
Page 16 Maharashtra State Board of Technical Education

You might also like