16
Decision Making and Branching in C SUMAN AGGARWAL ASSISTANT PROFESSOR ADVANCED EDUCATIONAL INSTITUTE ww.advanced.edu.in

C language control statements

Embed Size (px)

Citation preview

Page 1: C language  control statements

Decision Making and Branching in C

SUMAN AGGARWALASSISTANT PROFESSORADVANCED EDUCATIONAL INSTITUTE

www.advanced.edu.in

Page 2: C language  control statements

In programming the order of execution of instructions may have to be changed depending on certain conditions. This involves a kind of decision making to see whether a particular condition has occurred or not and then direct the computer to execute certain instructions accordinglyC language posses such decision making capabalities by supporting the following ststement:1. if statement2. Nesting of if else Statement3. switch statement4. goto statement

www.advanced.edu.in

Page 3: C language  control statements

Decision making with if statement The if statement is a powerful decision making statement and is used to control the flow of execution of statements.

The syntax is

if (expression)

The expression is evaluated and depending on whether the value of the expression is true (non zero) or false (zero) it transfers the control to a particular statement

www.advanced.edu.in

Page 4: C language  control statements

The general form of simple if statement is:

if (test expression){statement block;}statement n;

www.advanced.edu.in

Page 5: C language  control statements

Small Description

www.advanced.edu.in

Syntax is:if (test expression){statement block;}else{statement block;}statement n;

Page 6: C language  control statements

Nesting of if....else statement:

www.advanced.edu.in

When a series of conditions are to be checked, we may have to use more than one if... else statement in the nested form.

if (test condition 1){

if (test condition 2){

statement block 1;}

else{

statement block 2;} statement m;

}else

{if (test condition 3)

{statement block 3;

}else

{statement block 4

} statement n;}

statement x; -

Page 7: C language  control statements

Description of if....else statement: If the test condition 1 is true then, test condition 2 is checked and if it is true, then the statement block 1 will be executed and the control will be transferred to statement m and it will executed and then statement x will be executed.

If the test condition 1 is true but test condition 2 is false, statement block 2 will be executed and the control is transferred to statement m and it will be executed and then statement x will be executed.

If the test condition 1 is false, then test condition 3 is checked and if it is true, statement block 3 will be executed, then control is transferred to statement n and it will be executed and then statement x will be executed.

If the test condition 1 is false and test condition 3 is also false, statement block 4 will be executed, then the control is transferred to statement n and it will be executed and then statement x is executed.

-

www.advanced.edu.in

Page 8: C language  control statements

Switch Statement

www.advanced.edu.in

The switch structure is a multiple-selection structure that allows even more complicated decision statements than a two-way if/else structure allows.

It chooses one of the "cases" depending on the result of the control expression.

Only variables with the INT or CHAR data types may be used in the control expressions (i.e. parentheses) of switch statements.

Single quotes must be used around CHAR variables Single quotes are NOT used around the integer value

Page 9: C language  control statements

Switch Syntax

www.advanced.edu.in

switch (expression){case value 1 :statement block 1;break;case value 2:statement block 2;break;::default:default block;}statement n;

Page 10: C language  control statements

Flowchart Switch Statement

www.advanced.edu.in

Page 11: C language  control statements

Description Switch Statement

www.advanced.edu.in

The expression is an integer expression or characters. Value 1, value 2, ...... are constants , constant expressions (evaluable to an integral constant) or character and are known as case labels. Each of these values should be unique within a switch statement, statement block 1, statement block 2, ........ are statement list and may contain 0 or more statements. There is no need to put braces between these blocks. The case labels end with a colon (:). When the switch is executed, the value of the expression is successively compared against the values value 1, value 2, ...... If a case is found whose value matches with the value of the expression, then the block of statement that follows that case are executed. The break statement at the end of each block, signals the end of a particular case and causes an exit from the switch statement transferring the control to the statement n following the switch block. The default is an optional case. When present, it will be executed if the value of the expression does not match with any of the case values and then the statement n will be executed. If not present no action takes place if all matches fails and the control goes to statement n.

Page 12: C language  control statements

The goto statement

www.advanced.edu.in

This statement is used to branch unconditionally from one point to another in the program. This statement goto requires a label to locate the place where the branch is to be made. A label is any valid identifier and must be followed by a colon. The label is placed immediately before the statement where the control is to be transferred. Different ways of using goto statement are given below:

Syntax: goto label;

Page 13: C language  control statements

Forward jump

www.advanced.edu.in

•Forward jump: In this the position of the label is after the goto statement.goto label;::label:statement n;Example:goto read;n = 5 * 4;::read:scanf ("%d", &code);•::

Page 14: C language  control statements

Backward jump

www.advanced.edu.in

Backward jump: In this, the position of the label is before the goto statementlabel:statement n;: goto label;

Example:. . read:scanf ("%d", &code);::goto read;n = 5 * 4;::

Page 15: C language  control statements

Conclusion

www.advanced.edu.in

The C language programs presented until now follows a sequential form of execution of statements. Many times it is required to alter the flow of the sequence of instructions. C language provides statements that can alter the flow of a sequence of instructions. These statements are called control statements. These statements help to jump from one part of the program to another. The control transfer may be conditional or unconditional.

Page 16: C language  control statements

Suman AggarwalAssistant Professor

Advanced Educational Institutions

◦ Advanced Educational Institutions,◦ 70 km Milestone,

◦ Delhi-Mathura Road, Dist. Palwal, Haryana-121105

◦ +91–1275–398400, 302222

Contact Email Id: [email protected] Website: http://www.advanced.edu.in

www.advanced.edu.in

“THANK YOU”