Control and Branching Structures in Java.

In this article, Control and Branching Structures in Java is explained in detail with examples.
Control Structure.
In general program execution starts with main() and it follows line by line/linear/sequential flow of execution takes place until the end of main().
Ex:
public static void main(String args[])
{-----
------
body   // (Linear Flow of Execution/Step by step execution)
-----  
------
}
However, to have a non-linear flow of execution in a program we use control structures. They are divided into two types,
The statements inside our source files are generally executed from top to bottom. Control flow statements, however, break up the flow of execution by employing decision making, looping, and branching, enabling your program to conditionally execute particular blocks of code by the Java programming language.   (From topic Control and Branching Structures in Java)
  • Branching (block is executed only once, this section describes the decision-making statements if-thenif-then-elseswitch),
  • Looping control structures (block is executed more than once,this section describes the for,while,do-while).
Control Structure means executing a block of code using a condition.
Control Structure-javaform
Branching Control Structure.
These control structures are executed only once for a particular block of code based on a condition.
1.Simple If                   (From topic Control and Branching Structures in Java)
Syntax
if(condition)
{
------
----
body;
-----
------
}
Here, if the condition is true then the body of the control structure is executed and comes to next statement.
If the condition is false then the control comes directly to the next statement. (From topic Control and Branching Structures in Java)
Branching Control Structure-javaform
Ex
// program related to Control and Branching Structures in Java--simpleif
class SimpleIfStatement
{
public static void main(String args[])
{
char ch='$'; //local variable
ch='X';
ch='A';
if(ch=='X') //here value is updated to 'A' but we are giving X output is.
{
System.out.println("Hello World");
}
System.out.println("End the program");
}
}
Output
Simple If Statement-javaform

Ex
// program related to Control and Branching Structures in Java--simpleif
class SimpleIfStatement
{
public static void main(String args[])
{
char ch='$'; //local variable
ch='X';
ch='A';
if(ch=='A') //here value is updated to A output will be. 
{
System.out.println("Hello World");
}
System.out.println("End the program");
}
}
Output
Branching control Structure-Javaform

Ex
// program related to Control and Branching Structures in Java--simpleif
class SimpleIfStatement
{
public static void main(String args[])
{
if(true)
{
System.out.println("Hello World");
}
System.out.println("End the program");
}
}
Output
Branching Control Structure-output-javaform

Ex
// program related to Control and Branching Structures in Java--simpleif
class SimpleIfStatement
{
public static void main(String args[])      //(From topic Control and Branching Structures in Java)
{
if(false)
{
System.out.println("Hello World");            
}
System.out.println("End the program");
}
}
Output
                            To get the code file use me.

Continue to the next topic Multiple if and 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

 Do check my new startup Surprise Planners in HyderabadLavish Surprises our services are surprise party planners in Hyderabad, surprise giftssurprise birthday party planners Hyderabad, Wedding anniversary surprises, surprise planners Hyderabad.

Hi Friends, Please comment down your views in the comment section below. Thank you...

No comments:

Connect with Us by Subscribing here for more Updates..!

* indicates required