Tag Archives: break

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

Java: Switch Case Statement


The switch statement is a decision-making statement, also called a multi-way branch statement. It provides an easy way to jump execution to different parts of code based on the case value match with expression. This expression result allowed type as byte, short, char, and int primitive data types.

Note: After java 7+, it also allowed to work with enumerated types ( Enums in java), the String class and Wrapper classes.

Points to remember about Switch-Case statements

  • The value data type for a case must be the same data type as the variable in the switch.
  • The value for a case must be a constant or a literal.
    The value for the case, Variables are not allowed.
  • Duplicate case values are not allowed.
  • The break statement (optional) is used inside the switch to terminate a statement sequence.
  • If break statment is omitted, execution will continue on into the next case.
  • The default statement (optional) can appear anywhere inside the switch block. In case, if it is placed not at the end, then a break statement must be kept after the default statement to omit the execution of the next case statement.

See Also :

Syntax:

switch(variable)
{
case 1:
   //execute your code
break;

case n:
   //execute your code
break;

default:
   //execute your code
break;
}

Note:

  • The switch statement is fast when compared with the case of multiple else if because it directly jumps to match condition.

Switch-Case Example

ublic class SwitchCaseTest {

	public static void main(String[] args){
		        int month = 5;
		        String monthString; 

		        // switch statement with int data type
		        switch (month) {
		        case 1:
		        	monthString = "January";
		            break;
		        case 2:
		        	monthString = "February";
		            break;
		        case 3:
		        	monthString = "March";
		            break;
		        case 4:
		        	monthString = "April";
		            break;
		        case 5:
		        	monthString = "May";
		            break;
		        case 6:
		        	monthString = "June";
		            break;
		        case 7:
		        	monthString = "July";
		            break;
		        case 8:
		        	monthString = "August";
		            break;
		        case 9:
		        	monthString = "September";
		            break;
		        case 10:
		        	monthString = "October";
		            break;
		        case 11:
		        	monthString = "November";
		            break;
		        case 12:
		        	monthString = "December";
		            break;
		        default:
		        	monthString = "Invalid Month";
		            break;
		        }
		        System.out.println("Selected Month "+month+" is "+monthString);
		    }
		}

Output


Selected Month 5 is May