Vous êtes sur la page 1sur 15

Control Statements in C

In normal C program executes in a sequential order. When a program breaks the sequential order and jumps the another part of the code is called as branching. When branching is based on particular condition So it is known as Conditional Branching. C supports various kinds of decision making or control statements. They are divided into 3 types : 1) Selection Statements 2) Iteration Statements 3) Jump Statements
Sreenivas

RIM (I) Pvt Ltd

8/13/2012

O Selection Statements : -

These are 3 types : Simple if if else Switch


RIM (I) Pvt Ltd Sreenivas
8/13/2012

O Simple if : The if statement is a powerful selection statement which is used to control the flow of execution of statements. Syntax :if(boolean Expression) { Statements; } if the condition is true the block associated with if gets executed, otherwise it skips to the next statements to be executed.
RIM (I) Pvt Ltd Sreenivas
8/13/2012

O If else : The if else statement is an extension of the if statement. Syntax :if(boolean Expression) { Statements; } else { Statements; } if the condition is true then if block gets executed, otherwise else block will be executed.
RIM (I) Pvt Ltd Sreenivas
8/13/2012

Switch : The switch statement is used to check with multiple alternatives. The expression when value is to be checked can be an arithmetic or logical expression

Syntax : switch(expression) { case value1 : Statements1;break: case value2 : Statements2;break: case value n: Statements3;break: default : default Statements ; } We can not use all the data types along with the switch statement. We can use char, int , byte , short types only.
RIM (I) Pvt Ltd Sreenivas
8/13/2012

O Iteration Statements : -

These are 3 types : while do while for


RIM (I) Pvt Ltd Sreenivas
8/13/2012

O While : -

The while statement is used to carry out the looping operations. It has the following form. Syntax : while(condition) { Statements; } During execution the condition is tested first based on the value , the statements forms a loop, as long as the condition true the loop repeats. Once the condition becomes false, then its comes out of the loop.
RIM (I) Pvt Ltd Sreenivas
8/13/2012

O do-while : -

The do while also using for carry out the looping operations. Syntax:do { statements; } while(Condition); In do-while first the block of statements are gets executed and then condition testing is done.
RIM (I) Pvt Ltd Sreenivas
8/13/2012

O for : -

It has 3 expressions. The first expression indicates initialization. Second expression indicates condition and the third expression may be increment or decrement. Syntax:for(initilization;condition;inc/dec) { statements; } for loop also allows the declaring the variables inside the loop.
RIM (I) Pvt Ltd Sreenivas
8/13/2012

If we declare the variable inside the for loop , the

scope of that variable is limited to the for loop only. when ever we want to need represent the more than one expressions in the same for loop then we can use separators between them. In for loop statement either initialization or inc/dec statements are absent, but the separators are necessary. Just like an other programming languages c also supports nested loops that allows loop may be inside another.
RIM (I) Pvt Ltd Sreenivas
8/13/2012

Jump Statements : java supports 3 jump statements.


1) break 2) continue 3) return

These are used to transfer the control from one part of the program to another part of the program.

RIM (I) Pvt Ltd

Sreenivas

8/13/2012

1) Break : -

It is used to terminate the loop in between.

2) Continue : It can be used to transfer the control from loop block to increment or decrement block.
3) Return : The return statement is used to transfer a value for a method definition to method declaration .
RIM (I) Pvt Ltd Sreenivas
8/13/2012

O Errors : -

There are 4 types of errors in C Language. They are : 1. Compile time errors 2. Linking errors 3. Logical errors 4. Runtime errors

RIM (I) Pvt Ltd

Sreenivas

8/13/2012

O Typecasting : -

The process of converting one data type into another data type is called as typecasting. Escape sequences : These are special characters, which performs some formatting operations on the output to be displayed on the screen.
Escape Sequence \n \t \a Description New Line Horizontal tab Alert the system bell

\0
\\
RIM (I) Pvt Ltd Sreenivas

Null Character
Back Slash
8/13/2012

Vous aimerez peut-être aussi