In this article, Cascading If-else control Structure is explained in detail with examples. you will come to know about what is the cascading If-else control structure in Java, how it is defined in java program.Syntax is written as below
Syntax
if (condition 1)
{
true part1;
}
else if (condition 2)
{
true part2;
}
else if (condition 3)
{
true part3;
}
----
else
{
---
}
- Here,whichever condition is true,its corresponding true part is executed, and directly control comes out of the structure, by skipping remaining conditions and blocks.
- If none of the conditions is true then the last else block is executed. (From topic Cascading If-else control Structure)
Ex
// program related to Cascading If-else control Structure--cascading if-else
class IfElseCascade
{
public static void main(String args[])
{
int i;
char ch;
ch='x';
if(ch=='x')
i=10;
else if(ch=='y') //(From topic Cascading If-else control Structure)
i=20;
else if('ch=='z')
i=30;
else
i=40;
System.out.println("Number value I is: "+i);
}
}
Output
Number value I is: 10
Ex
// program related to Cascading If-else control Structure--cascading if-else
class IfElseCascade
{
public static void main(String args[])
{
int i;
char ch;
ch='y';
if(ch=='x')
i=10;
else if(ch=='y')
i=20;
else if('ch=='z')
i=30;
else
i=40;
System.out.println("Number value I is: "+i);
}
}
Output
Number value I is: 20
Ex
// program related to Cascading If-else control Structure--cascading if-else
class IfElseCascade
{
public static void main(String args[])
{
int i;
char ch;
ch='z';
if(ch=='x')
i=10;
else if(ch=='y')
i=20;
else if('ch=='z')
i=30;
else
i=40;
System.out.println("Number value I is: "+i);
}
}
Output
Number value I is: 30
Ex
//program related to Cascading If-else control Structure--cascading if-else
class IfElseCascade
{
public static void main(String args[])
{
int i;
char ch;
ch='A';
if(ch=='x')
i=10;
else if(ch=='y')
i=20;
else if('ch=='z')
i=30;
else
i=40;
System.out.println("Number value I is: "+i);
}
}
Output
Number value I is:40
Note
Paste the above-given program in the notepad and save the program as classname.java (i.e, IfElseCascade.java) and run the program using the command javac.IfElseCascade.java to get the class file.To execute the command is java IfElseCascade and check the output. (From topic Cascading If-else control Structure)
Continue to the next topic Switch case 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
Do check my new startup Surprise Planners in Hyderabad- Lavish Surprises our services are surprise party planners in Hyderabad, surprise gifts, surprise birthday party planners Hyderabad, Wedding anniversary surprises, surprise planners Hyderabad.
Hi Friends, Please comment down your views in the comment section below. Thank you...
Hi Friends, Please comment down your views in the comment section below. Thank you...
No comments:
Post a Comment