6.11 Write a program to use break statement and terminate the loop.
Explanation: In the above program the while loop is an infinite loop. The integer variable x is initialization to 1. In each iteration the value of x is displayed and incremented. The if condition checks the value of x with 8. If the value of x is equal to 8 the break statement is executed and the infinite while loop terminates. Thus, the break statement is used to exit from the loop as per shown in Figure.
Working of break statement
6.12 Write a program to demonstrate the use of continue statement.
Explanation: In the above program the for loop executes from 1 to 50. The if condition check whether the remainder of number is 0 or non â€“zero. If the remainder is zero, the continue statement is executed and the statement after continue statement will not be executed. The continue statement transfers the control at the beginning of the loop. Figure simulates the working of continue statement.
Working of continue statement