Decision making statements is also known as branching statements or conditional control statements.
In real life, we make certain decisions and for those decisions, we will get a certain result. In programming there will be some conditions, if the condition becomes true then the block of code will be execute.
A decision making statement contains one or more conditions to be evaluated along with a statement or statements that are to be executed if the condition is becomes true, and if the condition becomes false the other state will be executed, that is, the else statement will executed.
Conditional Statements
- if
- if-else
- nested-if
- switch case
if statement
If statement is one of the simple decision making statement. It is used to decide whether a certain block of statement will execute only, if the condition becomes true.
Syntax
if(condition){
//statement to be executed if the condition becomes true
}
Example
class example
{
public static void main(String args[])
{
int a = 10;
if(a>5)
{
System.out.println("This is an example for simple if statement");
}
}
}
Output
This is an example for simple if statement
if else statement
In if else statement, if the condition becomes true the block of code inside the if statement will be executed, if the condition becomes false by default the else statement will be executed.
Syntax
if(condition)
{
//statement to be executed if the condition becomes true
}
else
{
//statement to be executed if the condition becomes false
}
Example
class example
{
public static void main(String args[])
{
int a, b;
a = 10;
b = 20;
if(a > b)
{
System.out.println("A is less than B");
}
else
{
System.out.println("B is greater than A");
}
}
}
Output
B is greater than A
nested if statement
When an if statement contains another if statement, then it is called as nested if statement.
Syntax
if (condition_1)
{
// statement to be executed if the condition_1 becomes true
if (condition_2)
{
// statement to be executed if the condition_2 becomes true
}
}
else
{
//statement to be executed if the condition becomes false
}
Example
class example
{
public static void main(String args[])
{
int age;
age = 21;
if(age < 18)
{
System.out.println("You are Minor and you are not eligible to work");
}
else
{
if(age >= 18 && age <= 60)
{
System.out.println("You are Eligible to Work");
}
else
{
System.out.println("You are too old to work as per the Government rules");
}
}
}
}
Output
You are Eligible to Work
switch case statement
When there are multiple cases available in a program, switch statement is used. Switch statement is also called as multiway branch statement. It allows the user to select a particular case and will execute only the particular block of code. The break statement is used inside the switch statement to terminate a statement sequence. If the user select other than the given cases, the default block will execute and default statement is optional.
Syntax
switch(expression)
{
case value :
// Statements to be executed
break;
case value :
// Statements to be executed
break;
case value :
// Statements to be executed
break;
default : // Optional
// Statements to be executed
}
Example
class example
{
public static void main(String args[])
{
{
int i = 1;
switch (i)
{
case 0:
System.out.println("Excellent");
break;
case 1:
System.out.println("Well done");
break;
case 2:
System.out.println("You passed");
break;
case 3:
System.out.println("you failed, better luck next time");
break;
default:
System.out.println("Invalid number");
}
}
}
}
Output
Well done
Jump statements
Jump statements are used to transfer control from one part of the program to other part of the program. There are three jump statements in java.
break keyword
Break is used to force termination of a loop, bypassing the remaining code of the loop body.
Example
class example
{
public static void main(String args[])
{
int a = 20;
for(a = 1; a < 10; a++)
{
if(a==10)
break;
}
System.out.println("This is an example for break keyword");
}
}
Output
This is an example for break statement
continue keyword
The continue statement immediately jump to the next iteration of loop and execute other statement.
Example
class example
{
public static void main(String args[])
{
int a = 20;
for(a = 1; a < 10; a++)
{
if(a==10)
continue;
System.out.println(a);
}
System.out.println("This is an example for continue statement");
}
}
Output
1
2
3
4
5
6
7
8
9
This is an example for continue statement
return keyword
The return statement is used to return from a method. return type may be a void or primitive. Void type returns nothing and primitive type returns integer, float, double, etc.
Example
class example
{
public static void main(String args[])
{
int a = 20;
System.out.println("Before return");
{
if(a==20)
return;
}
System.out.println("After return");
}
}
Output
Before return
This iis the perfect blog for anybody who hopes to find out
about this topic. You definitely put a brand new spin on a topic which has been discussed for decades.Wonderful stuff, just excellent!
Thanks for your valuable feedback.
I got this site from my pal who shared with me concerning this website aand now this time I am browsing thgis webite and
rading very informative articles or reeviews at this place.
Hello there! Would you mind if I share your blog with my twitter group?
There’s a llot of people that I think would really appreciate your
content.Please let me know. Thanks!
Nice post. I was checking continuously this blog and
I’m impressed! Very useful information specially the
last part 🙂 I care for such info a lot. I
was looking for this particular information for a long time.
Thank you and best of luck.
Wow cuz this is greeat work! Congrats andd keep it up!
Fabulous, what a web site it is! This wweb site provides useful data to us,
keep it up.
Please keep us up to date like this. Thanks for sharing…
Hello! Do you use Twitter? I’d like to follow you if that would be
okay. I’m definitely enjoying your blog and look forward too new
articles.
Sweet blog! I found it while searching on Yahoo News.
Do you have any suggestions on how to get listed in Yahoo News?
I’ve been trying for a while but I never seem to
get there! Appreciate it 0mniartist asmr