\ Core Java Tutorial -unix statements
New Document
THE continue AND break STATEMENTS

All the statements within a loop are executed as long as certain conditions are satisfied. However, one can come out of the loop prematurely by using a break statement. Thus, a break statement is used to come out from within the scope of loop-control structures such as the while, the unill or a for loop in a program under certain conditions. Similarly, a continue statement is used to resume the next iteration of the loop without considering the statements that appear after the continue statement within the loop.

The break statement:

The break statement is used to terminate the execution of the entire loop, after completing the execution of all of the lines of code up to the break statement. It then steps down to the code following the end of the loop.

Syntax:

The following break statement would be used to come out of a loop:

break

The break command can also be used to exit from a nested loop using this format:

break n

Here n specifies the nth enclosing loop to exit from.

Example:

Here is a simple example which shows that loop would terminate as soon as a becomes 5:

 $cat menu2.sh
# A shell script illustrating the use of the break and continue.
ans=y
while [ "$ans" = "y" ]
do
echo "              MENU\n
1. List of files\n2. Today's Date\n3. Process Status\n4. Users of the System\n5. present
working directory\n6. Quit to Unix\n Enter your option : \c"
read choice
case "$choice" in
   1) ls -l ;;
   2) date ;;
   3) ps ;;
   4) who ;;
   5) pwd ;;
   6) break ;;
   *) echo "Invalid Choice"
      continue ;;
esac
done
$

This will produce following result:

0
1
2
3
4
5

Here is a simple example of nested for loop. This script breaks out of both loops if var1 equals 2 and var2 equals 0:

#!/bin/sh

for var1 in 1 2 3
do
   for var2 in 0 5
   do
      if [ $var1 -eq 2 -a $var2 -eq 0 ]
      then
         break 2
      else
         echo "$var1 $var2"
      fi
   done
done

This will produce following result. In the inner loop, you have a break command with the argument 2. This indicates that if a condition is met you should break out of outer loop and ultimately from inner loop as well.

1 0
1 5
The continue statement:

The following code for search a pattern in file name (or entire file name) and look at its size, code is derived from an ebook about scripting. It is working in HP Unix but I am unable to run in Linux (Ubuntu) Please advise me what is wrong for Linux? And besides , how can I get rid of errors in case of the file is not available? .

Syntax:
continue

Like with the break statement, an integer argument can be given to the continue command to skip commands from nested loops.

continue n

Here n specifies the nth enclosing loop to continue from.

Example:

The following loop makes use of continue statement which returns from the continue statement and start processing next statement:

pattern=core
total=0

for f in *
  do
     [ ! -f $f ] && continue
     if grep $f $pattern > /dev/null
     then
        size=`cat $f|wc -c`
        total=xpr $total + $size`
     fi
  done

echo Total size of $pattern is $total
Previous                                                                                                                                                       Next

Back to Top