CONTROL FLOW STATEMENT
IF- Statement:
It is the basic form where the if statement evaluate a test condition and direct program execution depending on the result of that evaluation.
Syntax:
If (Expression)
{
Statement 1;
Statement 2;
}
Example:
if (a>b)
{
printf (“a is larger than b”);
}
Program to count the number of occurrences of the letter ‘a’ in an input stream of characters terminated with a carriage return.
#include <stdio.h>
void main()
{
int count = 0, total = 0 ;
char ch ;
while ( ( ch = getchar() ) != 13 ) // 13 is ASCII value for //carriage return
{
if ( ch == ‘a’ )
{
count ++ ;
printf( “\n Retrieved letter ‘a’ number %d\n”, count ) ;
}
total ++ ;
_flushall() ;
}
printf( “\n\n %d letters a typed in a total of %d letters.”,
count, total ) ;
}
IF-ELSE Statement:
An if statement may also optionally contain a second statement, the “else clause,” which is to be executed if the condition is not met. Here is an example:
if(n > 0)
average = sum / n;
else
{
printf(“can’t compute average\n”);
average = 0;
}
NESTED–IF- Statement:
It’s also possible to nest one if statement inside another. (For that matter, it’s in general possible to nest any kind of statement or control flow construct within another.) For example, here is a little piece of code which decides roughly which quadrant of the compass you’re walking into, based on an x value which is positive if you’re walking east, and a y value which is positive if you’re walking north:
if(x > 0)
{
if(y > 0)
printf(“Northeast.\n”);
else printf(“Southeast.\n”);
}
else
{
if(y > 0)
printf(“Northwest.\n”);
else printf(“Southwest.\n”);
}
Switch Case
This is another form of the multi way decision. It is well structured, but can only be used in certain cases where;
- Only one variable is tested, all branches must depend on the value of that variable. The variable must be an integral type. (int, long, short or char).
- Each possible value of the variable can control a single branch. A final, catch all, default branch may optionally be used to trap all unspecified cases.
Hopefully an example will clarify things. This is a function which converts an integer into a vague description. It is useful where we are only concerned in measuring a quantity when it is quite small.
switch(number)
{
case 0 : printf(“None\n”); break;
case 1 : printf(“One\n”); break;
case 2 : printf(“Two\n”); break;
case 3 :
case 4 :
case 5 : printf(“Several\n”); break;
default : printf(“Many\n”); break;
}
Program to simulate a basic calculator.
#include <stdio.h>
void main()
{
double num1, num2, result ;
char op ;
while ( 1 )
{
printf ( ” Enter number operator number\n” ) ;
scanf (“%f %c %f”, &num1, &op, &num2 ) ;
_flushall() ;
switch ( op )
{
case ‘+’ : result = num1 + num2 ;
break ;
case ‘-‘ : result = num1 – num2 ;
break ;
case ‘*’ : result = num1 * num2 ;
break ;
case ‘/’ : if ( num2 != 0.0 ) {
result = num1 / num2 ;
break ;
} // else we allow to fall through for error message
default : printf (“ERROR — Invalid operation or division by 0.0” ) ;
}
printf( “%f %c %f = %f\n”, num1, op, num2, result) ;
} /* while statement */
}
Loops
Looping is a way by which we can execute any some set of statements more than one times continuously .In c there are mainly three types of loops are use :
- while Loop
- do while Loop
- For Loop
While Loop
Loops generally consist of two parts: one or more control expressions which (not surprisingly) control the execution of the loop, and the body, which is the statement or set of statements which is executed over and over.
The general syntax of a while loop is
Initialization
while( expression )
{ Statement1 Statement2 Statement3 }
Example : Program to sum all integers from 100 down to 1.
#include <stdio.h>
void main()
{
int sum = 0, i = 100 ;
while ( i )
sum += i– ;// note the use of postfix decrement operator!
printf( “Sum is %d \n”, sum ) ;
}
For Loop
Our second loop, which we’ve seen at least one example of already, is the for loop. The general syntax of a while loop is
for( Initialization;expression;Increments/decrements )
{ Statement1 Statement2 Statement3 }
The first one we saw was:
for (i = 0; i < 10; i = i + 1)
printf (“i = %d\n”, i);
For Example : Program to print out all numbers from 1 to 100.
#include <stdio.h>
void main()
{
int x ;
for ( x = 1; x <= 100; x++ )
printf( “%d\n”, x ) ;
}
For Example :- To print out all numbers from 1 to 100 and calculate their sum.
#include <stdio.h>
void main()
{
int x, sum = 0 ;
for ( x = 1; x <= 100; x++ )
{
printf( “%d\n”, x ) ;
sum += x ;
}
printf( “\n\nSum is %d\n”, sum ) ;
}
Do while Loop
This is very similar to the while loop except that the test occurs at the end of the loop body. This guarantees that the loop is executed at least once before continuing. Such a setup is frequently used where data is to be read. The test then verifies the data, and loops back to read again if it was unacceptable.
do{
printf(“Enter 1 for yes, 0 for no :”);
scanf(“%d”, &input_value);
} while (input_value != 1 && input_value != 0);
Example : To read in a number from the keyboard until a value in the range 1 to 10 is entered.
int i ;
do
{
scanf( “%d\n”, &i ) ;
_flushall() ;
} while ( i < 1 && i > 10 ) ;
The break Statement
break can be used to force an early exit from the loop, or to implement a loop with a test to exit in the middle of the loop body. A break within a loop should always be protected within an if statement which provides the test to control the exit condition.
The continue Statement
This is similar to break but is encountered less frequently. It only works within loops where its effect is to force an immediate jump to the loop control statement.
- In a while loop, jump to the test statement.
- In a do while loop, jump to the test statement.
- In a for loop, jump to the test, and perform the iteration.
Like a break, continue should be protected by an if statement. You are unlikely to use it very often.
Take the following example:
int i;
for (i=0;i<10;i++)
{
if (i==5)
continue;
printf(“%d”,i);
if (i==8)
break;
}
This code will print 1 to 8 except 5.