Anda di halaman 1dari 10

Unit-2

STATEMENTS
In general, a statement is a part of program that can be executed. Statement specifies an action.
C categorizes statements into three groups.
Selection
Iteration
Jump
Label
Expression
Block
1. Selection statements are if and switch.
2. The iteration statements (Loop Statements) are while, for, and do-while.
3. The jump statements are break, continue, goto, and return.
4. The label statements include the case and default statements (discussed along with the
switch statement) and the label statement itself (discussed with goto).
5. Expression statements are statements composed of a valid expression.
6. Block statements (compound statements) are simply blocks of code. (A block begins with a
{and ends with a }.)
Selection statements:
C supports 2 selection statements: if and switch. In addition, the ? Operator is an alternative
to if in certain circumstances.
if
The general form of the if statement is
if (expression)
statement;
else
statement;
where a statement may consist of a single statement, a block of statements, or nothing. The else
clause is optional.
The expression or condition following the keyword if is always enclosed within a pair of
parentheses. If the condition, whatever it is, is true, then the statement is executed. If the
condition is not true then the statement is not executed; instead the program skips past it.
(Or)
If expression evaluates to true (anything other than 0), the statement or block that forms the
target of if is executed; otherwise, the statement or block that is the target of else will be
executed, if it exists.
Ex:1 Void main( )
{
int num ;
printf ( "Enter a number less than 10 " ) ;
scanf ( "%d", &num ) ;
if ( num <= 10 )
printf ( "The num you have entered is less than or equal to 10" ) ;
}
On execution of this program, if you type a number less than or equal to 10, you get a message
on the screen through printf( ). If you type some other number the program doesnt do anything.

Ex:2 Void main( )


{
int num ;
printf ( "Enter a number less than 10 " ) ;
scanf ( "%d", &num ) ;
if ( num <= 10 )
printf ( "The num you have entered is less than or equal to 10" ) ;
else
printf ( "The num you have entered is greater than 10" ) ;
}
On execution of this program, if you enter a number less than or equal to 10, if block is
executed. If you enter some other number, else block will be executed.
A few points worth noting...
a) The group of statements after the if upto and not including the else is called an if block.
Similarly, the statements after the else form the else block.
b) Notice that the else is written exactly below the if. The statements in the if block and
those in the else block have been indented to the right.
c) Had there been only one statement to be executed in the if block and only one statement
in the else block we could have dropped the pair of braces.
d) As with the if statement, the default scope of else is also the statement immediately after
the else. To override this default scope a pair of braces as shown in the above example
must be used.
Nested ifs
A nested if is an if that is the target of another if or else. It is perfectly all right if we write an
entire if-else construct within either the body of the if statement or the body of an else statement.
This is called nestingof ifs.
In a nested if, an else statement always refers to the nearest if statement that is within the same
block as the else and that is not already associated with an else.
if(i)
{
if(j)
dosomething1();
if(k)
dosomething2(); /* this if */
else
dosomething3(); /* is associated with this else */
}
else
dosomething4(); /* associated with if(i) */

As noted, the final else is not associated with if(j) because it is not in the same block.
Rather, the final else is associated with if(i). Also, the inner else is associated with if(k), which is
the nearest if.
main( )
{
int i ;
printf ( "Enter either 1 or 2 " ) ;
scanf ( "%d", &i ) ;
if ( i == 1 )
printf ( "The number you have entered is 1" ) ;
else
{
if ( i == 2 )
printf ( " The number you have entered is 2" ) ;
else
printf ( " The number you have entered is other than 1 or 2" ) ;
}
}
The if-else-if Ladder
if-else-if ladder, sometimes called the if-else-if staircase because of its appearance.
Its general form is
if (expression) statement;
else
if (expression) statement;
else
if (expression) statement;
.
.
.
else statement;
In this case every else is associated with its previous if. The last else goes to work only if all the
conditions fail. Even in else if ladder the last else is optional.
The conditions are evaluated from the top downward.
As soon as a true condition is found, the statement associated with it is executed and the
rest of the ladder is bypassed.
If none of the conditions are true, the final else is executed. That is, if all other
conditional tests fail, the last else statement is performed.
If the final else is not present, no action takes place if all other conditions are false.

The if-else-if ladder is also be write as:


if (expression)
statement;
else if (expression)
statement;
else if (expression)
statement;
...
else
statement;
Using an if-else-if ladder, sample program is:
main( )
{
int m1, m2, m3, m4, m5, per ;
per = ( m1+ m2 + m3 + m4+ m5 ) / per ;
if ( per >= 60 )
printf ( "First division" ) ;
else if ( per >= 50 )
printf ( "Second division" ) ;
else if ( per >= 40 )
printf ( "Third division" ) ;
else
printf ( "fail" ) ;
}

The ? Alternative
The ? operator is used to replace if-else statements of the general form:
if (condition) var = expression;
else var = expression;
The ? is called a ternary operator because it requires three operands. It takes the general form
Exp1 ? Exp2 : Exp3
where Exp1, Exp2, and Exp3 are expressions.The value of a ? expression is determined as
follows: Exp1 is evaluated. If it is true, Exp2 is evaluated and becomes the value of the entire ?
expression. If Exp1 is false, then Exp3 is evaluated and its value becomes the value of the
expression. For example, consider
x = 10;
y = x>9 ? 100 : 200;
In this example, y is assigned the value 100. If x had been less than 9, y would have received the
value 200. The same code written with the if-else statement would be
x = 10;
if(x>9)
y = 100;
else
y = 200;
switch
The control statement that allows us to make a decision from the number of choices is called a
switch, or more correctly a switch-case-default. They most often appear as follows:
Switch is a built-in multiple-branch selection statement. which successively tests the value of an
expression against a list of integer or character constants. When a match is found, the statements
associated with that constant are executed. The general form of the switch statement is
switch (expression)
{
case constant1:
statement sequence
break;
case constant2:
statement sequence
break;
case constant3:
statement sequence
break;
.
.
.
default
statement sequence
}
The expression must evaluate to an integer type. Thus, you can use character or integer values,
but floating-point expressions, for example, are not allowed. The value of expression is tested
against the values, one after another, of the constants specified in the case statements. When a
match is found, the statement sequence associated with that case is executed until the break
statement or the end of the switch statement is reached. The default statement is executed if no
matches are found. The default is optional, and if it is not present, no action takes place if all
matches fail.
Case is a label statement; it cannot exist by itself, outside of a switch.
The break statement is one of Cs jump statements and uses it in loops as well as in the switch
statement. When break is encountered in a switch, program execution "jumps" to the line of
code following the switch statement.

There are three important things to know about the switch statement:
The switch differs from the if in that switch can only test for equality, whereas if can
evaluate any type of relational or logical expression.
No two case constants in the same switch can have identical values. Of course, a switch
statement enclosed by an outer switch may have case constants that are in common.
If character constants are used in the switch statement, they are automatically converted
to integers.
main( )
{
int i = 2 ;
switch ( i )
{
case 1 :
printf ( "I am in case 1 \n" ) ;
break ;
case 2 :
printf ( "I am in case 2 \n" ) ;
break ;
case 3 :
printf ( "I am in case 3 \n" ) ;
break ;
default :
printf ( "I am in default \n" ) ;
}
}
The output of this program would be:
I am in case 2
Technically, the break statements inside the switch statement are optional. They terminate the
statement sequence associated with each constant. If the break statement is omitted, execution
will continue on into the next case's statements until either a break or the end of the switch is
reached.
main( )
{
int i = 2 ;
switch ( i )
{
case 1 :
printf ( "I am in case 1 \n" ) ;
case 2 :
printf ( "I am in case 2 \n" ) ;
case 3 :
printf ( "I am in case 3 \n" ) ;
default :
printf ( "I am in default \n" ) ;
}
}
The output of this program would be:
I am in case 2
I am in case 3
I am in default
The operation of switch is shown below in the form of a flowchart for a better
understanding.
Nested switch Statements
You can have a switch as part of the statement sequence of an outer switch. Even if the case
constants of the inner and outer switch contain common values, no conflicts arise.

switch(x)
{
case 1:
switch(y)
{
case 0:
printf(''Divide by zero error.\n");
break;
case 1:
process(x, y);
break;
}
Break;
Case 2:
.
.
.

Iteration Statements
In C, iteration statements (also called loops) allow a set of instructions to be repeatedly executed
until a certain condition is reached. This condition may be predetermined (as in the for loop) or
open ended (as in the while and do-while loops).
The for Loop
for loop is probably the most popular looping instruction.
The general form of the for statement is
for (initialization; condition; increment)
statement ;
o The initialization is an assignment statement that is used to set the loop control variable.
o The condition is a relational expression that determines when the loop exits.
o The increment defines how the loop control variable changes each time the loop is
repeated.
o We must separate these three major sections by semicolons.
The for loop continues to execute as long as the condition is true. Once the condition becomes
false, program execution resumes on the statement following the for.

Anda mungkin juga menyukai