In this article,Nested-if control Structure is explained in detail with examples.
In this, you will come to know about the Nested if control structure Using an if-condition block inside another if condition block is known as nested-if.
In this, you will come to know about the Nested if control structure Using an if-condition block inside another if condition block is known as nested-if.
Syntax
if(condition 1)
{
outer true part;
}
if(condition 2)
{
Inner true part;
}
----
}
Hence, if condition 1is true then outer- if true part is executed also if condition 2 is true then only inner-if true part is executed. (From topic Nested-if control Structure)
Ex
// program related to Nested-if control Structure--nested-if.class NestingIfExample
{
public static void main(String args[])
{
int a,b,c;
a=100;
b=50;
c=300;
if(a>b) //Here condition is true so it enters into outer true part.
{if(a>c) // Here condition is false so it goes into else block and prints C is greatest.
{
System.out.println("A is greatest");
}
else
{
System.out.println("C is greatest");
}
}
else
{
if(b>c)
{
System.out.println("B is greatest");
}
else
{
System.out.println("C is greatest");
}
}
}
}
Output
C is greatest
// program related to Nested-if control Structure--nested-if.
class NestingIfExample
{
public static void main(String args[]) //(From topic Nested-if control Structure)
{
int a,b,c;
a=10;
b=50;
c=30;
if(a>b) //Here condition is false so it enters into else block of inner part.
{if(a>c)
{
System.out.println("A is greatest");
}
else
{
System.out.println("C is greatest");
}
}
else
{
if(b>c)
{
System.out.println("B is greatest");
}
else
{
System.out.println("C is greatest");
}
}
}
}
Output
B is greatest
Ex
// program related to Nested-if control Structure--nested-if.class NestingIfExample
{
public static void main(String args[])
{
int a,b,c;
a=100;
b=50;
c=30;
if(a>b) //Here condition is true so it enters into outer true part.
{
if(a>c) // Here condition is true so it goes into block and prints A is greatest.
{
System.out.println("A is greatest");
}
else
{
System.out.println("C is greatest");
}
}
else
{
if(b>c)
{
System.out.println("B is greatest");
}
else
{
System.out.println("C is greatest");
}
}
}
}
Output
A is greatest
We are sorry, From previous three articles we are not providing the code files as we are entered into the concepts you need to practice.So copy the code, if any errors find them compile them and learn. All the best. (From topic Nested-if control Structure)
continue to next topic Cascading if else control structure.
Begin your career in Digital Marketing,What is digital marketing? Digital Marketing online course. It's an current evolving technology which can give support to establish your own startup through Digital Marketing
Hi Friends, Please comment down your views in the comment section below. Thank you...
No comments:
Post a Comment