Unit III Control Structures
Unit III Control Structures
Control Structures
10 Hours
12 Marks
Industry identified competency addressed by this course:
- Develop C programs to solve broad-based computer related problems.
2 of 15
3.1.1.1 Simple if
When one wants to execute a statement on meeting a specific condition
only, (s)he may use simple if statement. Syntax for simple if is given here.
Syntax:
if(test-condition)
{
Statement A;
}
Statement X;
test
condition False
True
Statement A
Statement X
Example 2:
if(a>b && a>c)
printf(“%d is greatest among %d, %d and %d”,a,a,b,c);
printf(“\nThank you for using this program”);
3 of 15
3.1.1.2 if–else statement
When one wants to select path of execution depending on outcome of test-
condition, (s)he may use if–else statement. Syntax for if-else is given here.
Syntax:
if(test-condition)
{
Statement A;
}
else
{
Statement B;
}
Statement X;
test
condition False
True
Statement A Statement B
Statement X
4 of 15
3.1.1.3 else-if ladder (or if–else ladder)
If multiple conditions need to be verified on failure of previous conditions,
programmer may use else-if ladder (or if-else ladder). Syntax for else-if ladder is
given here.
Syntax:
if(test-condition 1)
{
Statement A;
}
else if(test-condition 2)
{
Statement B;
}
else if(---)
---
else
{
Statement N;
}
Statement X;
test
condition
1 False
True test
condition
1 False
Statement A
True
Statement B
Statement N
Statement X
5 of 15
Statement A gets executed if outcome of test-condition 1 is true.
Statement B gets executed if outcome of test-condition 2 is true and so on. If all
the conditions are false, Statement N gets executed. As statement X is after if
construct, it will get executed regardless of outcomes of test conditions.
Example:
if(percent>=75)
{
printf(“First Class with distinction”);
}
else if(percent>=60)
{
printf(“First Class”);
}
else if(percent>=50)
{
printf(“Second Class”);
}
else if(percent>=40)
{
printf(“Pass Class”);
}
else
{
printf(“No class”);
}
printf(“\nThank you for using this program”);
6 of 15
}
else
{
Statement D;
}
}
Statement X;
test
True False
condition
1
test test
True False True False
condition condition
2 3
Statement X
7 of 15
}
else
{
if(age>=60)
{
printf(“Eligible for Senior Citizen Concession”);
}
else
{
printf(“Not Eligible for Concession”);
}
}
printf(“\nThank you for using this program”);
3.1.2 switch-case
In some situations programmer may need to select only one path from
multiple available paths. In such situation, switch-case construct may be
preferred. Syntax for switch–case is given here.
Syntax:
switch(choice-variable)
{
case value1:
Statement A;
break;
case value2:
Statement B;
break;
case value3:
Statement C;
break;
----
----
default:
Statement N;
}
Statement X;
8 of 15
choice-
variable
value1 Statement A
value2 Statement B
default
Statement N
Statement X
Example 2:
char ch;
int a,b;
double c;
printf(“Enter two numbers: ”);
scanf(“%d%d”,&a,&b);
printf(“*** MENU ***\n”);
printf(“A for Addition\nS for Subtraction\nM for Multiplication”);
printf(“\nD for Division”);
printf(“\nWhich operation do you want to perform? ”);
scanf(“%c”,&ch);
switch(ch)
{
case „A‟:
c=a+b;
printf(“Addition = %lf”,c);
break;
case „S‟:
c=a–b ;
printf(“Subtraction = %lf”,c);
break;
case „M‟:
c=a*b;
printf(“Multiplication = %lf”,c);
break;
case 4:
c=(double)a/b;
printf(“Division = %lf”,c);
break;
default:
printf(“Wrong choice”);
}
printf(“\nThank you for using this program”);
10 of 15
Syntax:
goto label-name;
Example:
START:
printf(“At start\n”);
i++;
if(i<n)
{
goto START;
}
3.2 Looping
There is a chance that a loop may get executed indefinite number of times.
Programmer should take utmost care to avoid such situation. Generally every
loop involves following important things
- Initialization of a loop variable
- Test-condition
- Code to be repeated
- Increment/decrement of a loop variable
dsd
The „initialization of a loop variable‟ gets executed once. The code inside
the while loop (i.e. „code to be repeated‟ and „increment/decrement of a loop
variable‟) gets executed repeatedly for multiple number of times when outcome of
test-condition is true. Control comes out of the loop (and statement X gets
executed) when outcome of test-condition is false. It may happen that the code
inside the loop is never executed, if outcome of the test-condition is false at the
first trial. Programmer should take proper care while writing the test-condition.
11 of 15
Initialization of a loop variable
test
condition False
True
Code to be repeated
Statement X
12 of 15
The „initialization of a loop variable‟ gets executed once. The code inside
the do-while loop (i.e. „code to be repeated‟ and „increment/decrement of a loop
variable‟) gets executed repeatedly for multiple number of times when outcome of
test-condition is true. Control comes out of the loop (and statement X gets
executed) when outcome of test-condition is false. Even if outcome of the test-
condition is false, the code inside this loop gets executed at least once.
Code to be repeated
True test
condition
False
Statement X
13 of 15
Syntax:
for(initialization;test-condition;increment/decrement)
{
code to be repeated
}
Statement X;
The „initialization of a loop variable‟ gets executed once. The code inside
the while loop (i.e. „code to be repeated‟ and „increment/decrement of a loop
variable‟) gets executed repeatedly for multiple number of times when outcome of
test-condition is true. Control comes out of the loop (and statement X gets
executed) when outcome of test-condition is false. It may happen that the code
inside the loop is never executed, if outcome of the test-condition is false at the
first trial. Programmer should take proper care while writing the test-condition.
test
condition False
True
Code to be repeated
Statement X
14 of 15
Sample Questions
15 of 15