2. Control Statements
A program consists of a number of statements which are usually executed in sequence.
Programs can be much more powerful if we can control the order in which statements
are run.
Statements fall into three general types;
1. Assignment, where values, usually the results of calculations, are stored in variables.
2. Input / Output, data is read in or printed out.
3. Control, the program makes a decision about what to do next.
11/29/2020 2
3. Control Statements
Control statements in C are used to write powerful programs by:
1. Repeating important sections of the program.
2. Selecting between optional sections of a program.
11/29/2020 3
5. Control Statements cont.
Structure Meaning Example
Sequential It means the instructions are carried
out in sequence
a+=5;
a=a+b;
Selection Sequence of the instruction are
determined by the result of the
condition
if (x>y)
a = a+1;
else
a= a-1;
Iteration Here the statements are repeatedly
executed
while (a<=10)
printf (“%d” , a);
Encapsulation Here the an iteration statement can
have selection inside it or vice versa
while (a<=10)
{
if(a>5)
printf (%d” , a);
}
11/29/2020 5
‘C’ language provides four general structure by which statements can be executed
7. Types of Selection Statement
1. Simple if Selection statement
2. Nested if Selection statement
3. if else Selection statement
4. Nested if else Selection statement
5. else if ladder Selection statement
11/29/2020 7
8. Simple if Selection statement
It is used to control the flow of execution of the statements and also to
test logically whether the condition is true or false.
if the condition is true then the statement following the “if “ is executed if
it is false then the statement is skipped.
11/29/2020 8
Syntax:
if ( condition )
{
statement ;
}
Test Condition
Executable X - Statement
True
9. Selection Statement
Properties of an if statement:
1. if the condition is true then the simple or compound statements are executed.
2. If the condition is false it will skip the statement.
3. The condition is given in parenthesis and must be evaluated as true or false.
4. If a compound structure is provided, it must be enclosed in opening and closing braces
11/29/2020 9
10. //Biggest of Two Numbers
#include <stdio.h>
void main()
{
int a, b;
printf(“Enter the A and B Value:n”);
scanf(“%d,%d”, &a, &b);
if (a > b)
{
printf(“A is Big”);
}
}
11/29/2020 10
11. Basic Relational Operators
1. Basic Logic Operators can be used to combine more than one
condition in an “if” statement or invert the result of a condition
2. The 3 Basic Logic Operators are
&& , || , !
(and, or, not)
11/29/2020 11
1. Basic Relational Operators can be used in C to make “if” statement
conditions more useful
2. The 6 Basic Relational Operators are
> , <, >= , <= , == , !=
(greater than, less than, greater than or equal to, less than or equal
to, equal to, not equal to)
Basic Logic Operators
12. The if else statement
It is used to execute some statements when the condition is true and execute some other
statements when the condition is false depending on the logical test.
11/29/2020 12
Syntax:
if ( condition )
{
statement 1 ; (if the condition is true this statement will be executed)
}
else
{
statement 2 ; (if the condition is false this statement will be executed)
}
Test Condition
Executable X - Statement
True
Executable Y - Statement
False
13. if else statements
if (result >= 50)
{
printf ( “ Passedn “ ) ;
printf ( “ Congratulationsn “ );
}
else
{
printf ( “ Failedn “ ) ;
printf ( “ Good luck in the resitsn “ ) ;
}
11/29/2020 13
14. // Biggest of Two Numbers
#include <stdio.h>
void main()
{
int a, b;
printf(“Enter the A and B Value:n”);
scanf(“%d,%d”, &a, &b);
if (a > b)
{
printf(“A is Big”);
}
else
{
printf(“B is Big”);
}
}
11/29/2020 14
15. // Given Number is ODD or EVEN Number
#include <stdio.h>
void main()
{
int n;
printf(“Enter the Number:n”);
scanf(“%d”, &n);
if (n % 2 == 0)
{
printf(“Given Number is Even Number”);
}
else
{
printf(“Given Number is Odd Number”);
}
}
11/29/2020 15
16. Nested if….. else statement
11/29/2020 16
Syntax:
if ( condition 1)
{
if ( condition 2)
statement 1 ;
else
statement 2 ;
}
else
{
if (condition 3)
statement 3;
else
statement 4;
}
when a series of if…else statements are occurred in a program, we can write an entire if…else
statement in another if…else statement called nesting
18. // Biggest of Three Numbers
#include<stdio.h>
void main()
{
int a, b, c;
printf(“Enter the Three Numbers:n”);
scanf(“%d%d%d”,&a,&b,&c);
if (a > b)
{
if (a > c)
printf(“A is Big”);
else
printf(“C is Big”);
}
else
{
if (b > c)
printf(“B is Big”);
else
printf(“C is Big”);
}
}
11/29/2020 18
19. else if Ladder or Multiple if else Statements
11/29/2020 19
Syntax:
if (condition_1)
executed statement_1;
else if (condition_2)
executed statement_2;
else if (condition_3)
executed statement_3;
----------------------
----------------------
else if (condition_n)
executed statement_n;
else
executed statement_x;
When a series of decisions are involved we have to use more than one if – else statement called as
multiple if’s. Multiple if – else statements are much faster than a series of if – else statements, since the if
structure is exited when any one of the condition is satisfied.
20. 11/29/2020 20
Test Condition_1
Test Condition_2
Exec. Stat_1
Test Condition_3
TRUE
Test Condition_n
Exec. Stat_2
Exec. Stat_3
Exec. Stat_nExec. Stat_X
TRUE
TRUE
TRUE
FALSE
FALSE
FALSE
FALSE
22. main()
{
int n1,n2;
int val;
char op;
printf("Enter a simple expression ");
scanf("%d%c%d",&n1,&op,&n2);
if(op == '+')
val = n1 + n2;
else if(op == '-')
val = n1 - n2;
else if(op == '/')
val = n1 / n2;
else if(op == '*')
val = n1 * n2;
11/29/2020 22
else
{
printf(“?? operator %cn",op);
exit(1);
}
printf("%d%c%d = %dn",n1,op,n2);
}
/*This program reads in a simple expression with a very restricted format and prints out its value. */
23. Sample Program
Write a program to calculate the sales commission for the
data given below:
Sales value (Rs) Commission(%)
Less than 1000 No commission
Above 1000 but below 2000 5% of sales
Above 2000 but below 5000 8% of sales
Above 5000 10% of sales
11/29/2020 23
24. #include<stdio.h>
#include<conio.h>
void main()
{
float sales, com;
printf(“Enter the sales value :”);
scanf(“%f”, &sales);
if(sales<=1000)
com = 0;
else if(sales>1000 && sales <=2000)
com = sales*5/100;
else if(sales>2000 && sales <=5000)
com = sales*5/100;
else
com = sales * 10/100;
printf(“The commission for the sales value %f is %f”, sales, com);
}
11/29/2020 24
25. THE SWITCH STATEMENT
• The control statements which allow us to make a decision from the number of choices is called switch (or)
Switch-case statement.
• It is a multi way decision statement, it test the given variable (or) expression against a list of case value.
11/29/2020 25
switch (expression)
{
case constant 1:
simple statement (or)
compound statement;
case constant 2:
simple statement (or)
compound statement;
case constant 3:
simple statement (or)
compound statement;
}
switch (expression)
{
case constant 1:
simple statement (or)
compound statement;
case constant 2:
simple statement (or)
compound statement;
default :
simple statement (or)
compound statement;
}
26. Example Without Break Statement
#include<stdio.h>
void main ()
{
int num1,num2,choice;
printf(“Enter the Two Numbers:n”);
scanf(“%d%d”,&num1,&num2);
printf(“1 -> Additionn””);
printf(“2->Subtractionn”);
printf(“3->Multiplicationn”);
printf(“4->Divisionn”);
printf(“Enter your Choice:n”);
scanf(“%d”, &choice);
switch(choice)
{
case 1:
Printf(“Sum is %dn”, num1+num2);
case 2:
Printf(“Diif. is %dn”, num1-num2);
case 3:
Printf(“Product is %dn”, num1*num2);
case 4:
Printf(“Division is %dn”, num1/num2);
default:
printf (“Invalid Choice…..n”);
}
} 11/29/2020 26
#include<stdio.h>
void main ()
{
int num1,num2,choice;
printf(“Enter the Two Numbers:n”);
scanf(“%d%d”,&num1,&num2);
printf(“1 -> Additionn””);
printf(“2->Subtractionn”);
printf(“3->Multiplicationn”);
printf(“4->Divisionn”);
printf(“Enter your Choice:n”);
scanf(“%d”, &choice);
switch(choice)
{
case 1:
printf(“Sum is %dn”, num1+num2);
break;
case 2:
printf(“Diif. is %dn”, num1-num2);
break;
case 3:
printf(“Product is %dn”, num1*num2);
break;
case 4:
printf(“Division is %dn”, num1/num2);
break;
default:
printf (“Invalid Choice…..n”);
}
}
Example With Break Statement
27. Rules for Switch
• The expression in the switch statement must be an integer or character constant.
• No real numbers are used in an expression.
• The default is optional and can be placed anywhere, but usually placed at end.
• The case keyword must be terminated with colon (:);
• No two case constant are identical.
• The values of switch expression is compared with case constant in the order specified i.e. from top to bottom.
• The compound statements are no need to enclose within pair of braces.
• Integer Expression used in different case statements can be specified in any order.
• A switch may occur within another switch, but it is rarely done. Such statements are called as nested switch
statements.
• The switch statement is very useful while writing menu driven programs.
11/29/2020 27
28. Limitations of using a switch statement
• Only One variable can be tested with the available case statements with the values
stored in them (i.e., you cannot use relational operators and combine two or more
conditions as in the case of if or if – else statements).
• Floating – point, double, and long type variables cannot be used as cases in the
switch statement.
• Multiple statements can be executed in each case without the use of pair of braces
as in the case of if or if – else statement.
11/29/2020 28