Break & Continue statements

 

=======================================================
#Break & Continue statements
=======================================================

#! bin/bash
# break statement

for (( i=0; i<=10; i++ ))
do
if [ $i -gt 5 ]
then
break
fi
    echo $i
done
echo "================"



: '
Note: 
1st check for loop condition
2nd it will check if condition
if 1>5 false; it will not execute breakd statement, it will go for loop
if 6>5 true; it will execute break statement, then repeat break statement condition is false...
'



[prabhucloudxlab@cxln4 ~]$ sh hello.sh
0
1
2
3
4
5
================



#! bin/bash
# continue statement

for (( i=0; i<=10; i++ ))
do
if [ $i -eq 3 ] || [ $i -eq 7 ]
then
continue
fi
    echo $i
done
echo "================"


[prabhucloudxlab@cxln4 ~]$ sh hello.sh
0
1
2
4
5
6
8
9
10
================

: '
Note:
 
it will skip the 3, 7 
The break statement is used to exit the current loop. 
The continue statement is used to exit the current iteration of a loop and begin the next iteration
'