Tag Archives: continue

Python: Control Structure


In Python, The commonly used control structures are:

Selection Statements

During the execution of the program, we may not wish to execute all sets of statements sequentially. Sometimes we may wish to select between the set of statements based on some conditions. Based on the test condition evaluation, the flow is determined inside the program. Here is the list of selection statements in Python:

  • If statement: It is a conditional statement used for decision making in python. Example
  • else if statement: It is a conditional statement if block condition is false then execute the else block. Example
  • else if ladder statement: These are multiple else if statement in sequence if one condition not match then go next else for condition check .Example
  • Nested if statement: You can write if block with in another if block that is called nested if. Example

The example of if, else, elif and nested if will discuss in further blogs in more detail.

Looping Statements

Looping statements are used to execute the same block of code multiple times in python based on the test condition. Here is the list of looping statements in python.

  • while loop: The while loop is used to execute a piece of code as long as the test condition is true. While loop is preferred whenever the number of iterations is not known. Example
  • for loop: In python, for loop allows the loop to run over a specific sequence of values. In other words, for every value in the sequence, the loop runs once. Thus we can avoid infinite loops by using a for loop. Example
  • Nested loops: Loop with in another loop is called as nested loop. Example

The example of while loop, for loop and nested loop will discuss in further blogs in more detail.

Loop Control Statements

The flow inside looping statements are controlled using the looping control statements like pass, break and continue.

  • break: When we want to stop a loop or break away from it we can use the break statement. Example
  • continue: When we want to skip the remaining portion of loop statements and continue with the next iteration, we can use the continue statement. Example
  • pass : pass is a null statement that is used to do create empty blocks. When the pass is executed, it results in no operation and the control will move to the next statement applicable. Example

The example of the break, continue, and pass will discuss in further blogs in more detail.

Python: continue Loop Control Statement


The flow inside looping statements are controlled using the looping control statements like pass, break and continue.

When we want to skip the remaining portion of loop statements and continue with the next iteration, we can use continue statement.

Example

Go through the below code, Assume A – Adult passenger, C- Child, FC – Flight Captain, FA – Flight Attendant, SP – Suspicious passenger.

for pasngr in "A","A", "FC", "C", "FA", "SP", "A", "A":
	if(pasngr=="FC" or pasngr=="FA"):
	   print("No check required")
	     continue
	
	if(pasngr=="SP"):
	    print("Declare emergency in the airport")
	    break	
	if(pasngr=="A" or pasngr=="C"):
	    print("Proceed with normal security check")
print("Check the person")
print("Check for cabin baggage")


Output

Proceed with normal security check
Check the person
Check for cabin baggage
Proceed with normal security check
Check the person
Check for cabin baggage
No check required
Proceed with normal security check
Check the person
Check for cabin baggage
No check required
Declare emergency in the airport

In this above example, if you will see when value of passenger is FC or FA then it will execute the statement inside the if block because it’s having continue keyword the the next statement after continue will skip and pointer will reach to next value of for loop.

Same as when passenger will value will be SP then this block having break keyword, it will terminate the loop and stop execution inside the loop and pointer will directly jump to next statement after loop.

Python: break Loop Control Statement


The flow inside looping statements are controlled using the looping control statements like pass, break and continue.

When we want to stop a loop or break away from it we can use break statement.

Example

Go through the below code, Assume A – Adult passenger, C- Child, FC – Flight Captain, FA – Flight Attendant, SP – Suspicious passenger.

for pasngr in "A","A", "FC", "C", "FA", "SP", "A", "A":
	if(pasngr=="FC" or pasngr=="FA"):
	   print("No check required")
	     continue
	
	if(pasngr=="SP"):
	    print("Declare emergency in the airport")
	    break	
	if(pasngr=="A" or pasngr=="C"):
	    print("Proceed with normal security check")
print("Check the person")
print("Check for cabin baggage")


Output

Proceed with normal security check
Check the person
Check for cabin baggage
Proceed with normal security check
Check the person
Check for cabin baggage
No check required
Proceed with normal security check
Check the person
Check for cabin baggage
No check required
Declare emergency in the airport

In this above example, if you will see when value of passenger is FC or FA then it will execute the statement inside the if block because it’s having continue keyword the the next statement after continue will skip and pointer will reach to next value of for loop.

Same as when passenger will value will be SP then this block having break keyword, it will terminate the loop and stop execution inside the loop and pointer will directly jump to next statement after loop.

Java : Types of Statements


Java method body is a series of one or more statements. In a java programming language, a statement is a basic unit of execution which follows the syntax of the language and includes one or more clauses.

Note: All java statements except blocks statement terminated by a semicolon(;).

Type of Java Statements

Java supports five different types of statements:

Blocks

A block is a series of zero or more statements between a matching set of open and close curly braces.  For example :

  • The bodies of methods and switch statements are blocks.
  • The bodies of if, for, while, and do-while statements may also be blocks.
  • You can also simply create a new block inside another block by enclosing code within curly braces. A block contained within another block is itself a statement of the outer block.
  • Blocks that contain no statements are called an empty block.

Example of Statements

At the high level below are some examples of statements. You can get more detail about each type of statement in the corresponding link.

Java Block and Types of Statements
Java Block and Types of Statements


References

Java : Control Flow Statements


Statements in java source code generally executed from top to bottom, in the order they appear. However, with control-flow statements, that order can be interrupted to implement decision making, branching or looping so that the Java program can run particular blocks of code based on certain conditions. Control flow statements categorize as below:

See Also: Type of Statements in Java