Friday, 14 September 2018

Control Statements (Part 3)




In this post will strictly discuss break and continue statement and then will see some of the programs that will help you to understand the concepts.


Break - 
+ Break statement (Keyword) in java. 
+ When the break statement is encountered inside a loop, the loop is immediately terminated and the program control resumes at the next statement following the loop.
+* It can be used to terminate a case in the switch statement.


Continue - 
+ The continue keyword can be used in any of the loop control structures. It causes the loop to immediately jump to the next iteration of the loop.
+ In a for loop, the continue keyword causes control to immediately jump to the update statement(Step).
In a while loop or do/while loop, control immediately jumps to the Boolean expression.




Let's See Some Programs based on this...



Ques 1: WAP (Write a program) to get a number user (1-7) and print its equivalent day.
(without break)



Explanation -  

User enter 5 in the console,
week = 5;
case 5 matches, after execution of case 5
all the rest case will execute because execution of program follows top to down approach.
that why you are getting
Thursday
Friday
Saturday
Invalid day  as an output



Ques 2: WAP (Write a program) to get a number user (1-7) and print its equivalent day.
(with break)




Explanation -  

User enter 5 in the console,
week = 5;
case 5 matches, after execution of case 5 (output is Thursday)
JVM encounter break statement,
all the rest case will not execute because execution of program follows top to down approach.
so after execution of case 5, break statement execute and break statements immediately terminated switch.



Ques 3: WAP (Write a program) to print number from 1 to 10. (Using for loop)

Will see three (3) cases --
 flow of execution of for loop Initialisation -> Condition check -> Loop body -> Iteration/Increment




Case 1: using for loop, printing 1 to 10.


Explanation -  
- for loop is used for counting.
- i =1 (counting start from 1)
- i<=10 (condition - till contidion setisfied, it will counting) 
- i++ increment (every time i=current value of i +1)
- output is 1 to 10.


Case 2:  using for loop, printing 1 to 10 ( with continue).


Explanation -  
- for loop is used for counting.
- i =1 (counting start from 1)
- i<=10 (condition - till condition satisfied, it will counting) 
- i++ increment (every time i=current value of i +1)
- there is an if condition,
  • if condition true - control goes inside if block and continues execute, so when i=5  condition became true and continue statement executed then control goes for next iteration.
  • if condition false - control never goes inside if block so the program will work as above case.




Case 3: using for loop, printing 1 to 10 ( with break).


Explanation -  
- for loop is used for counting.
- i =1 (counting start from 1)
- i<=10 (condition - till condition satisfied, it will counting) 
- i++ increment (every time i=current value of i +1)
- there is an if condition,



  • if condition true - control goes inside if block and continues execute, so when i=5  condition became true and continue statement executed then control goes out of the loop.
  • if condition false - control never goes inside if block so the program will work as above case.



Link to download these two programs... Click Here.



Now, In next post, I will tell you how to get INPUT From USER.
Using Scanner Class.




No comments:

Post a Comment