Wednesday, 16 August 2017

Decision
making is one of the serious facilities which a programming language has to grant.
To understand Java better we have to understand this post and practice by
writing some decision making code

There
are two types of decision making statements in Java. One is very usually
used which is If statement and you will find it about in every piece
of code. Second is Switch statement.

If statement:

The
if statement is Java’s conditional branch statement.it return true or
false.

If
the Boolean expression is evaluated to true, statement(s) inside
the body of if (statements inside parenthesis) are executed.

Syntax

If(Boolean expression)

{

// Code here

}

Example :

Code

public class if_demo

{

public static void
main(String args[])

{

int
iNum1 = 20;

int
iNum2 = 10;

if(iNum1>iNum2)

{

System.out.println("iNum1
is greater than iNum2");

}

}

}

Output:

iNum1 is greater than iNum2

if else :

This
if else work like : if condition is true than statement1 is executed or else
statement2 is executed.

If
the Boolean condition is evaluated to true, statement(s) inside the
body of if (statements inside parenthesis) are executed.

If
the Boolean condition is evaluated to false, statement(s) inside the
body of if are skipped from execution.

Syntax

if(Boolean condition)

{

// statement1

}

else

{

// statement2

}

Example :

Code

public class if_else_demo

{

public static void main(String args[])

{

int
iNum1 = 10;

int
iNum2 = 20;

if(iNum1>iNum2)

{

System.out.println("iNum1 is greater
than iNum2");

}

else

{

System.out.println("iNum2 is less
than iNum1");

}

}

}

Output:

iNum2 is less than iNum1

If-else if

The
if statement executed from top to down. the moment one of the condition is
true, statement associated with that if statement executed.

If
none of the condition is true than else statement will be executed, only one of
the statement executed from list of else if statements.

Syntax

if(Boolean condition)

{

// statement1

}

else if(Boolean condition)

{

// statement2

}

else if(Boolean condition)

{

// statement3

}

........

else

{

// else statement

}

Example:

Code

public class if_else_if_demo

{

public static void main(String args[])

{

int
prsntg = 60;

if(prsntg
>= 70)

{

System.out.println("First class
with Distinction");

}

else
If(prsntg >= 60)

{

System.out.println("First
Class");

}

else
If(prsntg >= 50)

{

System.out.println("Second
Class");

}

else
If(prsntg >= 35)

{

System.out.println("Pass
Class");

}

else

{

System.out.println("Fail");

}

}

}

Output:

First Class

switch statement:

Switch
statement give an easy way to send off execution to dissimilar parts of
your code based on the value of an expression. It is use to replace
multilevel if-else-if statement. But the switch statement
can only be used if the conditions are based on the same regular value. The
syntax for switch statement is :

Syntax

switch(expression)

{

case value 1:

//
statement 1

break;

case value 2:

//
statement 2

break;

case value n:

//
statement n

break;

default:

//statements

break;

}

Example :

Code

public class switch_case_demo

{

public
static void main(String args[])

{

int
iDay = 5;

switch(iDay)

{

case
1:

System.out.println("Today
is Monday");

break;

case
3:

System.out.println("Today
is Tuesday");

break;

case
4:

System.out.println("Today
is Wednesday");

break;

case
5:

System.out.println("Today
is Thursday");

break;

case
6:

System.out.println("Today
is Friday");

break;

case
7:

System.out.println("Today
is Saturday");

break;

default:

System.out.println("Today
is Sunday");

break;

}

}

}

Output:

Today is Thursday

In
this post we learned if, if else, if else if, switch case with example,