Decision Making and Loops
These structures need the programmers to mention one or more conditions so as to be evaluated or tested by the program along with the statements to be tested and if the condition is said to be true, it will get executed or else it will skip in the false case.
- if statement: It has a boolean expression with one or more statements.
- if..else statement: If the condition for “if” statement fails, then else will get priority accordingly.
- switch statement: It allows a variable to be tested for equality over a list of values.
During execution, there may be stages where a piece of code needs to be executed a number of times. In general, statements are executed in an order. The priority of statement in a function is first one gets executed first, followed by second, and so on.
A loop statement permits us to run a statement or group of statements multiple times and following is the structure or flow of execution:
- Repeat Loop:
Executes a series of statements multiple times and reduces the program that supervises the loop variable.
- While Loop:
Repeats a statement or a group until the while test condition is true.
- For Loop:
Similar to while loop but tests the condition at the end of the loop.
Loop Control Statements
It changes execution from its normal order. All automatic objects are destroyed at the end of the loop.
- Break statement
Ends the loop statement and moves execution to the statement immediately after that statement in the loop.
- Next statement
The next statement skips the iteration which is desired and continues to next statement.