SlideShare a Scribd company logo
1 of 28
Download to read offline
Flow Control,[object Object],DhrubojyotiKayal,[object Object]
What are flow control statements?,[object Object],if-else,[object Object],while,[object Object],do-while,[object Object],for,[object Object],for each,[object Object],return,[object Object],break,[object Object],continue,[object Object],switch,[object Object],Agenda,[object Object]
“Break up the flow of execution by employing decision making, looping, and branching, enabling a program to conditionally execute particular blocks of code” – Java Trails,[object Object],All conditional statements use the truth or falsehood of a conditional expression to determine the execution path. ,[object Object],conditional expression : a == b evaluates to true or false,[object Object],Any of the relational operators can be used to produce conditional statement,[object Object],Java doesn’t allow you to use a number as a boolean,[object Object],What are control statements?,[object Object]
if(Boolean-expression) ,[object Object],		statement ,[object Object],if(Boolean-expression) ,[object Object],		statement ,[object Object],	else ,[object Object],		statement,[object Object],else is optional ,[object Object],The Boolean-expression must produce a boolean result ,[object Object],The statement is either a simple statement terminated by a semicolon, or a compound statement, which is a group of simple statements enclosed in braces. ,[object Object],if-else,[object Object]
int a = 5;,[object Object],int b = 10;,[object Object],if(a > b),[object Object],System.out.println(“a is bigger”);,[object Object],else,[object Object],System.out.println(“b is bigger”);,[object Object],If-else in Action,[object Object]
Exercise,[object Object]
Write a Java program which compares two integers and prints which one in larger and the incremented or decremented value. Before printing however you should increment in the if block and decrement in the else block.,[object Object]
Looping is controlled by while, do-while and for, which are sometimes classified as iteration statements. ,[object Object],Statement(s) repeats until the controlling Boolean-expression evaluates to false ,[object Object],Iteration,[object Object]
while(Boolean-expression) ,[object Object],Statement,[object Object],The Boolean-expression is evaluated once at the beginning of the loop and again before each further iteration of the statement,[object Object],int x = 1,[object Object],while(x <= 100) {,[object Object],System.out.println(x);,[object Object],   x++;,[object Object],},[object Object],while,[object Object]
do ,[object Object],		statement ,[object Object],	while(Boolean-expression); ,[object Object],The statement of the do-while always executes at least once, even if the expression evaluates to false the first time ,[object Object],In a while, if the conditional is false the first time the statement never executes. ,[object Object],int x = 1;,[object Object],do {,[object Object],System.out.println(x);,[object Object],	x++;,[object Object],} while (x <= 5),[object Object],do-while,[object Object]
Exercise,[object Object]
Write a Java program to print the numbers from 1…100 using a while loop.,[object Object],In the while loop check and print if the number is odd or even.,[object Object]
for(initialization; Boolean-expression; step) ,[object Object],		statement ,[object Object],Performs initialization before the first iteration.,[object Object],Then it performs conditional testing,[object Object],At the end of each iteration, some form of “stepping.” ,[object Object],Any of the expressions initialization, Boolean-expression or step can be empty ,[object Object],The expression is tested before each iteration, and as soon as it evaluates to false, execution will continue at the line following the for statement. ,[object Object],At the end of each loop, the step executes. ,[object Object],Useful if you already know how many times the statements need to be repeated.,[object Object],foreach will also work with any object that is Iterable. ,[object Object],for,[object Object]
for(inti = 1 ; i <10 ; i++) {,[object Object],System.out.println(i);,[object Object],},[object Object],inti = 1;,[object Object],for(; i <10 ;) {,[object Object],System.out.println(i);,[object Object],i++;,[object Object],},[object Object],for loop in Action,[object Object]
Exercise,[object Object]
Rewrite the exercise with while loop, replacing it with a for loop.,[object Object]
New in Java SE 5,[object Object],Useful for iterating over arrays and collections,[object Object],int a [] = new int[100];,[object Object],for(inti = 0 ; i < a.length ; i++){,[object Object],	a[i] = i;,[object Object],},[object Object],for ( int x : a) {,[object Object],System.out.println(x);,[object Object],},[object Object],for each,[object Object]
Exercise,[object Object]
Write a Java program to populate an integer array with 100 elements. ,[object Object],Extend the program to print the numbers which are odd and even in that array using for each loop. ,[object Object]
branch or change control without any test,[object Object],return, break, continue ,[object Object],goto - skip,[object Object],Conditional Branching,[object Object]
It specifies what value a method will return (if it doesn’t have a void return value) ,[object Object],It causes the current method to exit, returning that value. ,[object Object],return,[object Object]
public intgetLarger(int a, int b) {,[object Object],	if(a > b),[object Object],		return a;,[object Object],	else,[object Object],		return b;,[object Object],},[object Object],return in action,[object Object]
break quits the loop without executing the rest of the statements in the loop. ,[object Object],for(inti = 1 ; i <=10 ; i++) {,[object Object],	if( i % 5 == 0) ,[object Object],		break;,[object Object],System.out.println(i);,[object Object],},[object Object],In-case of nested for or while it will break out of the innermost loop where break was encountered == {Home work},[object Object],break,[object Object]
continue stops the execution of the current iteration and goes back to the beginning of the loop to begin the next iteration ,[object Object],for (inti = 1; i <= 10 ;i++) {,[object Object],	if ( i % 3 == 0),[object Object],		continue;,[object Object],System.out.println(i);,[object Object],} ,[object Object],continue,[object Object]
The switch statement selects from among pieces of code based on the value of an integral expression ,[object Object],switch(integral-selector) { ,[object Object],		case integral-value1 : statement; break; ,[object Object],		case integral-value2 : statement; break; ,[object Object],		case integral-value3 : statement; break; ,[object Object],		case integral-value4 : statement; break; ,[object Object],		case integral-value5 : statement; break; ,[object Object],		default: statement; },[object Object],Integral-selector is an expression that produces an integer value,[object Object],The switch compares the result of integral-selector to each integral-value. If it finds a match, the corresponding statement (a single statement or multiple statements; braces are not required) executes.,[object Object],If no match occurs, the default statement executes. ,[object Object],Each case ends with a break, which causes execution to jump to the end of the switch body. ,[object Object],break is optional, if it is missing, the code for the following case statements executes until a break is encountered. ,[object Object],switch,[object Object]
inti = 3;,[object Object],switch(i) {,[object Object],	case 1 : System.out.println(“One”);,[object Object],			break;,[object Object],	case 2 :,[object Object],System.out.println(“Two”);,[object Object],			break;,[object Object],	case 3 :,[object Object],System.out.println(“Three”);,[object Object],			break;,[object Object],	case 4 :,[object Object],System.out.println(“Four”);,[object Object],			break;,[object Object],	default:,[object Object],System.out.println(“Out of range”);,[object Object],},[object Object],switch in action,[object Object]
Write a Java program to declare a character array,[object Object],Loop through that array and use a switch selector to check for vowels,[object Object],Vowels = {‘a’ , ‘e’,’i’,’o’,’u’},[object Object],Home work,[object Object]
Q&A,[object Object]

More Related Content

What's hot

Chapter 2 : Programming with Java Statements
Chapter 2 : Programming with Java StatementsChapter 2 : Programming with Java Statements
Chapter 2 : Programming with Java StatementsIt Academy
 
Control structures in java
Control structures in javaControl structures in java
Control structures in javaVINOTH R
 
Control structure C++
Control structure C++Control structure C++
Control structure C++Anil Kumar
 
Java conditional statements
Java conditional statementsJava conditional statements
Java conditional statementsKuppusamy P
 
Control Structures
Control StructuresControl Structures
Control StructuresGhaffar Khan
 
Control Structures in Python
Control Structures in PythonControl Structures in Python
Control Structures in PythonSumit Satam
 
04 control structures 1
04 control structures 104 control structures 1
04 control structures 1Jomel Penalba
 
C Programming: Control Structure
C Programming: Control StructureC Programming: Control Structure
C Programming: Control StructureSokngim Sa
 
java programming- control statements
 java programming- control statements java programming- control statements
java programming- control statementsjyoti_lakhani
 
Control structures in Java
Control structures in JavaControl structures in Java
Control structures in JavaRavi_Kant_Sahu
 
Control and conditional statements
Control and conditional statementsControl and conditional statements
Control and conditional statementsrajshreemuthiah
 
Control structures in C++ Programming Language
Control structures in C++ Programming LanguageControl structures in C++ Programming Language
Control structures in C++ Programming LanguageAhmad Idrees
 
Java loops for, while and do...while
Java loops   for, while and do...whileJava loops   for, while and do...while
Java loops for, while and do...whileJayfee Ramos
 
Control Statements in Java
Control Statements in JavaControl Statements in Java
Control Statements in JavaNiloy Saha
 

What's hot (20)

Chapter 2 : Programming with Java Statements
Chapter 2 : Programming with Java StatementsChapter 2 : Programming with Java Statements
Chapter 2 : Programming with Java Statements
 
Control structures in java
Control structures in javaControl structures in java
Control structures in java
 
Control structure C++
Control structure C++Control structure C++
Control structure C++
 
Java control flow statements
Java control flow statementsJava control flow statements
Java control flow statements
 
Java conditional statements
Java conditional statementsJava conditional statements
Java conditional statements
 
Control statements in java
Control statements in javaControl statements in java
Control statements in java
 
Control Structures
Control StructuresControl Structures
Control Structures
 
Control Structures in Python
Control Structures in PythonControl Structures in Python
Control Structures in Python
 
04 control structures 1
04 control structures 104 control structures 1
04 control structures 1
 
Control flow statements in java
Control flow statements in javaControl flow statements in java
Control flow statements in java
 
Control statements in java programmng
Control statements in java programmngControl statements in java programmng
Control statements in java programmng
 
C Programming: Control Structure
C Programming: Control StructureC Programming: Control Structure
C Programming: Control Structure
 
java programming- control statements
 java programming- control statements java programming- control statements
java programming- control statements
 
Control structures in Java
Control structures in JavaControl structures in Java
Control structures in Java
 
Control and conditional statements
Control and conditional statementsControl and conditional statements
Control and conditional statements
 
Control structures in C++ Programming Language
Control structures in C++ Programming LanguageControl structures in C++ Programming Language
Control structures in C++ Programming Language
 
Java loops for, while and do...while
Java loops   for, while and do...whileJava loops   for, while and do...while
Java loops for, while and do...while
 
Control structures in C
Control structures in CControl structures in C
Control structures in C
 
Control Structures: Part 1
Control Structures: Part 1Control Structures: Part 1
Control Structures: Part 1
 
Control Statements in Java
Control Statements in JavaControl Statements in Java
Control Statements in Java
 

Viewers also liked

Viewers also liked (12)

L13 string handling(string class)
L13 string handling(string class)L13 string handling(string class)
L13 string handling(string class)
 
String java
String javaString java
String java
 
Strings
StringsStrings
Strings
 
String classes and its methods.20
String classes and its methods.20String classes and its methods.20
String classes and its methods.20
 
Java Strings
Java StringsJava Strings
Java Strings
 
String handling(string class)
String handling(string class)String handling(string class)
String handling(string class)
 
Learn java
Learn javaLearn java
Learn java
 
Java strings
Java   stringsJava   strings
Java strings
 
String in java
String in javaString in java
String in java
 
String and string buffer
String and string bufferString and string buffer
String and string buffer
 
String Handling
String HandlingString Handling
String Handling
 
Introduction to Java Strings, By Kavita Ganesan
Introduction to Java Strings, By Kavita GanesanIntroduction to Java Strings, By Kavita Ganesan
Introduction to Java Strings, By Kavita Ganesan
 

Similar to 07 flow control

Similar to 07 flow control (20)

Control statements
Control statementsControl statements
Control statements
 
C++ chapter 4
C++ chapter 4C++ chapter 4
C++ chapter 4
 
Chapter 5 java
Chapter 5 javaChapter 5 java
Chapter 5 java
 
Flow of control C ++ By TANUJ
Flow of control C ++ By TANUJFlow of control C ++ By TANUJ
Flow of control C ++ By TANUJ
 
control statements
control statementscontrol statements
control statements
 
Comp ppt (1)
Comp ppt (1)Comp ppt (1)
Comp ppt (1)
 
Java loops
Java loopsJava loops
Java loops
 
03 conditions loops
03   conditions loops03   conditions loops
03 conditions loops
 
dizital pods session 5-loops.pptx
dizital pods session 5-loops.pptxdizital pods session 5-loops.pptx
dizital pods session 5-loops.pptx
 
Programming in Arduino (Part 2)
Programming in Arduino  (Part 2)Programming in Arduino  (Part 2)
Programming in Arduino (Part 2)
 
Lecture - 5 Control Statement
Lecture - 5 Control StatementLecture - 5 Control Statement
Lecture - 5 Control Statement
 
130706266060138191
130706266060138191130706266060138191
130706266060138191
 
Python Control structures
Python Control structuresPython Control structures
Python Control structures
 
Loops c++
Loops c++Loops c++
Loops c++
 
21. Assertion.ppt
21. Assertion.ppt21. Assertion.ppt
21. Assertion.ppt
 
Constructs (Programming Methodology)
Constructs (Programming Methodology)Constructs (Programming Methodology)
Constructs (Programming Methodology)
 
Loops and iteration.docx
Loops and iteration.docxLoops and iteration.docx
Loops and iteration.docx
 
Ch3 repetition
Ch3 repetitionCh3 repetition
Ch3 repetition
 
Ch05
Ch05Ch05
Ch05
 
Android Application Development - Level 3
Android Application Development - Level 3Android Application Development - Level 3
Android Application Development - Level 3
 

More from dhrubo kayal

01 session tracking
01   session tracking01   session tracking
01 session trackingdhrubo kayal
 
03 handling requests
03 handling requests03 handling requests
03 handling requestsdhrubo kayal
 
02 up close with servlets
02 up close with servlets02 up close with servlets
02 up close with servletsdhrubo kayal
 
01 overview-servlets-and-environment-setup
01 overview-servlets-and-environment-setup01 overview-servlets-and-environment-setup
01 overview-servlets-and-environment-setupdhrubo kayal
 
14 initialization & cleanup
14   initialization & cleanup14   initialization & cleanup
14 initialization & cleanupdhrubo kayal
 
08 class and object
08   class and object08   class and object
08 class and objectdhrubo kayal
 
04 data types & variables
04   data types & variables04   data types & variables
04 data types & variablesdhrubo kayal
 
03 hello world with java
03   hello world with java03   hello world with java
03 hello world with javadhrubo kayal
 

More from dhrubo kayal (20)

Cipla 20-09-2010
Cipla   20-09-2010Cipla   20-09-2010
Cipla 20-09-2010
 
01 session tracking
01   session tracking01   session tracking
01 session tracking
 
03 handling requests
03 handling requests03 handling requests
03 handling requests
 
02 up close with servlets
02 up close with servlets02 up close with servlets
02 up close with servlets
 
01 overview-servlets-and-environment-setup
01 overview-servlets-and-environment-setup01 overview-servlets-and-environment-setup
01 overview-servlets-and-environment-setup
 
19 reflection
19   reflection19   reflection
19 reflection
 
18 concurrency
18   concurrency18   concurrency
18 concurrency
 
17 exceptions
17   exceptions17   exceptions
17 exceptions
 
16 containers
16   containers16   containers
16 containers
 
15 interfaces
15   interfaces15   interfaces
15 interfaces
 
14 initialization & cleanup
14   initialization & cleanup14   initialization & cleanup
14 initialization & cleanup
 
13 inheritance
13   inheritance13   inheritance
13 inheritance
 
12 encapsulation
12   encapsulation12   encapsulation
12 encapsulation
 
11 static
11   static11   static
11 static
 
10 access control
10   access control10   access control
10 access control
 
09 packages
09   packages09   packages
09 packages
 
08 class and object
08   class and object08   class and object
08 class and object
 
05 operators
05   operators05   operators
05 operators
 
04 data types & variables
04   data types & variables04   data types & variables
04 data types & variables
 
03 hello world with java
03   hello world with java03   hello world with java
03 hello world with java
 

07 flow control

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.