C - Statements and blocks
Share
Statements: C has three types of statement.
* assignment
=
* selection (branching)
if (expression)
else
switch
* iteration (looping)
while (expression)
for (expression;expression;expression)
do {block}
Statements and blocks:
if - else:
The if - else statement is used to express decisions. Formally, the syntax is
if (expression)
statement 1
else
statement 2
Where the else part is optional . The expression is evaluated; If it is true ( that is , if expression has a non-zero value ),
statement 1 is is excuted. If it is false ( expression is zero ) and if there is an else part , statement 2 is executed instead.
Since an if simply tests the numeric value of an expression, certain coding shortcuts are possible. The most obvious is writing
if ( expresssion )
instead of
if ( expression != 0)
Sometimes this is natural and clear ; at others times it can be cryptic
Because the else part of an if-else is optional , there is an ambiguity when an else is omitted from a nested if sequence. This is resolved
by associating the else with the closest previous else - less if .
For example in
if ( n > 0 )
if ( a > b)
z = a;
else
z = b;
rhe else goes with the inner if . as we have shown by indentation. If that isn't What you want , braces must be used to force the proper
association:
if ( n > 0 )
{
if ( a > b)
z = a;
}
else
z = b;
Example Program:
1. if else sample program
Else - If
By using Else If, it is possible to combine several conditions. Only the statements following the first condition that is found to be true will be executed. All other statements will be skipped. The statements of the final Else will be executed if none of the conditions are true. This example is written in the
The Construction
if ( expression )
statement
else if ( expression )
statement
else if ( expression )
statement
else if ( expression )
statement
else
statement
eg,
if (result >= 45)
printf("Pass ");
else
printf("Fail ");
Example program:
1. Else If sample program
Switch:
The Switch statement is a multi- way decision that tests whether an expression matches one of a number of constants integer values, and branches accordingly
switch ( expression ) {
case const-expr: statements
case const-expr: statements
-------
--------
default: statements
}
switch ( expression ) {
case value1:
program statement;
program statement;
......
break;
case valuen:
program statement;
.......
break;
default:
.......
.......
break;
}
The keyword break must be included at the end of each case statement. The default clause is optional, and is executed if the cases are not met. The right brace at the end signifies the end of the case selections.
eg,
switch( Grade )
{
case 'A' : printf( "Excellent" );
break;
case 'B' : printf( "Good" );
break;
case 'C' : printf( "OK" );
break;
case 'D' : printf( "Mmmmm...." );
break;
case 'F' : printf( "You must do better than this" );
break;
default : printf( "What is your grade anyway?" );
break;
}
Rules for switch statements:
values for 'case' must be integer or character constants
the order of the 'case' statements is unimportant
the default clause may occur first (convention places it last)
you cannot use expressions or ranges
Switch Example Program:
1. Switch Sample program.
Comments:
|
Submitted By:
Prof: Software Engineer
Tech: C ,Cpp
|