C- Control Statement

 Control Statement in 'C' 

 
To control the flow of the execution of programs, the 'C' language provides the following statements:

a.Conditional statement : These statements enable us to change the flow of the program. In other words, these statements are used to transfer the control from one point to another (depends on condition (s) ). In 'C' language following conditional statements are used:

if statement/if-else statement
This statement allows decisions to be made by evaluating a given condition as true or false. The keyboard it tells the compiler that it is a decision control instructions. The condition if is always enclosed within  a pair of parenthesis. The relational operators allow us to compare two values to see whether they are equal to each other, greater or less than the other. The general form of it statement is 

if (condition)
{
statement 
}
in the above example statement are executed when the condition is true.
if  (condition (s) )
{
statement 
}
else
{
statement 2
}
in this statement 1 will be executed when the condition is true otherwise statement 2 will be executed.

b. Switch Case statement :
This control statement, which allows us to make a decision from the number of choices, is called a switch. With the help of switch and case statement we may choose any number of decisions, or more correctly a switch-case default, since the combination of these three words go to together to make up the control statement .

Syntax:-
switch (expression)
{
case 1 :
statement (group1)
Case 2:
Statement ( group 2)
Case 3 :
Statement (group 3)
default:
Statement  (group 4)
}
The switch statement checks, whether an expression matches with number of integer or character constant.

Example: for "if-else"

#include<stdio.h>
void main ()
{
int age:
printf ("Enter your age");
scanf ("%d,"& age);
if (age> =18)
printf ("You can vote");
else
printf ("You can't vote");
}
 
Example: for "switch -case"
To calculate simple interest and compound interest.

#include<stdio.h>
#include <conio.h>
Void main ( )
{
int ch;
float p, r, t, si, ci;
clrscr ( );
printf ("Enter principle");
scanf ("%f". & p);
printf (" Enter rate ");
scanf ("%f", & r);
printf (" Enter time ");
scanf ("%f", & t);
printf (" 1.Simple Interest");
printf (" 2.Compund Interest");
printf (" Enter your choice ");
scanf ("%d", & ch);
switch (ch)
{
case 1:
si=(p*r*t)/100;
printf ("%f ", si);
break;
case 2:
ci = p((1+r/100)^t-p);
printf ("%f ", ci);
break;
default:
printf ("wrong choice");
}
getch ( );
}

Looping statement : A computer program is set of statement, which is normally executed sequentially. But in most of the cases it is necessary to repeat certain steps to meet a specific condition. This repetitive operation is done through a loop control  structure. 
A loop is basically the execution of sequence of statement repeatedly until a particular condition is true or false. In "C" language following looping statement are used.

1. While statement :
The while loop repeats a statement or a set of statement until a certain condition is true.
Syntax:
while (condition) 
{
Statements
}
Here, the condition may be any expression having non-zero value. The loop continues until the condition is true. When the condition fails, the program body attached with loop (statement), will not be executed.

Example: To print first 20 natural numbers
#include<stdio.h>
#include <conio.h>
void main ( )
{
int a =1 
while (a <=20)
printf ("%d". a);
a++;
}
getch ( );
}

do- while statement :
Working of this statement is similiar to the working of while statement but in this, at least one time the attached loop ( statement) is executed, no matter whether the condition is true or false.
Syntax: 
do
{
  Statement 
} while (condition);

Example : To print first 10 odd numbers
#include <stdio.h>
void main ( )
{
int a=1;
do
{
printf ("%d",a);
a+=2;
{ while (a<=19);
}

for Loop statement :
The for loop is an ideal looping statement when we know how many times the loop will be executed.
Syntax-
for (initialization ; condition;counter)
{
statements
}
Here, 
. Initialization is generally an assignment which is used to set the loop control variable, e.g., a=0.
.Condition is always tells the limit of the loop or determines when to exit the loop.
e.g., a<10
. Counter defines how the loop control variable will change each time the loop is repeated. This may be incremented or decremented.
e.g., a++, a--.

Examples:
To print your name 10 times.
#include <stdio.h>
#include <conio.h>
void main ( ) 
{
int i;
char name [20]
clrscr ( );
printf ("Enter your name");
gets (name)
for (i=1;i<=10;i++)
{
puts (name);
}
getch();
}