Page 1 :
goto, break, Un conditional, branch statements, continue, return, Now, let us discuss various types of jumps one by one in this section., 14.12.1 The goto statement, In this section, let us see "What is goto statement? When it is used? What are the, disadvantages?", Definition: The goto statement is a jump statement that transfers the control to the, specified statement in a program. This is an unconditional branch statement. The, specified statement is identified by a symbolic name (any identifier) ending with, colon “:". The syntax of goto statement is shown below:, d goto label:, odi giel, where, goto is a keyword, label is an identifier ending with the colon ":", Disadvantages, Using goto's in a program is an un-structured programming style., • The unstructured programs written using goto's are very difficult to read and, understand., • It is also very difficult to debug the programs., So, as far as possible it is better to avoid the usage of goto statements. The followilg, program adds all the natural numbers using goto statement., Scanned by CamScanner
Page 2 :
A Computer Concepts and C Programming Techniques 14.79, | TRACING, PROGRAM, #include <stdio.h>, void main(), Execution starts from here, {, int n, i, sum;, | Non executable statement, | Input, | Enter the number of terms, printf (“Enter the no. of terms\n");, scanf (“%d",&n);, Computations, sum = i = 0;, sum = 0, sum = sum + i;, i=i+ 1;, top:, sum = 0 + 0 +1+ 2 + 3 + 4 + 5=15, i = 0 1, 3 4, 5 6, if (i<=n ) goto top;, | Control comes out of for loop, | Output, Sum of series = 15, printf ("Sum of series = %d", sum);, %3D, 14.12.2 The break statement, +," b ning, In this section, we discuss another jump statement called break. "What is break, statement? When it is used?", Definition: The break statement is jump statement which can be used in switch, statement and loops. The break statement works as shown below:, • The break statement in switch statement causes control to terminate switch, statement and the statement following switch statement will be executed. Usually,, in any case, the break statement will be the last statement., • If break is executed in a loop (such as for/while/do-while), the control comes out, of the loop and the statement following the loop will be executed. The break, statement is used mainly to terminate the loop when a specific condition is, reached., th If break appears in the inner loop of a nested loop, control only comes out of, the inner loop as shown below:, Scanned by CamScanner
Page 3 :
inner loop to be terminated. The outer, for(...), {, for (.....), bo, if (error1), break;, Note: The break statement causes the, Inse, loop is still active., .RRR.., }, + m mR, Consider the following program segment:, i = 1;, 1lo ino aoo lono, for(;;), ot atog (, gn TRACING, {, if (i== 5 ) break;, Output, 1234, printf(“%d “,i++);, tesmaista lesnd odT IA, In the given for loop there is no initialization, terminal condition and, increment/decrement part. So, it is an infinite loop. Even though it appears as an, infinite loop, as the control enters into the loop, the value of i is compared with a, constant value 5. If the value of i is 5, break statement is executed and control, immediately comes out of the loop., wa goiwolldi inamstsie or bs inamstsie, ad iy ap, 14.12.3 The continue statement, Now, we shall see "What is continue statement? Why it is used?", bm qool, Definition: During execution of a loop, it may be necessary to skip a part of the 1oop, based on some condition. In such cases, we use continue statement. The continue, statement is used only in the loops to terminate the current iteration., For example, during processing of student records, it may be necessary to, exclude student names whose marks are less than or equal 50. In such case, a test, Scanned by CamScanner
Page 4 :
execution continues with next iteration of the loop., program that processes the student details can be skipped using continue. But, the, Now, the question is "What is the difference between break and continue statement?", is executed, the rest of the statements within the body of the loop are skipped and the, for-loop, control is transferred to exp3 (increment/decrement). The pictorial, conditional expression in the loop is executed. But, when continue is executed in the, a Computer Concepts and C Programming Techniques 14.81, whether the marks scored is less than or equal to 50. If so, the part of the, made to see, In the while-statement and do-while statement, whenever a continue statement, representation is shown below:, do, while ( expression ), for ( expl; exp2; exp3 ), {, АСTION-1;, АСTION-1;, ACTION-1;, continue;, continue;, continue;, АСTION-n;, АСTION-n;, АСTION-n;, } while ( expression );, }, For example, consider the statement shown below:, for (i = 1; i <=5; i++), {, if (i == 2) continue;, printf(“%d “,i);, |i =1 2, 345, Output, 3 4 5, Note that when i takes the value 2, the continue statement is executed and the, ements following continue are skipped. The control is transferred to exp3(i.e., i++), nd the execution continues from that point. The output of this program is 1 3 4 5., Scanned by CamScanner