google.com, pub-8786015629279405, DIRECT, f08c47fec0942fa0 Control Statement in Java Programming

Control Statement in Java Programming

0

 Control Statement in Java



Decision-making structures require that the programmer specifies one or more conditions to be evaluated or tested by the program, along with a statement or statements to be executed if the condition is determined to be true, and optionally, other statements to be executed if the condition is determined to be false. Shown below is the general form of a typical decision-making structure found in most of the programming languages



Java programming language assumes any non-zero and non-null values as true, and if it is either zero or null, then it is assumed as false value.

if statement

Statement execute set of command like when condition is true and its syntax is


If (condition)
{
Statement;
}

The statement is executed only when condition is true. If the if statement body is consists of several statement then better to use pair of curly braces. Here in case condition is false then compiler skip the line within the if block.




class control
{
public static void main(String args[])
{
int n=11;

If (n>10)
System.out.println (“ number is grater”);
}
}



Output:

Number is greater

if…..else ... Statement

it is bidirectional conditional control statement that contains one condition & two possible action. Condition may be true or false, where non-zero value regarded as true & zero value regarded as false. If condition are satisfy true, then a single or block of statement executed otherwise another single or block of statement is executed.


Its syntax is:-


if (condition)
{
Statement1;
Statement2;
}
else
{
Statement1;
Statement2;
}

Else statement cannot be used without if or no multiple else statement are allowed within one if statement. It means there must be a if statement with in an else statement.






Example:-

/* To check a number is eve or odd */

class control
{
public static void main(String args[])
{
int n=121;
If (n%2==0)
System.out.println (“even number”);
else
System.out.println(“odd number”);
}


Output: 

odd number

Nesting of if …else

When there are another if else statement in if-block or else-block, then it is called nesting of if-else statement.


Syntax is :-


if (condition)

{
If (condition)
{
Statement1;
}
else
{
statement2;
}
}
Statement3;

If….else LADDER

In this type of nesting there is an if else statement in every else part except the last part. If condition is false control pass to block where condition is again checked with its if statement.


Syntax is :-

if (condition)
{
Statement1;
}
else if (condition)
{
statement2;
}
else if (condition)
{
statement3;
}
else
{
statement4;
}
This process continue until there is no if statement in the last block. if one of the condition is satisfy the condition other nested “else if” would not executed. But it has disadvantage over if else statement that, in if else statement whenever the condition is true, other condition are not checked. While in this case, all condition are checked.

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.

Thank you for your interest 😊

We will back shortly after reviewing...

Thank you for your interest 😊

We will back shortly after reviewing...

Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Accept !
To Top