Conditional Statements

Note a few aspects of the above code. We have a period (.) after each sentence, but not with the ones start with while and if. Also there are two conditional sentences (statements) in the algorithms. The first one starts with a while, and the next starts with an if. These conditions are basically patterns that frequently arise within computer programs. The first one is called a while statement. It has the following form.


while condition do
steps to be done.


next instruction



We check the condition, if it is true, i.e., is satisfied, we execute the steps. But if the condition is not satisfied, is false, we don’t execute the mentioned steps of while statement and go back to the next instruction in the program. After executing the steps in the while loop we come back to check the condition again. Since we eventually keep looping within while statement as long as the specified condition is satisfied, this pattern is also called a ‘while loop’.

Each time we check the condition, we assume that we have taken an iteration.

The next conditional sentence starts with an if. This is called an if conditional statement. This is similar to while statement, but we check the condition only once. That is, after checking the condition and executing the steps (if condition is true), we don’t come back to the if statement to check the condition again. Instead, we directly proceed to the next instruction of the program. Format for an if statement is as follows:



if condition do
steps to be done.


next instruction



There is one more pattern in the above algorithm. This pattern specifies the way we decide to start and to end a statement. In the above algorithm, we used a period to separate simple sentences, and we used a pair of braces ({ }) to show the start and finish of if and while statements.

0 Response to "Conditional Statements"

Post a Comment