- Decision Making in programming is similar to decision making in real life.
- A programming language uses control statements to control the flow of execution of program based on certain conditions.
- These are used to cause the flow of execution to advance and branch based on changes to the state of a program.
- A. if
- Syntax:
if(condition)
{
}
- Here, condition after evaluation will be either true or false.
- Example:
{
public static void main(String args[])
{
int i = 11;
if (i > 12)
System.out.println("11 is less than 12");
System.out.println("I am Not in if");
}
}
- B. if-else
- The if statement alone tells us that if a condition is true it will execute a block of statements and if the condition is false it won’t.
- We can use the else statement with if statement to execute a block of code when the condition is false.
- Syntax:
if (condition)
{
// Executes this block if
// condition is true
}
else
{
// Executes this block if
// condition is false
}
- Example:
class IfElseDemo
{
public static void main(String args[])
{
int i = 11;
if (i < 12)
System.out.println("11 is smaller than 12");
else
System.out.println("12 is greater than 11");
}
}
- C. nested-if
- A nested if is an if statement that is the target of another if or else.
- Nested if statements means an if statement inside an if statement.
- Syntax:
if (condition1)
{
// Executes when condition1 is true
if (condition2)
{
// Executes when condition2 is true
}
}
- Example:
class NestedIfDemo
{
public static void main(String args[])
{
int i = 11;
if (i == 11)
{
// First if statement
if (i < 12)
System.out.println("11 is smaller than 12");
if (i < 15)
System.out.println("11 is smaller than 15 too");
else
System.out.println("15 is greater than 11");
}
}
}
- D. if-else-if ladder:
- The if statements are executed from the top down.
- As soon as one of the conditions controlling the if is true, the statement associated with that if is executed.
- If none of the conditions is true, then the final else statement will be executed.
- Syntax:
if (condition)
statement;
else if (condition)
statement;
.
.
else
statement;
- Example:
class ifelseifDemo
{
public static void main(String args[])
{
int i = 20;
if (i == 10)
System.out.println("i is 10");
else if (i == 15)
System.out.println("i is 15");
else if (i == 20)
System.out.println("i is 20");
else
System.out.println("i is not present");
}
}
- E. switch-case
- The switch statement is a multiway branch statement.
- It provides an easy way to dispatch execution to different parts of code based on the value of the expression.
- Syntax:
switch (expression)
{
case value1:
statement1;
break;
case value2:
statement2;
break;
.
.
case valueN:
statementN;
break;
default:
statementDefault;
}
- Expression can be of type byte, short, int char or an enumeration.
- Note: Beginning with JDK7, expression can also be of type String.
- Duplicate case values are not allowed.
- The default statement is optional.
- The break statement is used inside the switch to terminate a statement sequence.
- The break statement is optional. If omitted, execution will continue on into the next case.
- Example:
class SwitchCaseDemo
{
public static void main(String args[])
{
int i = 9;
switch (i)
{
case 0:
System.out.println("i is zero.");
break;
case 1:
System.out.println("i is one.");
break;
case 2:
System.out.println("i is two.");
break;
default:
System.out.println("i is greater than 2.");
}
}
}
- F. jump – break, continue, return
- Java supports three jump statement: break, continue and return.
- 1. Break: In Java, break is majorly used for:
- Terminate a sequence in a switch statement (discussed above).
- To exit a loop.
- Syntax: break;
- Example:
{
public static void main(String args[])
{
// Initially loop is set to run from 0-9
for (int i = 0; i < 10; i++)
{
// terminate loop when i is 5.
if (i == 5)
break;
System.out.println("i: " + i);
}
System.out.println("Loop complete.");
}
}
- 2. Continue
- It is used to continue running the loop but stop processing as per condition.
- Syntax: Continue;
- Example:
class ContinueDemo
{
public static void main(String args[])
{
for (int i = 0; i < 10; i++)
{
// If the number is even
// skip and continue
if (i%2 == 0)
continue;
// If number is odd, print it
System.out.print(i + " ");
}
}
}
- 3. Return:
- The return statement is used to explicitly return from a method.
- That is, it causes program control to transfer back to the caller of the method.
- Syntax: return;
- Example:
class Return
{
public static void main(String args[])
{
boolean t = true;
System.out.println("Before the return.");
if (t)
return;
System.out.println("This won't execute.");
}
}
Tags:
Core java