CONTROL STATEMENTS
Programming language constructs called control statements are used to manage how programs are executed. It enables the program to decide what to do and take different actions in response to certain circumstances. Control statements typically change the order in which statements are executed and allow for repeating or omitting some statements based on predefined circumstances.Conditional, loop, and jump statements are the three primary categories of control statements in programming. A section of code is only executed using conditional statements if a specific condition is met. To repeatedly run a block of code, use loop statements. To move control to another area of the program, utilize jump statements.
Programming is not complete without control statements since they enable more responsive and dynamic code. They make it possible for programmers to create programs that can adjust to changing circumstances and run particular code depending on specified circumstances. Programmers can make their code more effective, readable, and upkeep-free by employing control statements.
1. Conditional/selection clauses
2. Loop/Iteration statements
3. statement jumps
1)Conditional/Selection statements
- Simple if-else: The if statement alone tells us that if a condition is true it will execute a block of statements and if the condition is false it won’t. But what if we want to do something else if the condition is false. Here comes the C else statement. We can use the else statement with the if statement to execute a block of code when the condition is false. For example:-
- Else-if statement: The program first verifies the first condition (in the if statement) in an else-if statement. If the condition is met, the code block connected to the if statement is run before the entire if-else block is terminated. The following condition (in the else-if statement) is checked if the first condition is false before moving on to the next condition. If that circumstance holds true, the code block connected to the else-if statement is executed, and the entire if-else block is then terminated. If every condition is true, the programme runs the code block linked to the otherwise statement, if it is there.
The program in this illustration verifies the value of num. The first condition in the if statement is satisfied because num is bigger than 0, thus the programme performs the code block connected to that if statement (which outputs "num is positive"). The entire if-else section is then skipped by the programme. The programme would run the code block linked to the else-if expression (which outputs "num is negative") instead if num were a negative number. The programme would run the code block linked to the else statement (which outputs "num is zero") if num were zero.
- Nestedif-else: If there are multiple nested if-else statements, only the first if statement will be performed. The code in the else block is run if the outer expression returns false. Here is an illustration of a C++ layered if-else statement:
In this example, the first if statement checks if the value of num is greater than 0. If it is, the inner if-else statement checks if num is even or odd. Since num is 15, it is not divisible by 2, and so the program outputs "num is odd".
- Switch statement: The program initially evaluates the expression or variable included in brackets after the switch keyword in a switch-case statement. The value of that expression or variable is then contrasted with each of the scenarios given in the case statements. If a match is discovered, the programme runs the code block connected to that case statement before using the break statement to leave the switch block. In the absence of a match, the programme performs the code block linked to the default statement, if one exists. Here is an illustration of a C++ switch-case statement:
The program assesses the value of num in this case. The program runs the code block linked to the second case statement (which outputs "num is two") because num equals 2. The entire switch block is then terminated by the program. The code block related to the first case statement would be executed if the num were equal to 1. The program would run the code block connected to the third case statement if the num were equal to 3. The program would run the code block connected to the default statement if num had any other value.
- while loop: The program initially assesses the condition enclosed in brackets before entering a while loop. The while loop's corresponding code block is run by the program if the condition is true. The procedure is then repeated; the condition is once more assessed, and if it is still true, the code block is once more executed. So long as the condition doesn't change, this will keep happening. Here's an example of a while loop in C++:
In this instance, the program sets the variable i to zero at the beginning. The while loop is then invoked, and the programme tests whether the condition i 5 is true. Since this condition is satisfied and i is currently 0, the while loop's code block (which outputs the value of i and subsequently increments i) is executed by the programme. The procedure is then repeated by the programme, which again assesses the condition (which is still true because i is now 1) and runs the code block. This continues up until i reaches a value of 5.
- do-while loop: In a do-while loop, the code block corresponding to the loop is the first thing the program runs. After that, it assesses the parenthesized condition. The process is repeated if the condition is true: the code block is run once more, and the condition is once more evaluated. So long as the condition doesn't change, this will keep happening. Here's an example of a do-while loop in C++:
- for loop:- When you know how many times you want to iterate, you use a for loop. Initialization, a condition, and an increment or decrement make up its three components. As long as the condition remains true, the loop will keep running.
2. Continue
3. Pass
- break statement:- Utilising the break statement, one can abruptly end a loop or switch statement. Control is passed to the statement that comes after the loop or switches when the break statement is encountered within one of those statements. Here is an example of using a break in a loop:
In this example, the loop will iterate from 0 to 9. However, when i equals 5, the break statement is executed, causing the loop to terminate prematurely.
- continue statement: The continue statement is used to go on to the next iteration of a loop while skipping the current one. The remaining statements in the current iteration are skipped when the continue statement is met within a loop, and control is instead passed to the following iteration. Here is an example of using continue in a loop:
The loop will repeat from 0 to 9 times in this example. However, the continue statement is performed when i is an even integer (i.e., i% 2 == 0), leading the loop to skip the remaining statements in that iteration and move on to the following one.
- goto statement: To move from one labeled statement inside the same function to another, use the goto statement. The labeled statement can come either before or after the goto statement and must be contained within the same function. Here is an illustration of how to use goto:
The program prints "Hello" constantly in an infinite loop in this example since the labelled statement is start: and the goto statement returns control to it.
so that's it for today Guys, follow us to learn more...
Keep Learning......
No comments:
Post a Comment