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

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

  • 2. What are flow control statements? if-else while do-while for for each return break continue switch Agenda
  • 3. “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 All conditional statements use the truth or falsehood of a conditional expression to determine the execution path. conditional expression : a == b evaluates to true or false Any of the relational operators can be used to produce conditional statement Java doesn’t allow you to use a number as a boolean What are control statements?
  • 4. if(Boolean-expression) statement if(Boolean-expression) statement else statement else is optional The Boolean-expression must produce a boolean result 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. if-else
  • 5. int a = 5; int b = 10; if(a > b) System.out.println(“a is bigger”); else System.out.println(“b is bigger”); If-else in Action
  • 7. 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.
  • 8. Looping is controlled by while, do-while and for, which are sometimes classified as iteration statements. Statement(s) repeats until the controlling Boolean-expression evaluates to false Iteration
  • 9. while(Boolean-expression) Statement The Boolean-expression is evaluated once at the beginning of the loop and again before each further iteration of the statement int x = 1 while(x <= 100) { System.out.println(x); x++; } while
  • 10. do statement while(Boolean-expression); The statement of the do-while always executes at least once, even if the expression evaluates to false the first time In a while, if the conditional is false the first time the statement never executes. int x = 1; do { System.out.println(x); x++; } while (x <= 5) do-while
  • 12. Write a Java program to print the numbers from 1…100 using a while loop. In the while loop check and print if the number is odd or even.
  • 13. for(initialization; Boolean-expression; step) statement Performs initialization before the first iteration. Then it performs conditional testing At the end of each iteration, some form of “stepping.” Any of the expressions initialization, Boolean-expression or step can be empty 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. At the end of each loop, the step executes. Useful if you already know how many times the statements need to be repeated. foreach will also work with any object that is Iterable. for
  • 14. for(inti = 1 ; i <10 ; i++) { System.out.println(i); } inti = 1; for(; i <10 ;) { System.out.println(i); i++; } for loop in Action
  • 16. Rewrite the exercise with while loop, replacing it with a for loop.
  • 17. New in Java SE 5 Useful for iterating over arrays and collections int a [] = new int[100]; for(inti = 0 ; i < a.length ; i++){ a[i] = i; } for ( int x : a) { System.out.println(x); } for each
  • 19. Write a Java program to populate an integer array with 100 elements. Extend the program to print the numbers which are odd and even in that array using for each loop.
  • 20. branch or change control without any test return, break, continue goto - skip Conditional Branching
  • 21. It specifies what value a method will return (if it doesn’t have a void return value) It causes the current method to exit, returning that value. return
  • 22. public intgetLarger(int a, int b) { if(a > b) return a; else return b; } return in action
  • 23. break quits the loop without executing the rest of the statements in the loop. for(inti = 1 ; i <=10 ; i++) { if( i % 5 == 0) break; System.out.println(i); } In-case of nested for or while it will break out of the innermost loop where break was encountered == {Home work} break
  • 24. continue stops the execution of the current iteration and goes back to the beginning of the loop to begin the next iteration for (inti = 1; i <= 10 ;i++) { if ( i % 3 == 0) continue; System.out.println(i); } continue
  • 25. The switch statement selects from among pieces of code based on the value of an integral expression switch(integral-selector) { case integral-value1 : statement; break; case integral-value2 : statement; break; case integral-value3 : statement; break; case integral-value4 : statement; break; case integral-value5 : statement; break; default: statement; } Integral-selector is an expression that produces an integer value 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. If no match occurs, the default statement executes. Each case ends with a break, which causes execution to jump to the end of the switch body. break is optional, if it is missing, the code for the following case statements executes until a break is encountered. switch
  • 26. inti = 3; switch(i) { case 1 : System.out.println(“One”); break; case 2 : System.out.println(“Two”); break; case 3 : System.out.println(“Three”); break; case 4 : System.out.println(“Four”); break; default: System.out.println(“Out of range”); } switch in action
  • 27. Write a Java program to declare a character array Loop through that array and use a switch selector to check for vowels Vowels = {‘a’ , ‘e’,’i’,’o’,’u’} Home work
  • 28. Q&A