This document discusses various control flow statements in Java including branching statements, looping statements, and jump statements. It provides examples of if, if-else, if-else-if statements, switch statements, for loops, while loops, do-while loops, break, continue, and return statements. Key points include:
- Branching statements like if, if-else, if-else-if are used to control program flow based on boolean conditions. Switch statements provide an alternative for multiple if-else statements.
- Looping statements like for, while, do-while repeat a block of code while/until a condition is met.
- Jump statements like break and continue control flow within loops, while
Introduction to Control Flow Statements like Branching, Looping, and Jump Statements.
Explains Branching Statements: If, If-Else, Nested If, and Switch statements with code examples.
Describes Loop Control Statements including For Loop, For Each Loop, While Loop, and Do-While Loop.
Details on Jump Statements: Break, Continue, and Return with usage examples and explanations.Exercises to find errors and correct code snippets demonstrating control flow concepts.
If Statement
• Anif statement consists of a Boolean expression followed by one or more
statements. If the Boolean expression evaluates to true then the block of
code inside the if statement will be executed. If not the first set of code
after the end of the if statement (after the closing curly brace) will be
executed.
if(Boolean expression)
{
//Some code should be work
}
public static void main(String[] y)
{
int my_int=25;
if(my_int==25)
{
System.out.println("the condition is true");
}
}
Go to branch statement slide
4.
If else Statement
•An if statement can be followed by an optional else statement,
which executes when the Boolean expression is false.
if(Boolean expression)
{ //some code excut }
else
{ //some code excut }
public static void main(String[] y)
{
int my_int=25;
if(my_int<25)
{ System.out.println("the condition is true"); }
else
{ System.out.println("the condition is not true"); }
}
• Go to branch statement slide
5.
If…else…if statement
• Anif 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.
if(Boolean Expression 1)
{ //some code }
else if (Boolean Expression 2)
{ //some code }
else if (Boolean Expression 2)
{ //some code }
else
{ //some code }
6.
public static voidmain(String[] y)
{
int my_int=25;
if(my_int<25)
{
System.out.println("the condition is true");
}
else if(my_int>25)
{System.out.println("the else if condition is true for 25 >
25");
}
else if(my_int==25)
{
System.out.println("the condition is true for 25==25");
}
else
{
System.out.println("any of the condition is not ture");
}
}
Go to branch statement slides
7.
Nest 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.
if(boolean expression 1)
{
if(boolean expression 2)
{ //some code }
else
{ //some code }
}
8.
public static voidmain(String[] y)
{
int course_value=90;
String course_name="java";
String Course_type="core & advanced";
if(course_value==90)
{
if(course_name=="java")
{
if(Course_type=="core & advanced")
{
System.out.println("the nested condition is
true");
}
}
}
else
{
System.out.println("the nested conditions are not true");
}
}
Go to Branch statement slide
9.
Switch Statement
• Switchcase statements are a substitute for long if statements that
compare a variable to several "integral" values.. The value of the variable
given into switch is compared to the value following each of the cases, and
when one value matches the value of the variable, the computer
continues executing the program from that point.
switch(expression)
{
case value1: {//some code}break;
case value2: {//some code}break;
default: {//some code}break;
}
10.
public static voidmain(String[] y)
{
int x=10;
/*
* x value is compared with case values
*/
switch(x)
{
case 10:{System.out.println("the x value is 10");}break;
case 30:{System.out.println("the x value is 30");}break;
case 40:{System.out.println("the x value is 40");}break;
}
}
Go to branch statement slide
11.
LOOP control Statements
•loop is a sequence of instruction s that is continually repeated until a
certain condition is reached.
There are four types of loops:
• For loop
• For each loop
• While loop
• Do..While loop
<<goto first slide>>
12.
For loop
• Thefor is an entry-entrolled loop and is used when an action is to
repeated for a predetermined number of times
for(initial value; test condition; increment)
{ //some code }
public static void main(String[] y)
{
for(int i=0; i<5; i++)
{System.out.println("hello world "+i);}
}
Go to loop slide
13.
Foreach Statement
• AForEach style loop is designed to cycle through a collection of
objects,such as an array, in strictly sequential fashion, from start to
finish.Implement a foreach loop by using the keyword foreach, java adds
the foreach compability by enhancing the for statement.
• for(type itr-var:collection)statement-block
int[] a={1,2,3,4};
for(int x:a)
{
System.out.println("array of a["+x+"] "+a[x]);
}
Go to loop slide
14.
While loop(Entry Controlloop)
• It repeates a statement or block while its controlling expression(any boolean
Expression) is true
• The body of the loop will be executed as long as the conditional expression is
true.
while(condition)
{
//body of code
}
public static void main(String[] y)
{
while(x<5)
{ /*x++ increment value of x;*/
System.out.println("the value of x is "+x);
x++;
}
}
Go to loop slide
15.
Do-while loop
• Awhile loop is initially false then the body of the loop will not be executed at
all .The do-while loop always executes its body at least once because its
conditional expression is at the bottom of the loop
do
{ //body of code
}while(boolean expression);
System.out.println("the do While loop");
int x=1;
do
{
/*x++ increment value of x;
*/
System.out.println("the value of x is "+x);
x++;
}while(x<5);
Go to loop slided
Jump Statements
• Break
•In java the break statement has 3 uses
• Terminates a statement sequence in a switch statement
• It can be used to exit loop
• It can be used more civilized form of goto statement
• Using Break to exit the loop
By using break, you can force immediate termination of a loop .when break
statement is encountered inside a loop the loop is terminated and
program control resumes at the next statement following loop
18.
public static voidmain(String[] y)
{
for(int i=0;i<10;i++)
{
if(i==5)
{
System.out.println("condition break");
break;
}
System.out.println("the value of i is::"+i);
}
}
19.
• Using breakin the form of GOTO
• Syntax Break label_name;
public static void main(String[] y)
{
first:{
secound:{
third:{
System.out.println("third block");
if(true)
{
break secound;
}
}
System.out.println("secound block");
}
System.out.println("first block");
}
}
go to jump statements
20.
Continue
• A continuestatement causes control to be transferred directly to the
conditional expression that controls the loop
public static void main(String[] y)
{
for(int i=0;i<5;i++)
{
if(true)
{
System.out.println("the if block");
continue;
}
System.out.println("the for loop");
}
}
21.
Using continue withlabels
• Specify the labels with continue statement
public static void main(String[] y)
{
outer:for(int i=0;i<=2;i++){
for(int j=0;j<=1;j++)
{
System.out.println("value of J is::"+j);
continue outer;
}
System.out.println("the value of i is::"+i);
}
/*
for continue labels it should be used with loops only not for blocks
*/
}
Go to jump statements
22.
Return
• The returnstatement is used to explicitly return from a method.That is,it
causes program control to transfer back to the caller of the method.At any
time in a method the return statement can be used to cause execution to
branch back to the caller of the method.Thus,the return statement can be
used to cause execution to branch back to the caller ot the method.Thus, the
return statement immediately terminates the method in which it is executed.
public static void main(String[] y)
{
System.out.println("before return statement");
if(true)
{
return;
}
System.out.println("after return statement");
/*
here return causes execution to return to the java run-time system
*/
}