In computer programming, there are certain times when you have to iterate through a series of values and perform a task in each value. This case could be addressed using a looping statement which will continue to execute a command until the condition returns false.
There are several types of looping statement in computer programming like:
There are several types of looping statement in computer programming like:
- For Loop
- For Each
- Do While
- While
- Do Until
But in this lesson, we will only use For Loop and Do While because another looping statement will become easier to understand once you’ve learned how to use the two most common looping algorithms.
FOR LOOP
This looping statement has three important parts or parameters necessary for operation.
- Initialization
- Condition
- Increment/Decrement
Initialization = this refers to the initialization of value to the pilot variable.
Example: For (int a=0;)
Condition = this refers to the condition to meet before the execution of the following commands will stop.
Example: For (int a=0;a<10;)
Increment = this refers to the third parameter of For Loop necessary to meet the condition. For example, if our condition is for variable A to reach the value of 10 then we have to increment the value of variable A from 0
Example: For (int a=0;a<10;a++)
Example: For (int a=0;a<10;)
Increment = this refers to the third parameter of For Loop necessary to meet the condition. For example, if our condition is for variable A to reach the value of 10 then we have to increment the value of variable A from 0
Example: For (int a=0;a<10;a++)
This example means that as long as the value of variable A is less than 10 it will continue to execute the commands and increment the value of variable A. In the same manner, if the condition is to reach a lower value from higher then instead of incrementing the value of variable A we will decrease its value using a—command
Example: For (int a=10;a>0;a--) from value 10 we will reduce the value to zero each time the for loop executes.
Example: For (int a=10;a>0;a--) from value 10 we will reduce the value to zero each time the for loop executes.
Example in C/C++/Java
For(int a=0;a<5;a++){
System.out.println(“Hello World”);
}
Example in VB.Net
For int a=0 to 5
Console.WriteLine (“Hello World”)
next
Output:
For(int a=0;a<5;a++){
System.out.println(“Hello World”);
}
Example in VB.Net
For int a=0 to 5
Console.WriteLine (“Hello World”)
next
Output:
Hello World
Hello World
Hello World
Hello World
Hello World
Explain:
System.out.println(“Hello World”); = this line of code will be executed 5 times because the value of variable A starts from ZERO, so the count will be 0, 1, 2, 3, 4. It will stop at 4 because the next value 5 is no longer less than to number 5 but equal.
a++ = it will increment the value of variable A from value zero to 5.
Hello World
Hello World
Hello World
Hello World
Explain:
System.out.println(“Hello World”); = this line of code will be executed 5 times because the value of variable A starts from ZERO, so the count will be 0, 1, 2, 3, 4. It will stop at 4 because the next value 5 is no longer less than to number 5 but equal.
a++ = it will increment the value of variable A from value zero to 5.
Do While
Just like For Loop, this algorithm will execute a command repeatedly until a condition returns False.
There are three important parts necessary in order to complete the algorithm:
- Start
- Increment/Decrement
- Condition
Start = this is where the program starts to execute the command and it is represented by the word “DO” and an open curly brace.
Example: Do{
Increment = like For Loop, do while needs to increment of decrement the value of the pilot variable to meet the condition. It is usually located inside the curly brace.
Example: Do { A++; }
Condition = like For Loop, do while uses Boolean Expression to determine if it still has to execute the commands or exit the algorithm. This usually located enclosed in a parenthesis of while as a parameter.
Example: }While(A<5);
Example C/C++/Java:
Int A; //variable declaration
A = 0; //variable initializaton
Condition = like For Loop, do while uses Boolean Expression to determine if it still has to execute the commands or exit the algorithm. This usually located enclosed in a parenthesis of while as a parameter.
Example: }While(A<5);
Example C/C++/Java:
Int A; //variable declaration
A = 0; //variable initializaton
Do{ //start
System.out.println(“Hello World”); //statement
A++; //increment or could be decrement depending on the condition
}While(A<5) //condition
Example VB.Net:
Dim A as integer
A = 0
While A<5
Console.WriteLine(“HelloWorld”)
End While
Output
Hello World
Hello World
Hello World
Hello World
Hello World
Download PDF File
System.out.println(“Hello World”); //statement
A++; //increment or could be decrement depending on the condition
}While(A<5) //condition
Example VB.Net:
Dim A as integer
A = 0
While A<5
Console.WriteLine(“HelloWorld”)
End While
Output
Hello World
Hello World
Hello World
Hello World
Hello World
Download PDF File
No comments:
Post a Comment