Computer Concept & Programming In C - Unit III - 14

Q.30. Explain the following:
(i) The break statement (ii) The continue statement
(iii) Assignment operators
Related Questions -
Q. Show the usage of break and continue statement by taking an example.                                                                         (AKTU. 2009-10)
Q. Differentiate between break and continue statements in C language.
                                                       (AKTU. 2012 - 13)
Ans. (i) The Break Statement: -
The break statement is used to terminate a case in the switch construct. It is also used to terminate a loop, immediately by passing any condition in there. The control will be transferred to the first statement following the loop block. If you have nested loops, then the break statement inside one loop transfers the control to the next outer loop. This is the main difference between the break and the exit function, which (exit) terminates the whole program.
In the following program the counter “c” starts from “1” and goes all the way to “1000” while the string “Hello World” is displayed. You can however, terminate the loop by pressing any key. Then you get a massage indicating the value of the counter right after point when the break took place: “Done at counter value = ……..” 
A new function kbhit ( ) is used in this program to check for a key stroke. If a key has been pressed, it returns the value “1”, otherwise it returns “0”. The function kbhit ( ) is defined in the header file <conio.h>
# include <stdio.h>
# include <conio.h>
void main ( )
{int c = 1;
while (c < = 1000)
{
printf (“\ n Hello World”);
c + +;
if (kbhit ( )! = 0)
break;
}
printf (“\ n Done at counter value = % d”, c);
}
A sample run gives the following output:
Hello World
Hello World
Hello World
Hello World
Done at counter value = 5.
(ii) The Continue Statement: -
Instead of breaking a loop, there are times when you want to stay in a loop but skip over some statements within the loop. To do this, you can use the continue statement provided by C. 
The continue statement causes execution to jump to the top of the loop immediately. You should be aware that using the continue statement, as well as the break statement, may make your program hard to debug.
Using the continue statement.
# include <stdio.h>
void main ( )
{
int i, sum;
sum = 0;
for (i = 1; i < 8; i++)
{
if ((i = = 3)¦¦(i = = 5))
continue;
sum + = i;
}
printf (“The sum of 1, 2, 4, 6 and 7 is: %d \ n”, sum);
}
The output of the program is:
The sum of 1, 2, 4, 6 and 7 is: 20
By doing so, the expression (i = = 3)¦¦(i = = 5) is evaluated in the if statement of line 10. if the expression returns 1 (that is, value of i is equal to either 3 or 5), the continue statement is executed, which causes the sum operation to be skipped and another iteration to be started at the beginning of the for loop. In this way, we obtain the sum of the integer values of 1, 2, 4, 6 and 7 but skip 3 and 5, automatically by using one for loop.
After the for loop, the value of sum, 20, is displayed on the screen by the printf ( ) function.
(iii) Assignment Operators: -
In the C language, the (=) operator is called the assignment operator. The general statement form to use the assignment operator is: left hand operand = right hand operand. 
Here, the statement causes the value of the right-hand-operand to be assigned (or written) to the memory location of the left hand operand. For example, the a = 5, statement writes the value of the right hand operand (5) into the memory location of the integer variable a.
Similarly b = a = 5; assign 5 to the integer variable a first and then to the integer variable b. After the execution of the statement, both a and b contains the value of 5.
Combining Arithmetic Operators with (=): -
Consider the example: Given two integer variable x and y how do you assign the sum of x and y to another variable z? By using the assignment operator (=) and the addition operator (+), you get the following statement z = x + y; this time instead of assigning the result to the third variable z. Let’s write the sum back to the integer variable x : x = x + y;
Here, on the right side of the assignment operator (=) the addition of x and y is executed, on the left side of =, the previous value saved by x is replaced with the result of the addition from the right side. The C language gives you a new operator + =, to do the addition and the assignment together. Therefore, you can rewrite the x = x + y statement to x + = y;
The combinations of the assignment operator (=) with the arithmetic operators +, -, *, / and % give you another types of operators arithmetic assignment operators:
Operator Description
+ = Addition assignment operator
- = Substraction assignment operator
* = Multiplication assignment operator
/ = Division assignment operator
% = Remainder assignment operator 
The following is the equivalence of statements.
x + = y; is equivalent to x = x + y;
x - = y; is equivalent to x = x - y;
x * = y; is equivalent to x = x * y;
x / = y; is equivalent to x = x / y;
x % = y; is equivalent to x = x % y;
The following program shows using arithmetic assignment operators.
# include <stdio.h>
void main ( )
{
int x, y, z;
x = 1;
y = 3;
z = 10;
printf (“\ n Given x = %d, y = %d, and z = %d,\ n”, x, y, z);
x= x + y;
printf (“x = x + y assigns %d to x; \n”, x);
x = 1;
x + = y;
printf (“x + = y assigns %d to x; \n”, x);
x = 1;
z = z*x + y;
printf (“z = z*x+y assigns %d to z,\n”, z);
z = 10;
z = z * (x+ y);
printf (“z = z * (x + y) assigns % d to z; \ n”, z);
z = 10;
z = z * (x + y);
printf (“z * = x + y)assigns %d to z.\n”, z);
}
Given x = 1, y = 3, and z = 10,
x = x + y assigns 4 to x;
x + = y assigns 4 to x
z = z * x + y assigns 13 to z
z = z * (x + y) assigns 40 to z
z * = x + y assigns 40 to z.