Presentation is loading. Please wait.

Presentation is loading. Please wait.

Control Structures.

Similar presentations


Presentation on theme: "Control Structures."— Presentation transcript:

1 Control Structures

2 Control Structure Control structure is divided into three parts:
Selection statement Iteration statement Jumps in statement

3 Selection Statement if statement switch statement
Selection statement is also called as Decision making statements because it provides the decision making capabilities to the statements. In selection statement, there are two types: if statement switch statement These two statements allows you to control the flow of a program with their conditions.

4 Decision Making with if Statements
The “if statement” is also called as conditional branch statement. It is used to control program execution through two paths. It allows the computer to evaluate the expression first and then depending on whether the value of the expression is ‘true’ or ‘false’, it transfers the control to particular statement. Entry Test Condition ? false If (test expression) true

5 if Statement The if-statement may be implemented in different forms depending on the complexity of condition to be tested. Simple if Statement If…else Statement Nested if...else Statement else if ladder

6 Simple if Statement The general form of a simple if statement is…
The ‘Statement-block’ may be a single statement or a group of statements. If the test expression is true, the statement-block will be executed; Otherwise the statement-block will be skipped and the execution will jump to the statement-x. if (test expression) { statement-block; } statement-x;

7 The If…Else Statement The if…else statement is an extension of the simple if statement. The ‘Statement-block’ may be a single statement or a group of statements. If the test expression is true, the true-statement-block will be executed; otherwise the false-statement-block will be executed and the execution will jump to the statement-a. If (condition) { true - Statement block; } else false - Statement block; Statement-a;

8 Nesting of If…Else Statement
When the series of decisions are involved, we may have to use more than one if…else statement in nested form. If (condition1) { If (condition2) Statement block1; } else Statement block2; Statement block3; Statement 4;

9 If-Else-if Ladder if(condition-1) {//if condition-1 is true } else if (condition-2) {//if condition-2 is true } else if (condition-3) {//if condition-n is true } . . else if (condition-n) {//if condition-n is true } else { //if none of the conditions are true.} Statements which will be executed always

10 The switch Statement The switch statement provides another way to decide which statement to execute next The switch statement evaluates an expression, then attempts to match the result to one of several possible cases Each case contains one value (a constant) and a list of statements The flow of control transfers to statement associated with the first case value that matches

11 The switch Statement The general syntax of a switch statement is:
and case are reserved words switch ( expression ) { case value1 : statement-list1 case value2 : statement-list2 case value3 : statement-list3 case ... } If expression matches value2, control jumps to here

12 The switch Statement Often a break statement is used as the last statement in each case's statement list A break statement causes control to transfer to the end of the switch statement If a break statement is not used, the flow of control will continue into the next case Sometimes this may be appropriate, but often we want to execute only the statements associated with one case

13 The switch Statement switch (option) { case 'A': aCount++; break;
An example of a switch statement: switch (option) { case 'A': aCount++; break; case 'B': bCount++; case 'C': cCount++; }

14 The switch Statement A switch statement can have an optional default case The default case has no associated value and simply uses the reserved word default If the default case is present, control will transfer to it if no other case value matches If there is no default case, and no other value matches, control falls through to the statement after the switch


Download ppt "Control Structures."

Similar presentations


Ads by Google