Why use Conditional statement?
Conditional statements are also known as decision-making statements.
We make some decisions such as which movie to watch or which book to read..,
The same decision making is also incorporated in programs by using Conditional Statements.
We can control the flow of a program by using conditional statements.
How to check a number is even or odd?
we can use if-else statement.
Syntax
if(booleanExpression){ statement(s); }else{ statement(s); }
Example:Check a number is even or odd
public class Demo{ public static void main(String args[]){ int a=20; if(a%2==0){ System.out.println("a is Even"); } else{ System.out.println("a is Odd"); } } }Try it Yourself
Exercise Q&A
10767.What is the output of the following program?
public class IfExample { public static void main(String[] args){ boolean var1=true; if (var1 ) System.out.println("Inside If Condition"); else System.out.println("Inside Else Condition"); } }
Inside Else Condition
Inside If Condition
Error
No Output
10768.
public class selection_statements { public static void main(String args[]){ int var1 = 6; int var2 = 5; if(var1>var2) System.out.println("var1 is greater"); else System.out.println("var2 is greater"); } }
var1 is greater
var2 is greater
Error
Greater
How to print the day using switch in java?
To check more than one condition we can use switch case.
Syntax
switch (expression) { case value_1 : statement(s); break; case value_2 : statement(s); break; . . . casevalue_n: statement(s); break; default: statement(s); }
Example:Print a day
public class SwitchDate{ public static void main(String[]args){ int day =3; switch(day){ 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; case 7:System.out.println("sunday"); break; default:System.out.println("Invalid day"); } } }Try it Yourself
Solve this!!
10769.What is the output of the following program?
public class SwitchControl { public static void main(String[] args){ int i=10; switch(i) { case 10 : System.out.println("Case 10"); break; case 20 : System.out.println("Case 20"); break; case default : System.out.println("Default Case"); break; } } }
Case 10
Case 20
Default Case
Error
10770.
class SwitchControl { public static void main(String[] args) { int i=10; switch(i) { case 5 : System.out.println("Case 5"); break; case 10 : System.out.println("Case 10"); break; case 20 : System.out.println("Case 20"); break; default : System.out.println("Default Case"); } } }
Case 5
Case 10
Case 20
Default Case
- Collection
- ArrayList
- LinkedList
- Vector
- HashSet
- Conditional Statements
- Classes and Objects
- Inheritance
- Packages
- Exception Handling
- Threads
- TreeSet
- LinkedHashSet
- HashMap
- TreeMap
- LinkedHashMap
- HashTable
- Iterator and ListIterator
- String
- Scanner
- Array Small Programs
- Introduction
- Variables
- DataTypes
- Operators
- Conditional Statements
- Loop
- Arrays
- Object Oriented Programming
- Classes and Objects
- Access Modifiers
- Methods
- Abstraction
- Constructor
- Packages
- StringBuffer
- Inheritance
- StringBuilder
- Encapsulation
- Java IO
- Polymorphism
- Interface
- Types Of Variables
- Static Keyword
- Final Keyword
- Super Keyword
- This Keyword
- Exception Handling