0% found this document useful (0 votes)
22 views23 pages

Control Structure

The document discusses different types of control structures in Java including sequence, selection, and repetition statements. It provides examples of if, if-else, if-else if-else, switch, while, do-while, and for statements. The control structures determine the flow and sequence of a Java program.

Uploaded by

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

Control Structure

The document discusses different types of control structures in Java including sequence, selection, and repetition statements. It provides examples of if, if-else, if-else if-else, switch, while, do-while, and for statements. The control structures determine the flow and sequence of a Java program.

Uploaded by

sultan jemal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 23

Control Structure

• Java will execute the statements in your code in a


specific sequence or flow.
• Control structures are the flow of the program; the
sequence of statements that are executed in a
program.
• They act as “direction signals” to control the path a
program takes.

1
Overview of Java statements
• Java has three kinds of control structures
 Sequence statement
 Selection (decision making) statements
• allows the code to execute a statement or blocks statements conditionally.
if statement
switch statement
 Repetition statements
for statement
 Performs the actions in its body zero or more time
while statement
 Performs the actions in its body zero or more times
do…while statement
 Performs the actions in its body one or more times2
The if Statement
• An if statement consists of a Boolean expression
followed by one or more statements.
• Execute an action if the specified condition is true.
• Syntax:
if(Boolean_expression)
{
/* Statement_1
Statement_2
Statement_n */
}
3
Example
public class IfStat{
public static void main(String args[]){
int x =10;
if( x <20){
System.out.print("This is if statement");
}
}
}

4
The if...else Statement
• An if statement can be followed by an optional else
statement, which executes when the Boolean
expression is false.
• Syntax:
if(Boolean_expression)
{
/*statements*/
} else {
/*statements*/
}
5
Example
public class IfElse{
public static void main(String args[]){
int x =30;
if(x <20){
System.out.print("This is if statement");
} else {
System.out.print("This is else statement");
}
}
}
6
The if...else if...else Statement
• An if statement can be followed by an optional else
if...else statement, which is very useful to test various
conditions using single if...else if statement.
• When using if, else if , else statements there are few
points to keep in mind.
– An if can have zero or one else's and it must come
after any else if's.
– An if can have zero to many else if's and they must
come before the else.
– Once an else if succeeds, none of the remaining
else if's or else's will be tested.
7
Syntax
if(Boolean_expression1){
/* statements */
} else if(Boolean_expression2){
/* statements */
} else if(Boolean_expression3){
/* statements */
} else {
/* statements */
}

8
Nested if...else Statement
• It is always legal to nest if-else statements which
means you can use one if or else if statement inside
another if or else if statement.
• Syntax:
if(Boolean_expression1){
/* statements */
if(Boolean_expression2){
/* statements */
}
}
9
Example
public class Test{
public static void main(String args[]){
int x =30;
int y =10;
if( x ==30){
if( y ==10){
System.out.print("X = 30 and Y = 10");
}
}
}
10
Switch Statement
• A switch statement allows a variable to be tested for equality against a list of
values.
• Each value is called a case, and the variable being switched on is checked
for each case.
• Syntax:
switch(expression){
case value :
//Statements
break;
case value :
//Statements
break;
//You can have any number of case statements.
default:
//Statements
} 11
Example
public class Test{
public static void main(String args[]){
char grade = 'C';
switch(grade) {
case'A':
System.out.println("Excellent!");
break;
case'B':
t p u t:
System.out.println("Excellent!"); Ou one
e l l d
break; W d e is C
case'C': r g ra
System.out.println("Well done"); You
break;
default:
System.out.println("Invalid grade");
}
System.out.println("Your grade is "+ grade);
}
13
}
Repetition Statements
• Repetition statements allow us to execute a statement multiple
times repetitively.

• They are often simply referred to as loops.

• Like conditional statements, they are controlled by boolean


expressions.

• Java has three kinds of repetition statements: the while loop, the
do…while loop, and the for loop.

• The programmer must choose the right kind of loop for the
situation. 14
The while Statement
• The while statement has the following syntax:

while ( condition )
while is a
statement;
reserved word

If the condition is true, the statement is executed.


Then the condition is evaluated again.

The statement is executed repetitively until


the condition becomes false.

15
The while Statement
• Note that if the condition of a while statement is false
initially, the statement is never executed

• Therefore, the body of a while loop will execute zero or more


times.

• The body of a while loop must eventually make the condition


false.

• If not, it is an infinite loop, which will execute until the user


interrupts the program.

16
The while Statement
//While Loop Example
public class Main {
   public static void main(String args[]){
Output:
      int a= 10;
a : 10
      while( a < 20 ){ a : 11
      System.out.print("a : " + a ); a : 12
      a++; a : 13
      System.out.print("\n"); a : 14
   } a : 15
a : 16
   }
a : 17
} a : 18
a : 19 17
The do Statement
• The do statement has the following syntax:
Uses both do
the do and {
while statement;
reserved }
words while ( condition )

The statement is executed once initially, then the condition is evaluated

The statement is repetitively executed until the condition becomes false

18
The do Statement
• A do loop is similar to a while loop, except that the condition is
evaluated after the body of the loop is executed.
• Therefore the body of a do loop will execute at least one time.
//Do… While Loop Example
public class Main {
   public static void main(String args[]){ Output:
      int a= 3;
a:3
      do{
a:4
         System.out.print(" a : " + a );
         a++;
a:5
         System.out.print("\n"); a:6
      }while( a < 10 ); a:7
   } a:8
} a:9
19
The for Statement
• The for statement has the following syntax:

The initialization portion The statement is


Reserved
is executed once executed until the
word
before the loop begins condition becomes false

for ( initialization ; condition ; increment )


statement;

The increment portion is executed at the end of each iteration

20
The for Statement
• A for loop is equivalent to the following while
loop structure:
initialization;
while ( condition )
{
statement;
increment;
}

21
The for Statement
• Like a while loop, the condition of a for
statement is tested prior to executing the loop
body
• Therefore, the body of a for loop will execute
zero or more times
• It is well suited for executing a specific
number of times that can be determined in
advance

22
Example
public class Main {
public static void main(String[] args) {
int i,j;
for(i=0;i<=10;i++) { Output:
for(j=10;j>=1;j--) *
if(j<=i)
**
***
System.out.print("*");
****
else *****
System.out.print(" "); ******
System.out.println(); *******
} ********
} *********
**********
} 23
break and continue statements
• beak statement
– tells the compiler to exit the switch statement.
• continue statement
– this statement causes the program to jump to the
next iteration of the loop.

24

You might also like