by Dinesh Thakur

In some situations we may have to change the order of execution of statements based on certain conditions , or repeat a group of statements until certain specified conditions are met. For these situation C provides decision making statements also know as control statements. C has following control statements:

 

1. if statement.

2. switch statement.

3. Conditional operator.

4. goto statement.

 

1. if statement:- it is a powerful decision making statement and is used to control the flow of execution of statements. It is basically a two way dscision statement and is used in conjunction with an expression.It takes the following form:

if (Text expression)

{

};

It allows the computer to evaluate the expression first and then, depending on whether the value of the expression is ‘true’ (or non zero) or ‘false’ (zero) , it transfer the control to a particular statement. The if statement may be implemented in different forms depending on the complexity of conditions to be tested. The different forms are:

 

  1. Simple if statement

2. if …..else statement.

3. Nested if …..else statement.

4. else if ladder.

 

2. The switch statement:- C provide multiway decision statement known as a switch.

The switch statement tests the value of a given variable (or expression) against a list of case values and when a match is found , a block of statements associated with case is executed. The general form of the switch statement is as shown below:

switch(expression)

{

case value-1:

block-1;

break;

case value-2:

block-2;

break;

……

……

default:

default- block-;

break;

}

Statement-x;

The expression is an integer expression or characters. Value-1,value-2….. are constants or constant expressions and are known as case labels. Block-1,block-2 ….. are statement lists and mat contain zero or more statements. Note that case labels end with a colon(:).

 

The default is an optional case. When present, it will be executed if the value of the expression does not match with any of case values. if not present, no action takes place if all matches fail and the control goes to the statement-x.

 

3. The ? : Operator:- The C language has an unusual operator, useful for making two way decisions. This operator is a combination of ? and : ,and takes three operands. This operator is known as the conditional operator. Syntax:-

Conditional expression ? expression1 : expression2

The conditional expression is evaluated first. If the result is nonzero, expression1 is evaluated and is returned as the value of the conditional expression. Otherwise, expression2 is evaluated and its value is returned

 

4. The goto statement:- C supports the goto statement to branch unconditionally from one point to another in the program. The goto requires a label in order to identify the branch is to be made. A label I any valid variable name, and must be followed by a colon. The label is placed immediately before the statement where the control is to be transferred. Syntax:

Goto label;

-------------------

-------------------

label:

statement;

 







About Us

Dinesh ThakurDinesh Thakur is a Columinist and designer with strong passion and founder of Computer Notes. if you have any ideas or any request Find Dinesh Thakur on
linkedin FaceBook Twitter Google Plus