0% found this document useful (0 votes)
10 views24 pages

Java Control Structures Explained

The document provides an overview of Java control structures, categorizing them into conditional statements, looping statements, and unconditional statements. It details various types of conditional statements such as simple if, if-else, if-else-if, and switch-case, along with examples. Additionally, it covers looping constructs like for, while, and do-while loops, and discusses unconditional statements like break and continue.

Uploaded by

hrock982
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views24 pages

Java Control Structures Explained

The document provides an overview of Java control structures, categorizing them into conditional statements, looping statements, and unconditional statements. It details various types of conditional statements such as simple if, if-else, if-else-if, and switch-case, along with examples. Additionally, it covers looping constructs like for, while, and do-while loops, and discusses unconditional statements like break and continue.

Uploaded by

hrock982
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

| WT Lab

JAVA CONTROL STRUCTURES

CONTROL STRUCTURES

 In general, programming / code will be executed sequentillay. In


such situations, code can be altered based on the needs. This is done
by using the constrol structures

 In java, control structures can be classified as three groups. They are


1. Conditional Statements (Selection or Decision Making Statements)
2. Looping Statements (Iterative Statements)
3. Unconditional Statements
1. CONDITIONAL STATEMENTS (BRANCHING STATEMENTS)

 Conditional statements allow to execute a statement or group of the


statements based on the condition / rule

 Java language supports following types of conditional statements. They


are
 Simple if
 If else statements
 If else –if statements
 Switch case statements

Java Control Structures Page 1


| WT Lab

SIMPLE IF

 It execute a statement based on the specified condition is true else it will


not execute the statements.
Syntax

if(condition / expression)
{
// code
}

I. EXAMPLE OF SIMPLE IF
SOURCE CODE
package conditional;
public class SimpleIf
{
public static void main(String[] args) {
[Link]("-----------------------------------------");
[Link]("\tSimple If");
[Link]("-----------------------------------------");
// local variable
int k=55;
if(k%2==1)
{
[Link]("Number : "+k);
[Link]("Given Number is Odd...");
}

Java Control Structures Page 2


| WT Lab

}
}
OUTPUT

IF ELSE STATEMENTS

 It has sepearte if and else block.


 It execute a statement based on the specified condition is true else it will
execute the else statements.
Syntax

if(condition / expression)
{
// true statements
}
else
{
// false statements
}

Java Control Structures Page 3


| WT Lab

II. EXAMPLE OF IF ELSE


SOURCE CODE
package conditional;
// load this package for runtime inputs
import [Link].*;
public class If_else {
public static void main(String[] args)throws Exception {
[Link]("-----------------------------------------");
[Link]("\tIf-Else Example");
[Link]("-----------------------------------------");
// define DataInputStream class for dynamic input
DataInputStream ds=new DataInputStream([Link]);
// read a number which is string formatted number
[Link]("Enter a number: ");
String str=[Link]();
// convert string formatted number to integer
int num=[Link](str);
if(num%2==0)
{
[Link]("Given Number is Even...");
}
else
{
[Link]("Given Number is Odd...");
}
}

Java Control Structures Page 4


| WT Lab

OUTPUT: (FIRST INPUT)

OUTPUT: (SECOND INPUT)

Java Control Structures Page 5


| WT Lab

IF ELSE-IF STATEMENTS

 It has single if and multiple else if statements


 It is used to execute the several conditions
Syntax

if(condition / expression)
{
// true statements
}
else if
{
// false statements 1
}
else if
{
// false statements 2
}

else
{
// false statement
}

Java Control Structures Page 6


| WT Lab

III. EXAMPLE OF IF ELSE-IF STATEMENTS


SOURCE CODE
// user defined package
package conditional;
// import this pacckage for runtime input
import [Link].*;
public class If_ElseIf
{
public static void main(String[] args) throws Exception
{
// local variables
int a,b,c;
[Link]("-----------------------------------------");
[Link]("\tIf Else-Ifs Example");
[Link]("-----------------------------------------");
// define DataInputStream class for dynamic input
DataInputStream ds=new DataInputStream([Link]);
// read a number which is string formatted number
[Link]("Enter the first number: ");
String s1=[Link]();
// convert string to integer using parseInt() static method of Wrapper class
a=[Link](s1);
// read a number which is string formatted number
[Link]("Enter the second number: ");
String s2=[Link]();
// convert string to integer using parseInt() static method of Wrapper class

Java Control Structures Page 7


| WT Lab

b=[Link](s2);
// read a number which is string formatted number
[Link]("Enter the third number: ");
String s3=[Link]();
// convert string to integer using parseInt() static method of Wrapper class
c=[Link](s3);
// check the condition for biggest of three numbers
if((a>b)&&(a>c))
{
[Link](a+" is Big Number...");
}
else if(b>c)
{
[Link](b+" is Big Number...");
}
else
{
[Link](c+" is Big Number...");
}
}
}

Java Control Structures Page 8


| WT Lab

OUTPUT

SWITCH CASE STATEMENTS

 It is an optional for if-else-if statements


 It is used to execute the statements based on the label condition is true.
 Case label can be integer or char or string.

Java Control Structures Page 9


| WT Lab

Syntax

switch(condition)
{
case label-1:
// true statements
break;
case label-2:
// true statements
break;

Default:
Break;
}

Java Control Structures Page 10


| WT Lab

IV. EXAMPLE OF SWITCH CASE STATEMENTS


SOURCE CODE
package conditional;
public class Switch
{
public void disp()
{
int day=5;
[Link]("Code: "+day);
// switch case
switch(day)
{
case 1:
[Link]("Given day is Monday...");
break;
case 2:
[Link]("Given day is Tuesday...");
break;
case 3:
[Link]("Given day is Wednesday...");
break;
case 4:
[Link]("Given day is Thursday...");
break;
case 5:
[Link]("Given day is Friday...");

Java Control Structures Page 11


| WT Lab

break;
case 6:
[Link]("Given day is Saturday...");
break;
case 7:
[Link]("Given day is Sunday ...");
break;
default:
[Link]("Invalid code...");
break;
}
}
public static void main(String[] args)
{
[Link]("-----------------------------------------");
[Link]("\tSwitch Case Example");
[Link]("-----------------------------------------");
// object creation for current class
Switch obj=new Switch();
// accessing instance method using object
[Link]();
}
}

Java Control Structures Page 12


| WT Lab

OUTPUT

2. LOOPING STATEMENTS (ITERATIVE STATEMENTS)

 Looping statements allow to execute a statement or group of the


statements multiple times based on the condtion / expression.

 Java language supports following loop statements. They are


1. For loop
2. While Loop
3. Do While Loop
1. For loop

 It is used to execute a block of the statements number of times

 It includes initialization, condition and increment / decrement


parts to complete the tasks

Java Control Structures Page 13


| WT Lab

Syntax

for(initialization; condition; increment / decrement)


{
// user code
}

V. EXAMPLE OF FOR LOOP


SOURCE CODE
package looping;
public class Forloop
{
public static void main(String[] args)
{
[Link]("-------------------------------------");
[Link]("\tFor Loop Example");
[Link]("-------------------------------------");
for(int i=0;i<5;i++)
{
[Link]("i= "+i);
}
}
}

Java Control Structures Page 14


| WT Lab

OUTPUT

2. While Loop

 It is a alternative for java for loop

 It is used to execute the statements, if the condition is true


otherwise it will skip the loop
Syntax

initialization
while(condition)
{
// code
// increment or decrement
}

Java Control Structures Page 15


| WT Lab

VI. EXAMPLE OF WHILE LOOP


SOURCE CODE
package looping;
import [Link].*;
public class Whileloop
{
public static void main(String[] args)throws Exception
{
[Link]("-------------------------------------");
[Link]("\tWhile Loop Example");
[Link]("-------------------------------------");
int f=1, i=0,n;
// define DataInputStream class for dynamic input
DataInputStream ds=new DataInputStream([Link]);
// read a number which is string formatted number
[Link]("Enter the Number: ");
String str=[Link]();
// convert string to integer using parseInt() static method of Wrapper
class
n=[Link](str);
while(i<n)
{
f=f*(i+1);
// increment i be one
i++;
}

Java Control Structures Page 16


| WT Lab

[Link]("Factorial of Given Number "+n+" : "+f);


}
}

OUTPUT

3. Do While Loop

 It is a alternative for while loop

 Like while, it is used to execute the statements, if the condition is


true otherwise it will skip the loop

 Unlike while loop, it will execute the statements at least once if the
condition fails. Because the condition part will be placed at the end of
the loop.

 Unlike while loop, the condition will be terminated by semicolon.

Java Control Structures Page 17


| WT Lab

DIFFERENCE BETWEEN WHILE LOOP AND DO WHILE LOOP


S.N WHILE LOOP DO WHILE LOOP
1. Entry control loop Exit control loop

 Here it first checks the  Here it checks the condtion


condition at the time of entry for exit and if the condition
and will execute the for the exit evaluation is true
statements if the condition then control will exit from
or expression is true the loop else control will
enter again into the loop.
 Ex. for loop, while loop
 Ex. do while loop
2. Body of the statements won’t be Body of the statements will be
executed if the condtion is false executed at least once even the
(not true) condition is false
3. It takes less time to execute and It takes more time to execute and
the code is shorter the code is longer

Syntax

initialization
do
{
// code
// increment or decrement
} while(condition);

Java Control Structures Page 18


| WT Lab

VII. EXAMPLE OF DO WHILE LOOP


SOURCE CODE
package looping;
public class Dowhile
{
public static void main(String[] args)
{
[Link]("----------------------------------------");
[Link]("\tDo-While Loop Example");
[Link]("----------------------------------------");
int i=0, n=5;
do
{
[Link]("i= "+(i+1));
i++;
}while(i<n);
}
}

Java Control Structures Page 19


| WT Lab

OUTPUT

3. Unconditional Statements (Jumping Statements)

 Without any condtion, these statements are altering the sequence of


programs.

 It is also called as jumping statement

 Java supports the following type of unconditional statements. They are


1. Break
2. Continue
3. Return

Java Control Structures Page 20


| WT Lab

1. Break Statement

 It is used with selection statements like if statement


 It is used for the following purposes. They are
 To exit a loop
 Terminate a sequence in a swtich statement.
Syntax

if (condition)
break;

VIII. EXAMPLE OF BREAK STATEMENT


SOURCE CODE
package unconditional;
public class Break
{
public static void main(String[] args)
{
[Link]("-------------------------------------");
[Link]("\tBreak Example");
[Link]("-------------------------------------");
for(int i=0;i<10;i++)
{
if(i>5)
break;
else
[Link]("i= "+i);

Java Control Structures Page 21


| WT Lab

}
[Link]("End of the main method...");
}
}

OUTPUT

2. Continue Statement

 It is also used with selection statements like if statement and inside the
looping statements such as for loop, while loop, etc, …
 It is used for the following purposes. They are
 To restart a loop
 Control directly jumps to beginning of the loop for the next iteration,
skipping the current iteration of the loop’s body if the condition is
true.
Syntax

if (condition)
continue;

Java Control Structures Page 22


| WT Lab

IX. EXAMPLE OF CONTINUE STATEMENT


SOURCE CODE
package unconditional;
public class Continue
{
public static void main(String[] args)
{
[Link]("-------------------------------------------");
[Link]("\tJava Continue Example");
[Link]("-------------------------------------------");
for(int i=0;i<10;i++)
{
if(i==3 || i==5)
continue;
else
[Link]("i= "+i);
}
[Link]("End of the main method...");
}
}

Java Control Structures Page 23


| WT Lab

OUTPUT

Java Control Structures Page 24

You might also like