 The decision is describe to computer as a
conditional statement that can be answered true or false
 Since these statement control the flow of execution
they also known as control statements
 The if statement have different forms they are:
 Simple if statement
 If else statement
 Nested if else statement
 Else if ladder
SIMPLE IF STATEMENT
 if test expression is true the statement block
will be executed otherwise the statement block
will be skipped and the execution will jump to
next statement
Syntax
if(test expression )
{
statement block;
}
statement x;
Example program :
i= int(input(“Enter the number:”));
if (i<=10)
printf(“Condition is true”);
Output :
Enter the number:9
Condition is true
IF ELSE STATEMENT
 If test expression is true then true block
statement are executed otherwise false block
statement are executed not both block are executed
at same time.
Syntax :
if(test expression)
{
true block statement;
}
else
{
false block statement;
}
statement x;
printf("Enter your age:");
scanf("%d",&age);
if(age >=18)
{
printf("You are eligible for voting");
}
else
{
printf("You are not eligible for voting");
}
Output :
Enter your age:14
You are not eligible for voting
NESTED IF ELSE :
If condition 1 is false the statement 3 will be
executed otherwise it continue to perform
second test
if condition 2 is true the statement 1 will be
evaluated otherwise statement 2 will be
evaluated and then transferred to statement x
Syntax :
if(test condition 1)
{
if(test condition 2)
{
statement 1;
}
else
{
statement 2;
}
}
else
{
statement 3;
}
statement x;
Example program :
n=int(input(“Enter number:”));
if(n<=15)
if(n==10)
print(“Play volleyball”);
else
print(“Play cricket”);
else
print(“don’t play game”);
Output :
Enter number:10
Play volleyball
ELSE IF LADDER :
 The condition are evaluated from top of ladder
downwards.
As soon as a true condition is found the
statement associated with it is executed and the
control is transferred to the statement x.
When all n conditions become false then final
else containing the default statement will be
executed.
Syntax :
if(condition 1)
statement 1;
else if(condition 2)
statement 2;
else if(condition 3);
statement 3;
else if(condition n);
statement n;
else
default statement;
statement x;
Example program
a=int(input(“enter 1st number:”))
b=int(input(“enter 2nd number:”))
c=int(input(“enter 3rd number:”))
If(a>b)and(a>c)
print(“a is greater”)
Elseif(b<a)and(b<c)
print(“b is greater”)
Else
print(“c is greater”)
Output
enter 1st number:10
enter 2nd number:25
enter 3rd number:15
b is greater
DECISION MAKING AND LOOPING
 The test may be either to determine whether loop
has been repeated specified number of times or to
determine whether a particular condition has been
met
 The c language provides three constructs for
performing loop operations they are:
 While statement
 Do while statement
 For statement
The while is an entry controlled loop
statement
The test condition is evaluated and if
condition is true then the body of loop is
executed
After execution of the body the test
condition is once again evaluated and if it
is true the body is executed once again
The body of the loop may have one or
more statement
SYNTAX
while(test condition)
{
body of the loop
}
Example program
int count=1;
while(count<=4)
{
printf(“%d”,count);
count++;
}
OUTPUT
1 2 3 4
 The body of the loop may not be executed at all
if the condition is not satisfied at the very first
attempt
On some occasion to execute body of the loop
before the test is performed such situations can
be handled with the help of do statement
SYNTAX
Do
{
body of the loop
}
while(test condition);
Example program
int j=0;
do
{
printf("Value of variable j is: %dn", j);
j++;
}
while (j<=3);
OUTPUT
Value of variable j is: 0
Value of variable j is: 1
Value of variable j is: 2
Value of variable j is: 3
FOR LOOP
 The for loop is another entry controlled loop that
provides a more concise loop control structure
 initialization of control variables is done first
using assignment statement
 The value of control variable is tested using test
condition
SYNTAX
for(initialization;testcondition;increment)
{
body of the loop
}
Example program
int i;
for (i=1; i<=3; i++)
{
printf("%dn", i);
}
Output
1
2
3

Control and conditional statements

  • 2.
     The decisionis describe to computer as a conditional statement that can be answered true or false  Since these statement control the flow of execution they also known as control statements  The if statement have different forms they are:  Simple if statement  If else statement  Nested if else statement  Else if ladder
  • 3.
    SIMPLE IF STATEMENT if test expression is true the statement block will be executed otherwise the statement block will be skipped and the execution will jump to next statement Syntax if(test expression ) { statement block; } statement x;
  • 4.
    Example program : i=int(input(“Enter the number:”)); if (i<=10) printf(“Condition is true”); Output : Enter the number:9 Condition is true IF ELSE STATEMENT  If test expression is true then true block statement are executed otherwise false block statement are executed not both block are executed at same time.
  • 5.
    Syntax : if(test expression) { trueblock statement; } else { false block statement; } statement x;
  • 6.
    printf("Enter your age:"); scanf("%d",&age); if(age>=18) { printf("You are eligible for voting"); } else { printf("You are not eligible for voting"); } Output : Enter your age:14 You are not eligible for voting
  • 7.
    NESTED IF ELSE: If condition 1 is false the statement 3 will be executed otherwise it continue to perform second test if condition 2 is true the statement 1 will be evaluated otherwise statement 2 will be evaluated and then transferred to statement x
  • 8.
    Syntax : if(test condition1) { if(test condition 2) { statement 1; } else { statement 2; } } else { statement 3; } statement x;
  • 9.
    Example program : n=int(input(“Enternumber:”)); if(n<=15) if(n==10) print(“Play volleyball”); else print(“Play cricket”); else print(“don’t play game”); Output : Enter number:10 Play volleyball
  • 10.
    ELSE IF LADDER:  The condition are evaluated from top of ladder downwards. As soon as a true condition is found the statement associated with it is executed and the control is transferred to the statement x. When all n conditions become false then final else containing the default statement will be executed.
  • 11.
    Syntax : if(condition 1) statement1; else if(condition 2) statement 2; else if(condition 3); statement 3; else if(condition n); statement n; else default statement; statement x;
  • 12.
    Example program a=int(input(“enter 1stnumber:”)) b=int(input(“enter 2nd number:”)) c=int(input(“enter 3rd number:”)) If(a>b)and(a>c) print(“a is greater”) Elseif(b<a)and(b<c) print(“b is greater”) Else print(“c is greater”) Output enter 1st number:10 enter 2nd number:25 enter 3rd number:15 b is greater
  • 13.
    DECISION MAKING ANDLOOPING  The test may be either to determine whether loop has been repeated specified number of times or to determine whether a particular condition has been met  The c language provides three constructs for performing loop operations they are:  While statement  Do while statement  For statement
  • 14.
    The while isan entry controlled loop statement The test condition is evaluated and if condition is true then the body of loop is executed After execution of the body the test condition is once again evaluated and if it is true the body is executed once again The body of the loop may have one or more statement
  • 15.
    SYNTAX while(test condition) { body ofthe loop } Example program int count=1; while(count<=4) { printf(“%d”,count); count++; }
  • 16.
    OUTPUT 1 2 34  The body of the loop may not be executed at all if the condition is not satisfied at the very first attempt On some occasion to execute body of the loop before the test is performed such situations can be handled with the help of do statement
  • 17.
    SYNTAX Do { body of theloop } while(test condition); Example program int j=0; do { printf("Value of variable j is: %dn", j); j++; } while (j<=3);
  • 18.
    OUTPUT Value of variablej is: 0 Value of variable j is: 1 Value of variable j is: 2 Value of variable j is: 3 FOR LOOP  The for loop is another entry controlled loop that provides a more concise loop control structure  initialization of control variables is done first using assignment statement  The value of control variable is tested using test condition
  • 19.
    SYNTAX for(initialization;testcondition;increment) { body of theloop } Example program int i; for (i=1; i<=3; i++) { printf("%dn", i); } Output 1 2 3