A program is nothing but the execution of sequence of one or more instructions. Quite often, it is desirable to alter the sequence of the statements in the program depending upon certain circumstances.

  1. To alter the flow of a program
  2. Test the logical conditions
  3. Control the flow of execution as per the selection these conditions can be placed in the program using decision-making statements.

C supports mainly three types of control statements

    a. Decision making statements
  1. Simple if Statement
  2. if -else Statement
  3. Nested if-else statement
  4. else -if Ladder
  5. switch statement
    b. Loop control statements
  1. for Loop
  2. while Loop
  3. do-while Loop
    c. Unconditional control statements
  1. goto Statement
  2. break Statement
  3. continue Statement

Decision making statements

(1) Simple "if" statement :

The "if" statement is a powerful decision making statement and is used to control the flow of execution of statements.

if (Condition or test expression)
{
Statement;
}
Rest of the program;

  1. It is basically a "Two-way" decision statement (one for TRUE and other for FALSE)
  2. The statement as executed only when the condition is true.
  3. In case the condition is false the compiler skips the lines within the "if Block".
  4. The condition is always enclosed within a pair of parenthesis ie ().
  5. The conditional statement should not the terminated with Semi-colons (ie ;)
  6. The Statements following the "if"- statement are normally enclosed in Curly Braces ie {}.
Example: 

# include
# include 
void main( ) 
{ 
int m,n; clrscr( );
printf("\n Enter two numbers:");
scanf("%d %d", &m, &n);
if((m-n)= =0)
printf("\n two numbers are equal");
getch();
}

(2)"if-else" Statement :

It is observed that the if statement executes only when the condition following if is true.
It does nothing when the condition is false.
In if-else either True-Block or False -Block will be executed and not both.
The "else" Statement cannot be used without "if".

Syntax:

if ( Test Expression or Condition )
{
Statements;
}
else
{
Statements;
}

Example:

Write a program to print the given number is even or odd
# include 
# include 
main( )
{
int n; 
clrscr( );
printf("Enter a number");
scanf("%d", &n);
if( (n%2)==0 )
printf("\n The given number is EVEN "); 
else
printf("\n The given number is ODD ""); 
getch( );
}

(3) Nested "if-else" Statement:

Using of one if-else statement in another if-else statement is called as nested if-else control statement. When a series of decisions are involved, we may have to use more than one if-else statement in nested form.

Syntax: 

if (Test Condition1)
 {
 if ( Test Condition2) 
 { 
 Statement -1; 
 }
 else
 {
 Statement -2;
 }
 } 
 else 
 { 
 if ( Test Condition3)
 {
Statement -3;
}
else 
{
 Statement -4;
 }
}

If Test Condition-1 is true then enter into outer if block, and it checks Test Condition-2 if it is true then Statement-1 executed if it is false then else block executed i.e Statement-2. If Test Condition -1 is false then it skips the outer if block and it goes to else block and Test Condition-3 checks if it is true then Statement-3 executed, else Statement-4 executed.

Example:

# include
 # include
 main( )
 {
 float a,b,c;
 printf("Enter Three Values");
 scanf("%f%f%f", &a, &b, &c); 
 printf("\n Largest Value is") ;
 if(a>b)
 {
 if(a>c) printf("%f", a);
 else
 printf("%f",c);
 }
 else 
 {
 if (b>c) 
 printf(" %f", b); 
 else printf("%f", c);
 }
 getch( ); 
 }

The "else - if" Ladder:

This is another way of putting if s together when multiple decisions are involved. A multipath decision is a chain of if s in which the statement associated with each else is an if. Hence it forms a ladder called else-if ladder.

Write a program to read three numbers and find the largest one by using "else-if" ladder.

# include
 # include
 main( ) 
 { 
 int a, b, c
 clrscr ( );
 printf("Enter 1st number");
 scanf("%d", &a);
 printf("Enter 2nd number");
 scanf("%d", &b); 
 printf("Enter 3rd number");
 scanf("%d", &c);
 if ((a>b) && (a>c))
 printf("Highest Number is %d", a);
 else if((b>a) && (b>c))
 printf("Highest Number is %d", b);
 else
 printf("Highest Numbers is %d", c);
 getch( );
 }

The "switch-case" Statement:

The switch statement causes a particular group of statements to be chosen from several available groups. The selection is based upon the current value of an expression which is included within the switch statement. The switch statement is a multi-way branch statement.

In a program if there is a possibility to make a choice from a number of options, this structured selected is useful. The switch statement requires only one argument of int or char data type, which is checked with number of case options. The switch statement evaluates expression and then looks for its value among the case constants. If the value matches with case constant, then that particular case statement is executed.

    The switch( ) Organization:
  1. The entire case structure following switch( ) should be enclosed with pair of curly braces { }.
  2. In the block the variable or expression can be a character or an integer.
  3. Each case statement must contain different constant values.
  4. Any number of case statements can be provided.
  5. If the case structure contains multiple statements, they need not be enclosed within pair of curly braces.
 Example:
 
# include
 # include
 main( )
 {
 int a, b, c, ch;
 clrscr ( ) ;
 printf("\t = = = = = = = = = = "");
 printf ("n\t MENU");
 printf(“\n\t= = = = = = = = = = =”);
 printf("\n \t  ADDITION" );
 printf("\n \t  SUBTRACTION");
 printf("\n \t  MULTIPLICATION" );
 printf("\n \t  DIVISION");
 printf("\n \t  REMAINDER" );
 printf("\n \t  LARGER OUT OF TWO" );
 printf("\n \t  EXIT" );
 printf("\n \t = = = = = = = ");
 printf("" \n\n\t ENTER YOUR CHOICE");
 scanf("%d", &ch); 
 if(ch < = 6 && ch >=1)
 { printf("ENTER TWO NUMBERS:"); 
 scanf("%d %d”, &a, &b); }
 switch(ch)
{
case 1: 
c = a+b ;
 printf("\n Addition: %d", c);
 break;
 case 2:
 c=a-b; printf("\n Subtraction: %d", c);
 break; 
 case 3:
 c = a* b ;
 printf("\n Multiplication: %d", c);
 break;
 case 4:
 c = a / b; printf("\n Division: %d", c);
 break; 
 case 5:
 c = a % b; printf("\n Remainder: %d", c);
 break; 
 case 6: 
 if (a > b) printf("\n \t %d is larger than %d", a, b);
 else if (b > a) 
 printf("\n \t %d is larger than %d", b, a);
 else printf("\n \t %d and %d are same", a, b);
 break; 
 case 7:
 printf("\ n Terminated by choice");
 exit( );
 break;
 default:
 printf("\ n invalid choice"); 
 }
 getch ( );
}

Loop Control Statements

The "for" loop

"initialize expression", "Test Condition expression" and "updation expression"
The expressions are separated by Semi-Colons (;).
The loop variable should be assigned with a starting and final value.
Each time the updated value is checked by the loop itself.
Increment / Decrement is the numerical value added or subtracted to the variable in each round of the loop.

Syntax:

for(initialize expression; test condition; updation )
{
Statement-1;
Statement-2;
}

Example: the first five numbers starting from one together with their squares.

#include
 #include
 main( )
 {
 int i; 
 clrscr( );
 for(i = 1; i <=5; i++) 
 printf("\n Number: %d its Square %d", i, i*i);
 getch( );
 }

Nested "for" loop:

We can also use loop within loops. i.e. one for statement within another for statement is allowed in C. (or C allows multiple for loops in the nested forms). In nested for loops one or more for statements are included in the body of the loop. ANSI C allows up to 15 levels of nesting. Some compilers permit even more

Syntax:

for( initialize ; test condition ; updation)
{
for(initialize ; test condition ; updation)
{
Body of loop;
}
}

Example: Program to perform subtraction of 2 loop variables. Use nested for loops.

#include 
#include  
 void main( ) 
 { 
 int a, b,
 sub;
 clrscr( );
for (a=3; a > =1; a - - )
 {
 for(b=1;b<=2;b++)
 {
 sub = a – b;
 printf(“a=%d b=%d a-b = %d \n”, a,b, sub);
 }
 } 
 getch( );
 }

The while loop

Syntax:

Initialization Expression;
while( Test Condition)
{
Body of the loop Updaion Expression
}

The while is an entry -controlled loop statement. The test condition is evaluated and if the condition is true, then the body of the loop is executed. The execution process is repeated until the test condition becomes false and the control is transferred out of the loop.

On exit, the program continues with the statement immediately after the body of the loop. The body of the loop may have one or more statements. The braces are needed only if the body contains two or more statements. Its a good practice to use braces even if the body has only one statement.

Example:

 # include
 # include
 main() 
 { 
 int a=1, Sum=0;
 clrscr() ;
while(a<=10)
{ 
Sum = Sum + a; a++;
 }
 printf("Sum of 1 to 10 numbers is: %d", sum);
 getch();
 }

The do-while loop:

In do-while, the condition is checked at the end of the loop. The do-while loop will execute at least one time even if the condition is false initially. The do-while loop executes until the condition becomes false.

Syntax:

Initialization Expression;
do
{
Body of the loop Updation Expression;
}
while ( Test Condition);

Example:

# include
# include
main()
 {
 int n, x=2; 
 clrscr();
 printf("Enter the number for testing (prime or not"); 
 scanf("%d", &n); 
 do { if(n%x = = 0)
 {
 printf("\n the number %d is not prime", n);
 exit(0);
 } 
 x++; 
 } 
 while ( x < n);
 printf("\n the number %d is prime", n);
 getch();
 }

Unconditional Control Statements

The break Statement

A break statement terminates the execution of the loop and the control is transferred to the statement immediately following the loop. i.e., the break statement is used to terminate loops or to exit from a switch. It can be used within a for, while, do-while, or switch statement. The break statement is written simply as break;

Example:

 switch (choice==toupper(getchar( ))
 { 
 case "R": printf("Red");
break;
 case "W":
 printf("White");
 break; 
 case "B":
 printf("Blue");
 break;
 default:
 printf("Error");
 }

The "continue" Statement

The continue statement is used to bypass the remainder of the current pass through a loop. The loop does not terminate when a continue statement is encountered. Instead, the remaining loop statements are skipped and the computation proceeds directly to the next pass through the loop. The continue statement can be included within a while, a do-while, a for statement.

It is simply written as "continue". The continue statement tells the compiler "Skip the following Statements and continue with the next Iteration". In "while" and "do" loops continue causes the control to go directly to the test -condition and then to continue the iteration process.

Example:

# include 
void main() 
{ 
int i=1, num, sum =0;
 for(i=0; i < 5; i ++) 
 {
 printf("Enter an integer");
 scanf("%d", &num); if(num < 0) 
 {
 printf("you have entered a negative number");
continue ;
} 
sum + = num; 
}
 printf("The sum of the Positive Integers Entered = % d \ n", sum); 
}

The "goto" Statement

C supports the "goto" statement to branch unconditionally from one point to another in the program. Although it may not be essential to use the “goto” statement in a highly structured language like C, there may be occasions when the use of goto is necessary. The goto requires a label in order to identify the place where the branch is to be made.

A label is any valid variable name and must be followed by a colon( : ). The label is placed immediately before the statement where the control is to be transferred. The label can be any where in the program either before or after the goto label statement.

Example:

# include 
# include 
# include 
void main()
{
int x;
 clrscr( );
 printf("Enter a Number"); 
 scanf("%d", &x); if(x % 2 = = 0) 
 goto even;
 else 
 goto odd;
 even:
 printf("\n %d is Even Number"); 
 return;
 odd:
 printf("\n %d is Odd Number"); 
 }

News Walls


Copyrights © 2016. All Rights Reserved to Learn Computers Home, India. //Site Designed.