Tag Archives: while loop

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: 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.

Syntax

Python while loop

Example

	num=5
	count=1
	while count <= num:
	    print("The current number is:",count)
	    count+=1

Output

The current number is: 1
The current number is: 2
The current number is: 3
The current number is: 4
The current number is: 5

In the above example, while loop will check the condition continuously the value count and compare with num value as long as condition is true. In case of true all the statement inside the while loop will execute.

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 : Looping Statements


Programming language loops are control flow statements used when need to execute a set of instructions repeatedly when some conditions become true. There are three types of loops in java.

for loop

The for loop is used to iterates a part of the programs multiple times. It’s most preferred to use when the number of iteration is fixed.

For Detail Information follow: Java: for loop

Syntax


for(initialization;condition;increment/decrement){  
// code to be executed 
}

Example

//for loop
for(int i=1; i<=15; i++){
System.out.println(i);
}

The syntax for infinitive loop

for(;;){
//code to be executed
}

while loop

The while loop is used to executes a part of the programs repeatedly on the basis of given boolean condition. It’s most preferred when the number of the iteration is not fixed.

For Detail Information follow: Java: while loop

Syntax


while(condition){  
//code to be executed 
}

Example

//while loop
int i=1;
while(i<=10){
System.out.println(i);
i++;
}

The syntax for infinitive loop

while(true){
//code to be executed
}

do-while loop

The do-while loop is used to executes a part of the programs at least once and the further execution depends upon the given boolean condition. It’s preferred when the number of iteration is not fixed and you must have to execute the loop at least once.

For Detail Information follow: Java: do-while loop

Syntax

     
do{  
//code to be executed  
}while(condition); 

Example

//do-while loop
int i=1;
do{
System.out.println(i);
i++;
}while(i<=15);

The syntax for infinitive loop

do{
//code to be executed
}while(true);

[Java] How to Prints Alphabets by Java Loops


Here you will see, How to prints Alphabets through Java all loops (for, While and do while). In these loops we are just increments char value and characters are increment continuously.

public class PrintAlphabets {

public static void main(String[] args) {
// By for Loop
char ch;
for (ch = 'a'; ch <= 'z'; ch++) {
System.out.print(ch + " ");
}
System.out.println();

// By While loop
char c = 'a';

while (c <= 'z') {
System.out.print(c + " ");
c++;
}
System.out.println();

// By do while loop
c = 'A';

do {
System.out.print(c + " ");
c++;
} while (c <= 'Z');
}

}

Output

a b c d e f g h i j k l m n o p q r s t u v w x y z
a b c d e f g h i j k l m n o p q r s t u v w x y z
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

More

For more Algorithms and Java Programing Test questions and sample code follow below links