Control flow refers to the order in which statements within a program execute. While programs typically follow a sequential flow from top to bottom, there are scenarios where we need more flexibility. This article provides a clear understanding about everything you need to know about Control Flow Statements.
Table of Content
Control flow statements are fundamental components of programming languages that allow developers to control the order in which instructions are executed in a program. They enable execution of a block of code multiple times, execute a block of code based on conditions, terminate or skip the execution of certain lines of code, etc.
Control Flow Statements Type | Control Flow Statement | Description |
---|---|---|
Conditional Statements | if-else | Executes a block of code if a specified condition is true, and another block if the condition is false. |
switch-case | Evaluates a variable or expression and executes code based on matching cases. | |
Looping Statements | for | Executes a block of code a specified number of times, typically iterating over a range of values. |
while | Executes a block of code as long as a specified condition is true. | |
do-while | Executes a block of code once and then repeats the execution as long as a specified condition is true. | |
Jump Statements | break | Terminates the loop or switch statement and transfers control to the statement immediately following the loop or switch. |
continue | Skips the current iteration of a loop and continues with the next iteration. | |
return | Exits a function and returns a value to the caller. | |
goto | Transfers control to a labeled statement within the same function. (Note: goto is generally discouraged due to its potential for creating unreadable and error-prone code.) |
Conditional statements in programming are used to execute certain blocks of code based on specified conditions. They are fundamental to decision-making in programs. Here are some common types of conditional statements:
The if statement is used to execute a block of code if a specified condition is true.