Programming Constructs



Programming Here we are going to see conditional statements and looping statements Details Using Conditional Constructs The ability to take decisions is fundamental to human beings. Decision-making can be incorporated into programs as well. This helps in determining the sequence in which a program will execute instructions. You can control the flow of a program by using conditional constructs. Conditional constructs allow you to execute selective statements based on the result of the expression being evaluated. The various conditional constructs that can be included in a C# program are: The if…else construct The switch…case construct The if statement in the if…elseconditional construct is followed by a logical expression where data is compared and a decision is made on the basis of the result of the comparison. The following statements depict the syntax for the if…elseconstruct: if (expression) { statements; } else { Statements; } The switch…case Construct Another conditional construct available in C# is the switch…caseconstruct. It is used when you have to evaluate a variable for multiple values. The following code depicts the syntax for the switch…case construct: switch(VariableName) { case Constant Expression_1:statements;break; case Constant Expression_2:statements;break;... case ConstantExpression_n:statements;break; default:statements;break; } When the switchstatement is executed, the variable given in the switchstatement is evaluated and compared with each case constant. If one of the case constants is equal to the value of the variable given in the switch statement, control is passed to the statement following the matched case statement. A breakstatement is used to exit the switchstatement. This prevents the execution of the remaining case structures by ending the execution of the switch…caseconstruct. If none of the cases match, the statements under the defaultstatement are executed. The keyword switchis followed by the variable in parentheses, as shown in the following code: switch(var) Each casekeyword is followed by a possible value for the variable, as shown in the following statement: case constantValue: The data type of the constant should match the data type of the variable being evaluated by the switchconstruct. Before entering the switchconstruct, a value should be assigned to the switch variable. You can replace a complex if…elseconstruct with the switch…caseconstruct when you want to check multiple values for a variable. Using Loop Constructs Loop structures are used to execute one or more lines of code again and again. In C#, the following loop structures can be used: The whileloop The do…whileloop The forloop The while Loop The whileloop construct is used to execute a block of statements for a definite number of times, depending on a condition. The whilestatement always checks the condition before executing the statements within the loop. When the execution reaches the last statement in the whileloop, the control is passed back to the beginning of the loop. If the condition still holds true, the statements within the loop are executed again. The execution of the statements within the loop continues until the condition evaluates to false. The following statements depict the syntax for the while loop construct: while (expression) { statements; } The do…while Loop The do…whileloop construct is similar to the while loop construct. Both will continue looping until the loop condition becomes false. However, the statements within the do…whileloop are executed at least once, as the statements in the block are executed before the condition is checked. The following statements shows the syntax for the do…whileloop construct: do { statements; }while(expression); The for Loop The for loop structure is used to execute a block of statements for a specific number of times. The following statements shows the syntax of the forloop construct: for (initialization; termination; increment/decrement) { statements } The initialization expression initializes the for loop construct. It is executed once at the beginning of the loop. The termination expression checks when to end the loop. Before the execution of an iteration, this expression is evaluated. If this expression evaluates to false, the loop ends doing performing the iteration. The increment or decrement expression is called for each iteration. All these components are optional.

Sent from Windows Mail

Popular posts from this blog

ch11 review silberschatz operating systems concepts essentials 2nd ed