Conditional Execution II
True and False in CMost experienced C programmers would wince at the expression if(a!=0). The reason is that in the C programming language dosen't have a concept of a Boolean variable, i.e. a type class that can be either true or false. In C true is represented by any numeric value not equal to 0 and false is represented by 0. This fact is usually well hidden and can be ignored, but it does allow you to write
if(a != 0) just as if(a)because if 'a' isn't zero then this also acts as the value true.
Using break and continue Within LoopsThe break statement allows you to exit a loop from any point within its body, bypassing its normal termination expression. When the break statement is encountered inside a loop, the loop is immediately terminated, and program control resumes at the next statement following the loop. The break statement can be used with all three of C's loops.
The continue statement is somewhat the opposite of the break statement. It forces the next iteration of the loop to take place, skipping any code in between itself and the test condition of the loop. In while and do-while loops, a continue statement will cause control to go directly to the test condition and then continue the looping process. In the case of the for loop, the increment part of the loop continues. One good use of continue is to restart a statement sequence when an error occurs.
#includemain()
{
int x ;
for ( x=0 ; x<=100 ; x++)
{
if (x%2) continue;
printf("%d\n" , x);
}
}
Here we have used C's modulus operator: %. A expression:
a % bproduces the remainder when 'a' is divided by 'b'; and zero when there is no remainder.
Here's an example of a use for the break statement:
#includemain()
{
int t ;
for ( ; ; )
{
scanf("%d" , &t) ;
if ( t==10 ) break ;
}
printf("End of an infinite loop...\n");
}
Select Paths with switch
While if is good for choosing between two alternatives, it quickly becomes cumbersome when several alternatives are needed. C's solution to this problem is the switch statement. The switch statement is C's multiple selection statement. It is used to select one of several alternative paths in program execution and works like this: A variable is successively tested against a list of integer or character constants. When a match is found, the statement sequence associated with the match is 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; break;
}
Each case is labelled by one, or more, constant expressions (or integer-valued constants). The default statement sequence is performed if no matches are found. The default is optional. If all matches fail and default is absent, no action takes place. When a match is found, the statement sequence associated with that case are executed until break is encountered.
Example Program:
#includemain()
{
int i;
printf("Enter a number between 1 and 4");
scanf("%d",&i);
switch (i)
{
case 1:
printf("one");
break;
case 2:
printf("two");
break;
case 3:
printf("three");
break;
case 4:
printf("four");
break;
default:
printf("unrecognized number");
}
/* end of switch */
}
This simple program recognizes the numbers 1 to 4 and prints the name of the one you enter. The switch statement differs from if, in that switch can only test for equality, whereas the if conditional expression can be of any type. Also switch will work with only int and char types. If the statement sequence includes more than one statement they will have to be enclosed with {} to form a compound statement.