About Me

Hello there! Before talking about qualifications and professional abilities, I would like to introduce myself. I am a Yuvraj Basnet and professional software developer and have been working in this field for 3 long years. I have finished my graduation from The University of Delhi on Computer Science Engineering I considered myself a reasonably experienced programmer with a particular interest in the human side of software development, computers are fascinating machine, but they're mostly reflection of the people using them. In the art of software development, studying code isn't enough; you have to study the people behind the software, too.

Control Statements in C (Part 2)

Control Statements in C (Part 2)

Jump Statements:

In the previous article we have discussed control statements in C language i.e, (Decision Making and Loop statements) and in this article we have discuss part 2 of control statements i.e, Jump Statements. So, without further wasting anytime let's get started.

Definition of Jump Statements:

The statements is a unconditionally transfer program control within a function. C Language provides is multiple statements through which we can transfer the control anywhere in the program.

There are four Jump Statements offered by C:

1. Break
2. Continue
3. Goto
4. Return

1. Break:

A break statement is used to end the execution of the rest of the blocks or statements where it is present and takes the control out of the block to the next statement. It is mostly used in loops and switch-case to diversion the rest of the statement and take the control to the end of the loop.

Another point which is taken into attention is that break statement when used in nested loops only terminates the inner loop where it is used and not any of the outer loops. Let’s take a simple program to better understand how break statement works: 


OUTPUT:
1 2 3 4 5 6 7 8 9 10

In the above program, we all see that as soon as the condition if(i==10) becomes true the control flows out of the loop and the program terminates.

2. Continue:

The Continue statement like any another jump statements interfere  or alter the flow of control during the time of execution. Continue is mostly used in loops. Sooner than ends the loop it stops the execution of the statements below and takes the control to the next iteration. comparable to a break statement, in case of nested loop, the continue passes the flow of control to the next iteration of the inner loop where it is present and not to any of the outer loops. Let’s have a look at the following example:


OUTPUT:
1 3 4 1 3 4

In the above program, we all see that the printf() command for the condition j=2 is skipped each time at the time of program execution because of continue. also see that only the condition j=2 gets affected by the continue statement. The outer loop runs without any interruption in its iteration.

3. Goto:

This jump statement is used to transfer the flow of control to any part of the program where the program wants. The programmer needs to specify a identifier and label with the goto statement in the following manner:

goto label;

This label indicates the location in the program where the control jumps to. let's take a simple program to understand how goto works:


OUTPUT:
Two

In the above program, we all see that when the control goes to the goto there; statement when variable (i) becomes equal to 2 then the control goes out of the loop to the label(there: ) and prints "Two" on the screen.

4. Return Statement:

This jump statement is usually used at the end of a function to terminate it with or without a value. return statement usually takes the control from the calling function back to the main function.

An important point to be taken into consideration is that return can only be used in functions that is declared with a return type such as int, double, float, char, etc. The functions which is declared with (void) type does not return any value. Also, the function returns the value that belongs to the similar data type as it is declared. let's take a simple example to show how the return works:


In the above program we have two functions that have a return type but only one function is returning a value i.e, func() and the other is just used to terminate the function i.e,main(). The function func() is returning the char value of the given number 110. We all also see that it is returning a character value because return type of func() is char.
The return in main() function returns zero because it is important to have a return value here because main has been given the return data type int.















Post a Comment

0 Comments