Upcoming SlideShare
×

# 07 flow control

132 views

Published on

0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total views
132
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
0
0
Likes
0
Embeds 0
No embeds

No notes for slide

### 07 flow control

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