Jump statements in programming are used to change the flow of control within a program. They allow the programmer to transfer program control to different parts of the code based on certain conditions or requirements. Here are common types of jump statements:
1. Break Statement in Programming:
The break
statement is primarily used to exit from loops prematurely. When encountered inside a loop, it terminates the loop’s execution and transfers control to the statement immediately following the loop.
C++
#include <iostream>
using namespace std;
int main()
{
for ( int i = 0; i < 10; i++) {
if (i == 5)
break ;
cout << i << " " ;
}
return 0;
}
|
C
#include <stdio.h>
int main()
{
for ( int i = 0; i < 10; i++) {
if (i == 5)
break ;
printf ( "%d " , i);
}
return 0;
}
|
Java
import java.io.*;
class GFG {
public static void main(String[] args)
{
for ( int i = 0 ; i < 10 ; i++) {
if (i == 5 )
break ;
System.out.print(i + " " );
}
}
}
|
C#
using System;
public class GFG {
static public void Main()
{
for ( int i = 0; i < 10; i++) {
if (i == 5)
break ;
Console.Write($ "{i} " );
}
}
}
|
Javascript
for (let i = 0; i < 10; i++) {
if (i === 5)
break ;
console.log(i + " " );
}
|
Python3
for i in range ( 10 ):
if i = = 5 :
break
print (f "{i} " , end = "")
|
2. Continue Statement in Programming:
The continue
statement is used to skip the current iteration of a loop and proceed to the next iteration.
C++
#include <iostream>
using namespace std;
int main()
{
for ( int i = 0; i < 10; i++) {
if (i % 2 == 1)
continue ;
cout << i << " " ;
}
return 0;
}
|
C
#include <stdio.h>
int main()
{
for ( int i = 0; i < 10; i++) {
if (i % 2 == 1)
continue ;
printf ( "%d " , i);
}
return 0;
}
|
Java
import java.io.*;
class GFG {
public static void main(String[] args)
{
for ( int i = 0 ; i < 10 ; i++) {
if (i % 2 == 1 )
continue ;
System.out.print(i + " " );
}
}
}
|
C#
using System;
public class GFG {
static public void Main()
{
for ( int i = 0; i < 10; i++) {
if (i % 2 == 1)
continue ;
Console.Write($ "{i} " );
}
}
}
|
Javascript
for (let i = 0; i < 10; i++) {
if (i % 2 === 1)
continue ;
console.log(i + " " );
}
|
Python3
for i in range ( 10 ):
if i % 2 = = 1 :
continue
print (f "{i} " , end = "")
|
3. Return Statement in Programming:
The return
statement is used to exit a function and optionally return a value to the caller.
C++
#include <iostream>
using namespace std;
bool isEven( int N) { return N % 2 == 0; }
int main()
{
int N = 5;
if (isEven(N)) {
cout << "N is even" ;
}
else {
cout << "N is odd" ;
}
return 0;
}
|
C
#include <stdio.h>
int isEven( int N) { return N % 2 == 0; }
int main()
{
int N = 5;
if (isEven(N)) {
printf ( "N is even" );
}
else {
printf ( "N is odd" );
}
return 0;
}
|
Java
import java.io.*;
class GFG {
static boolean isEven( int N) { return N % 2 == 0 ; }
public static void main(String[] args)
{
int N = 5 ;
if (isEven(N)) {
System.out.println( "N is even" );
}
else {
System.out.println( "N is odd" );
}
}
}
|
C#
using System;
public class GFG {
static bool IsEven( int N) { return N % 2 == 0; }
static public void Main()
{
int N = 5;
if (IsEven(N)) {
Console.WriteLine( "N is even" );
}
else {
Console.WriteLine( "N is odd" );
}
}
}
|
Javascript
function isEven(N) {
return N % 2 === 0;
}
let N = 5;
if (isEven(N)) {
console.log( "N is even" );
} else {
console.log( "N is odd" );
}
|
Python3
def isEven(N):
return N % 2 = = 0
N = 5
if isEven(N):
print ( "N is even" )
else :
print ( "N is odd" )
|
4. Goto Statement in Programming:
Some programming languages support the goto
statement, which allows transferring control to a labeled statement within the same function or block of code. However, the use of goto
is generally discouraged due to its potential for creating unreadable and unmaintainable code.
C++
#include <iostream>
using namespace std;
int main()
{
int i = 0;
loopStart:
if (i < 5) {
cout << i << " " ;
i++;
goto loopStart;
}
return 0;
}
|
C
#include <stdio.h>
int main() {
int i = 0;
loopStart:
if (i < 5) {
printf ( "%d " , i);
i++;
goto loopStart;
}
return 0;
}
|
C#
using System;
public class GFG {
static public void Main()
{
int i = 0;
loopStart:
if (i < 5) {
Console.Write(i + " " );
i++;
goto loopStart;
}
}
}
|
Control flow statements in Programming
Control flow refers to the order in which statements within a program execute. While programs typically follow a sequential flow from top to bottom, there are scenarios where we need more flexibility. This article provides a clear understanding about everything you need to know about Control Flow Statements.
Table of Content
- What are Control Flow Statements in Programming?
- Types of Control Flow statements in Programming
- Conditional Statements in Programming
- Looping Statements in Programming
- Jump Statements in Programming