Java Control Flow Statements

Java Control Flow Statements tin hand the sack hold out divided inward to 3 categories Java Control Flow Statements
Java Control Flow Statements

Java Control Flow Statements tin hand the sack hold out divided inward to 3 categories,
i) Decision Making Statements
ii) Loop Statements
iii) Branching Statements
-------------------------------------------------
i) Decision Making Statements

a) Two types of Statements

1) if statement
2) switch statement

b) Three Types of Conditions

1) Single Condition (Positive, Negative)

ex:
if (a>b) {
.
.
}
-----------------
Negative Condition
if (b<a) {
.
.
}
--------------
if (!(b>a)) {
.
.
}
----------------------------------
2) Compound Condition (Positive, Negative)

Ex:
if ((a>b) && (a>c)){
.
.
}
------------
if ((a>b) || (a>c)){
.
.
}
------------
if ((a>b) && (a>c) && (a>d)){
.
.
}
--------------------------------
if ((a>b) && (a>c) || (a>d)){
.
.
}
--------------------------------------
3) Nested Condition

if(a>b){
 if(a>c){
  if(a>d){
.
.
}
}
}
--------------------------
c) Usage of Conditional Statements / Decision Making Statements

1) Execute a block of Statements when status is true.

Syntax:

if (condition) {
Statements
---------
--------
}

Example:

int a =10, b=50;

if (a>b){
System.out.println("A is a Big Number");
}
-----------------------------
int a =10, b=5;

if (b<a){
System.out.println("A is a Big Number");
}
--------------------------------
int a =10, b=5;

if (!(b>a)){
System.out.println("A is a Big Number");
}
------------------------------------
2) Execute a block of Statements when a chemical compound status is true

Syntax:
if ((condition1/Operand1/expression) && Or || (Condition2)) {
Statements
.
.
}
-------------------------------------
int a =10, b=7, c=4;

if ((a>b) && (a>c)){
System.out.println("A is a Big Number");
}
---------------------
int a =10, b=7, c=40;

if ((a>b) || (a>c)){
System.out.println("A is a Big Number");
}
----------------------------------------
3) Execute a block of Statements when status is truthful otherwise execute roughly other block of Statements

if (condition) {
Statements
---------
----------
}
else
{
Statements
-------------
------------
}
------------------------------------
Example:

int a =10, b=10;

if (a>b) {
System.out.println("A is a Big Number");
}
else{
System.out.println("B is a Big Number");
}
------------------------------------
4) Decide amid several alternates(else if)

Syntax:

if (condition) {
Statements
-------------
------------
----------
}
else if (condition) {
Statements
----------
----------
----------
}
else if (condition) {
Statements
----------
----------
----------
}
else 
{
Statements
----------
-----------
}
---------------------------------
Initialize an Integer Variable as well as verify the range

If the discover is inward betwixt 1 as well as 100 as well as thus display "Number is a Small Number"
If the discover is inward betwixt 101 as well as chiliad as well as thus display "Number is a Medium Number"
If the discover is inward betwixt 1001 as well as 10000 as well as thus display "Number is a Big Number"
If the discover is to a greater extent than than 10000 as well as thus display "Number is a High Number"
Otherwise display "Number is a either Zero or Negative Number"
-----------------------------------------
Java Program:

int a = -100;

if ((a>0) && (a<=100)){
System.out.println("A is a Small Number");
}
else if ((a>100) && (a<=1000)){
System.out.println("A is a Medium Number"); 
}
else if ((a>1000) && (a<=10000)){
System.out.println("A is a Big Number"); 
}
else if (a>10000){
System.out.println("A is a High Number"); 
}
else 
{
System.out.println("Number is either Zero or Negative Number"); 
}
}
}
-----------------------------
5) Execute a block of Statements when to a greater extent than than ane status is truthful (nested if)

syntax:
if (condition){
 if (condition){
  if (condition){
  Statements
  --------------
  --------------
  }
  }
  }
-------------------------------------------
Example:
Initialize a, b, c, as well as d variables (Integer variables), banking concern stand upwards for if the a variable is bigger than other 3 variables or not?

int a=100, b=90, c=80, d=700;

if (a>b){
 if (a>c){
  if (a>d){
  System.out.println("A is a Big Number"); 
  }
 }
}
----------------------------------------
int a=100, b=900, c=800, d=700;

if (a>b){
 if (a>c){
  if (a>d){
  System.out.println("A is a Big Number"); 
  }
  else {
  System.out.println("A is Not a Big Number-3rd status is False");
  }
 }
 else{
 System.out.println("A is Not a Big Number-2nd status is False");
 }
}
else
{
System.out.println("A is Not a Big Number -1st status is False"); 
}
}
}
-----------------------------------------------
Using Compound Condition

int a=100, b=90, c=80, d=70;

if ((a>b) && (a>c) && (a>d)){
System.out.println("A is a Big Number"); 
}
else
{
System.out.println("A is Not a Big Number");
}
------------------------------------------------------
Nested if Condition vs. Compound Condition

In Nested if status nosotros tin hand the sack write multiple else parts, where equally inward Compound Condition nosotros tin hand the sack write unmarried else business office only.
-----------------------------------------------------
Problem: Find the biggest variable (Integer variables) amid four variables

Use Compound Condition as well as else if...

int a=100, b=90, c=80, d=700;

if ((a>b) && (a>c) && (a>d)){
System.out.println("A is a Big Number");
}
else if ((b>a) && (b>c) && (b>d)){
System.out.println("B is a Big Number");
}
else if ((c>a) && (c>b) && (c>d)){
System.out.println("C is a Big Number");
}
else {
System.out.println("D is a Big Number");
}
}
}
-------------------------------------
6) Decide amid several alternates (using switch statement)

Syntax:

switch (expression) {

case value:
Statements
-------------
--------------
break;
case value:
Statements
-------------
--------------
break;
case value:
Statements
-------------
--------------
break;

default:
Statements
-----------
-----------
}

Example:

char bird = 'Z';

switch (grade){

case 'A':
System.out.println("Excellent");
break;

case 'B':
System.out.println("Good");
break;

case 'C':
System.out.println("Better");
break;

default:
System.out.println("Invalid Grade");
}

}
}
-------------------------------------------------
ii) Looping Statements

1) for loop
2) field loop
3) produce field loop
4) Enhanced for loop

1) for loop

Description: It repeats a block of statements for a specified discover of times,

Syntax:

for (Stratvalue; EndValue; Increment / Decrement){
Statements
-------------
-----------
}
--------------------------------------
Print 1 to 10 Numbers
Example:
for (int i=1; i<=10; i++){
System.out.println(i);
}
------------------------------
Print 1 to v Numbers except fourth Number

for (int i=1; i<=5; i++){

if (i != 4){
System.out.println(i);
}

}
----------------------------------------------
Print 1 to 10 Numbers inward contrary Order

for (int i=10; i>=1; i--){
System.out.println(i);
}
---------------------------------
2) field loop

Description: It repeats a block of Statements field status is true

Syntax:
initialization
while (condition){
Statements
--------------
--------------
Increment/Decrement
}
---------------------------------
Examples:

1) Print 1 to 10 Numbers
2) Print 10 to 1 Numbers
3) Print 1 to v Numbers except tertiary number
------------------------------------------------
int i=1;

while (i<=10){
System.out.println(i);
i++;
}
------------------------------------
int i=10;

while (i>=1){
System.out.println(i);
i--;
}
-------------------------------------------
int i=1;
while (i<=5){
if (i != 3){
System.out.println(i);
}
i++;
}
-----------------------------------------
3) produce field loop

It repeats a block of statements field status is true
It executes statements at to the lowest degree ane time irrespective of the condition

Syntax:

Initialization
do
{
Statements
-----------
----------
Increment/Decrement
} field (condition);
------------------------------------
Print 1 to 10 Numbers
Print 10 to 1 Numbers
Print 1 to v Numbers except fourth number,
----------------------------------------
int i=1;
do
{
System.out.println(i);
i++;
} field (i<=10);
-------------------------------------------
int i=10;
do
{
System.out.println(i);
i--;
} field (i>=1);
--------------------------------------
int i=10;
do
{
System.out.println(i);
i--;
} field (i>=1);
---------------------------------------
int i=1;
do
{
if (i != 4){
System.out.println(i);
}
i++;
} field (i<=5);
---------------------------------------------------------
4) Enhanced for loop

Syntax:

It executes all elements inward an Array

Array Declaration;
for (declaration: Expression/Array){
Statements
--------------
--------------
-------------
}

Example:
String [] languages = {"C", "COBOL", "Java", "VBScript"};

for (String lang: languages){
System.out.println(lang);
}
---------------------------------------------
int a=10, b=20;
int [] mathOperations = novel int[3];
mathOperations[0]=a+b;
mathOperations[1]=a-b;
mathOperations[2]=a*b;

for (int operation: mathOperations){
System.out.println(operation);
}
---------------------------------
int [] mathOperations = novel int[3];
mathOperations[0]=a+b;

for (int operation: mathOperations){
System.out.println(operation);
}
------------------------------------------------
3) Branching Statements

> Branching Statements are used to transfer command from ane signal to roughly other inward the code
> Branching Statements are defined past times the 3 keywords- break, continue, as well as return.

1) break

> interruption controversy is used to terminate the execution as well as comes out of loop.
> Mostly interruption controversy used inward switch, as well as inward loops likewise nosotros tin hand the sack role it.

Example:
for (int i=1; i<=10; i++){
System.out.println(i);
if (i == 4){
break;
}
------------------------------
2) continue

continue controversy is likewise same equally interruption statement, the solely departure is when interruption controversy executes it comes out of loop, whereas maintain controversy comes out from the loop temporarily.

Example:
for (int i=1; i<=10; i++){
if (i%2 == 0){
continue;
}
System.out.println(i);
}
-----------------------------------
3) return 

return controversy is used inward user defined methods (methods amongst provide value), which returns a value or controversy from electrical current to calling provide controversy must hold out ever final controversy inward the method.

Example:
public int add(int a, int b){
int consequence = a+b;
return result;
}
public static void main(String[] args) {
VariablesOperators sirisha = novel VariablesOperators();
int d= sirisha.add(10, 30);
System.out.println(d);//40
System.out.println(sirisha.add(10, 40));//50
--------------------------------------------
public bird VariablesOperators {
public static void main(String[] args) {
VariablesOperators sirisha = novel VariablesOperators();
int d= sirisha.add(10, 30);
System.out.println(d);//40
System.out.println(sirisha.add(10, 40));//50
}
public int add(int a, int b){
int consequence = a+b;
return result;
}

}
-----------------------------------------------
Note: 
> Write user defined methods earlier primary method or afterward primary method (with inward bird only)
> Call user defined (Non-static) methods inside primary method only
> Don't write user defined methods inside primary method.
---------------------------------------------------------------------
Also Watch:
Java Step past times Step Videos

Sumber http://www.gcreddy.com/
Post a Comment (0)
Previous Post Next Post