Statements and flow control - C++ Tutorials. A simple C++ statement is each of the individual instructions of a program, like the variable declarations and expressions seen in previous sections. They always end with a semicolon (; ), and are executed in the same order in which they appear in a program. But programs are not limited to a linear sequence of statements. During its process, a program may repeat segments of code, or take decisions and bifurcate. For that purpose, C++ provides flow control statements that serve to specify what has to be done by our program, when, and under which circumstances. Many of the flow control statements explained in this section require a generic (sub)statement as part of its syntax. Switch (case) Statement, used with sensor input. An if statement allows you to choose between two discrete options, TRUE or FALSE. When there are more than two options, you can use multiple if statements, or you can use the. This statement may either be a simple C++ statement, - such as a single instruction, terminated with a semicolon (; ) - or a compound statement. A compound statement is a group of statements (each of them terminated by its own semicolon), but all grouped together in a block, enclosed in curly braces: . Whenever a generic statement is part of the syntax of a flow control statement, this can either be a simple statement or a compound statement. Selection statements: if and else. The if keyword is used to execute a statement or block, if, and only if, a condition is fulfilled. Its syntax is: if (condition) statement. Here, condition is the expression that is being evaluated. If this condition is true, statement is executed. If it is false, statement is not executed (it is simply ignored), and the program continues right after the entire selection statement. For example, the following code fragment prints the message (x is 1. Its syntax is. if (condition) statement. For example: 1. 23. For example: 1. 23. Again, it would have also been possible to execute more than a single statement per case by grouping them into blocks enclosed in braces: . They are introduced by the keywords while, do, and for. The while loop. The simplest kind of loop is the while- loop. Its syntax is. while (expression) statement. The while- loop simply repeats statement while expression is true. C++ switch statement - Learning C++ in simple and easy steps : A beginner's tutorial containing complete knowledge of C++ Syntax Object Oriented Language, Methods, Overriding, Inheritance, Polymorphism, Interfaces, STL.
If, after any execution of statement, expression is no longer true, the loop ends, and the program continues right after the loop. For example, let's have a look at a countdown using a while- loop: 1. This is the first number in the countdown. Then the while- loop begins: if this value fulfills the condition n> 0 (that n is greater than zero), then the block that follows the condition is executed, and repeated for as long as the condition (n> 0) remains being true. The whole process of the previous program can be interpreted according to the following script (beginning in main): n is assigned a value. The while condition is checked (n> 0). At this point there are two possibilities: condition is true: the statement is executed (to step 3)condition is false: ignore statement and continue after it (to step 5)Execute statement. Return automatically to step 2. Continue the program right after the block: print liftoff! Otherwise, the loop will continue looping forever. In this case, the loop includes - -n, that decreases the value of the variable that is being evaluated in the condition (n) by one - this will eventually make the condition (n> 0) false after a certain number of loop iterations. To be more specific, after 1. Note that the complexity of this loop is trivial for a computer, and so the whole countdown is performed instantly, without any practical delay between elements of the count (if interested, see sleep. For example, the following example program echoes any text the user introduces until the user enters goodbye: 1. In the previous example, the user input within the block is what will determine if the loop ends. And thus, even if the user wants to end the loop as soon as possible by entering goodbye, the block in the loop needs to be executed at least once to prompt for input, and the condition can, in fact, only be determined after it is executed. The for loop. The for loop is designed to iterate a number of times. Its syntax is. for (initialization; condition; increase) statement; Like the while- loop, this loop repeats statement while condition is true. But, in addition, the for loop provides specific locations to contain an initialization and an increase expression, executed before the loop begins the first time, and after each iteration, respectively. Therefore, it is especially useful to use counter variables as condition. Example of Switch Statements . If the total of this textbox is: PostDiscountTextBox.Text = $500.00. The switch statement is a control statement that selects a switch section to execute from a list of candidates. A switch statement includes one or more switch sections. Each switch section contains one or more case. It works in the following way: initialization is executed. Generally, this declares a counter variable, and sets it to some initial value. This is executed a single time, at the beginning of the loop. If it is true, the loop continues; otherwise, the loop ends, and statement is skipped, going directly to step 5. As usual, it can be either a single statement or a block enclosed in curly braces . They can be left empty, but in all cases the semicolon signs between them are required. For example, for (; n< 1. A loop with no condition is equivalent to a loop with true as condition (i. Because each of the fields is executed in a particular time in the life cycle of a loop, it may be useful to execute more than a single expression as any of initialization, condition, or statement. Unfortunately, these are not statements, but rather, simple expressions, and thus cannot be replaced by a block. As expressions, they can, however, make use of the comma operator (,): This operator is an expression separator, and can separate multiple expressions where only one is generally expected. For example, using it, it would be possible for a for loop to handle two counter variables, initializing and increasing both: 1. Because n is increased by one, and i decreased by one on each iteration, the loop's condition will become false after the 5. Range- based for loop. The for- loop has another syntax, which is used exclusively with ranges. This kind of for loop iterates over all the elements in range, where declaration declares some variable able to take the value of an element in this range. Ranges are sequences of elements, including arrays, containers, and any other type supporting the functions begin and end; Most of these types have not yet been introduced in this tutorial, but we are already acquainted with at least one kind of range: strings, which are sequences of characters. An example of range- based for loop using strings: 1. We then use this variable, c, in the statement block to represent the value of each of the elements in the range. This loop is automatic and does not require the explicit declaration of any counter variable. Range based loops usually also make use of type deduction for the type of the elements with auto. Typically, the range- based loop above can also be written as: 1. Its purpose is to check for a value among a number of possible constant expressions. It is something similar to concatenating if- else statements, but limited to constant expressions. Its most typical syntax is: switch (expression). When it finds this break statement, the program jumps to the end of the entire switch statement (the closing brace). If expression was not equal to constant. If it is equal to this, it executes group- of- statements- 2 until a break is found, when it jumps to the end of the switch. Finally, if the value of expression did not match any of the previously specified constants (there may be any number of these), the program executes the statements included after the default: label, if it exists (since it is optional). Both of the following code fragments have the same behavior, demonstrating the if- else equivalent of a switch statement: switch exampleif- else equivalentswitch (x) . In the most typical use (shown above), this means that break statements are needed after each group of statements for a particular label. If break is not included, all statements following the case (including those under any other labels) are also executed, until the end of the switch block or a jump statement (such as break) is reached. If the example above lacked the break statement after the first group for case one, the program would not jump automatically to the end of the switch block after printing x is 1, and would instead continue executing the statements in case two (thus printing also x is 2). It would then continue doing so until a break statement is encountered, or the end of the switch block. This makes unnecessary to enclose the statements for each case in braces . For example: 1. 23. It is not possible to use variables as labels or ranges, because they are not valid C++ constant expressions. To check for ranges or values that are not constant, it is better to use concatenations of if and else if statements.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. Archives
December 2016
Categories |