0% found this document useful (0 votes)
25 views

Unit III Control Structures

The document discusses control structures in C programming, specifically decision making and branching statements. It covers simple if statements, if-else statements, else-if ladders, and nested if-else statements. Examples are provided for each type of control structure. The key learning outcomes are to write C programs using decision making structures for two-way and multi-way branching to solve problems. Looping statements are also introduced.

Uploaded by

kenemep346
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

Unit III Control Structures

The document discusses control structures in C programming, specifically decision making and branching statements. It covers simple if statements, if-else statements, else-if ladders, and nested if-else statements. Examples are provided for each type of control structure. The key learning outcomes are to write C programs using decision making structures for two-way and multi-way branching to solve problems. Looping statements are also introduced.

Uploaded by

kenemep346
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Unit III

Control Structures
10 Hours
12 Marks
Industry identified competency addressed by this course:
- Develop C programs to solve broad-based computer related problems.

Course Outcome addressed by Unit:


- Develop C programs using control structures

Learning Outcomes those are to be achieved through practical:


- Decision making and branching using if, if-else structure.
Write program to
i) Determine whether given year is leap year or not
ii) Determine whether a string is palindrome
(Expt. No. 5) (Compulsory)
- Write program to
i) Find greatest of three numbers using conditional operator
ii) Find whether given character is vowel
(Expt. No. 6) (Compulsory)
- Develop programs using switch statement
Write program to
i) Print day of week by getting numbers from 1 to 7
ii) Print student’s grade by accepting percent marks
(Expt. No. 7) (Compulsory)
- Write program to check whether the triangle is isosceles, equilateral, scalene or right-
angled triangle. (Expt. No. 8) (Compulsory)
- Looping
Write program to
i) Find sum of digits of a given number
ii) Generate multiplication table up to 10 for numbers from 1 to 5
(Expt. No. 9) (Compulsory)
- Looping
Write program to
i) Find Fibonacci series for given number
ii) Write program to produce following output
1
2 3
4 5 6
7 8 9 10
(Expt. No. 10) (Compulsory)

Major Learning Outcomes (Cognitive Domain):


3a. Write a C program using decision making structure for two-way branching to solve
given problem
3b. Write a C program using decision making structure for multi-way branching to solve
the given problem
3c. Write C program using loop statements to solve given iterative problem
1 of 15
3d. Use related statements to alter the program flow in the given loop
Affective Domain Outcomes:
- Follow safety practices.
- Practice energy conservation.
- Follow ethical practices.

Topics and Subtopics:


3.1.Decision making and branching – relational and logical operators, if statement, if-else
statement, nested if-else, if-else ladder, switch statement, goto statement
3.2.Looping – while loop, do-while loop, for loop, use of break and continue statements

Suggested specification table:


Distribution of Theory Marks
Remember Level Understand Level Apply and Above Level Total Marks
02 02 08 12
This specification table provides general guidelines to assist students for their learning
and to teachers to teach and assess students with respect to attainment of Learning
Outcomes (LOs). The actual distribution of marks at different taxonomy levels (R, U and
A) in the question paper may vary from above table.

Whatever programs we have developed till this point are executed in


sequential manner. But practically there is a need to change the flow of
execution of the statements in the program. For achieving it, following types of
control structures may be used.
- Decision making statements
- Looping statements

Relational operators and logical operators are prominently used along


with these control structures. These operators are already discussed in 2.2.7.2
and 2.2.7.3.

3.1 Decision making and branching

When programmer want to change path of execution, (s)he may use


decision making and branching statements. Various decision-making and
branching statements supported by C are
- if
- switch-case
- Conditional operator
- goto statement

3.1.1 ‘if’ statements


C supports following variants of if statements
- Simple if
- if-else
- else-if ladder (or if-else ladder)
- Nested if-else

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;

The test-condition may be an expression containing relational operators


and logical operators. Statement A gets executed if and only if outcome of test-
condition is true. As statement X is after if construct, it will get executed
regardless of whether outcome of test condition is True or False.

test
condition False

True

Statement A

Statement X

Figure 3.1: Flowchart for simple if statement


Example 1:
if(n%9 == 0)
{
printf(“%d is divisible by 9”,n);
}
printf(“\nThank you for using this program”);

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;

Statement A gets executed if outcome of test-condition is true. If outcome


is false, Statement B gets executed. As statement X is after if construct, it will
get executed regardless of whether outcome of test condition is True or False.

test
condition False

True

Statement A Statement B

Statement X

Figure 3.2: Flowchart for if–else


Example:
if(n%2 == 0)
{
printf(“%d is EVEN”,n);
}
else
{
printf(“%d is ODD”,n);
}
printf(“\nThank you for using this program”);

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

Figure 3.3: Flowchart for else–if ladder

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”);

3.1.1.4 Nested if–else


If series of decisions are involved, programmer may use nested if–else.
One of the possible syntax for nested if–else is given here.

One of the Syntax:


if(test-condition 1)
{
if(test-condition 2)
{
Statement A;
}
else
{
Statement B;
}
}
else
{
if(test-condition 3)
{
Statement C;

6 of 15
}
else
{
Statement D;
}
}
Statement X;

Statement A gets executed if outcomes of test-condition 1 and 2 are true.


Statement B gets executed if outcome of test-condition 1 is true and outcome of
test-condition 2 is false. Statement C gets executed if outcome of test-condition 1
is false and outcome of test-condition 3 is true. Statement D gets executed if
outcomes of test-condition 1 and 3 are false. As statement X is after if construct,
it will get executed regardless of outcomes of test conditions.

test
True False
condition
1

test test
True False True False
condition condition
2 3

Statement A Statement B Statement C Statement D

Statement X

Figure 3.4: Flowchart for nested if–else


Example:
if(gender==„F‟)
{
if(age>=60)
{
printf(“Eligible for Senior Citizen Women Concession”);
}
else
{
printf(“Eligible for Ladies Concession”);
}

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;

If the value of choice-variable matches with value1, Statement A gets


executed. If it matches with value2, Statement B gets executed and so on. If
value of choice-variable does not match with any value, code written in default
case (i.e. Statement N) gets executed. On execution of break statement, control of
execution comes out of switch case and Statement X executes. If programmer
forgets to write break statement, multiple cases may get executed. So break
statement is important (if not used, syntax error is not generated). The default
case is also not compulsory. But it is always a good habit to use it (as it handles
unexpected situations).
The data type of choice-variable as well as values mentioned in cases may
be either integer (any of six integer data types) or char.

8 of 15
choice-
variable

value1 Statement A

value2 Statement B

default
Statement N

Statement X

Figure 3.5: Flowchart for switch–case


Example 1:
int day_of_week;
printf(“Enter a day of week: ”);
scanf(“%d”,&day_of_week);
switch(day_of_week)
{
case 1:
printf(“Monday”);
break;
case 2:
printf(“Tuesday”);
break;
case 3:
printf(“Wednesday”);
break;
case 4:
printf(“Thursday”);
break;
case 5:
printf(“Friday”);
break;
case 6:
printf(“Saturday”);
break;
case 7:
9 of 15
printf(“Sunday”);
break;
default:
printf(“No such day”);
}
printf(“\nThank you for using this program”);

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”);

3.1.3 Conditional operator


Conditional operator is discussed in 2.2.7.6.

3.1.4 goto statement


If programmer want to transfer the control of execution directly to a
specific statement, it can be achieved using goto statement. Syntax for using goto
statement is given here.

10 of 15
Syntax:
goto label-name;
Example:
START:
printf(“At start\n”);
i++;
if(i<n)
{
goto START;
}

3.2 Looping

When programmer want to execute a single statement or a set of


statements multiple times, (s)he may use looping statements. Each repetition of
such code is called iteration. Various looping constructs supported by C are
- while
- do–while
- for

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

3.2.1 while loop


It is entry-controlled loop. The control of execution enters into the loop if
the outcome of test-condition is true. Syntax for while loop is given here.
Syntax:
initialization of a loop variable
while(test-condition)
{
code to be repeated
increment / decrement of a loop variable
}
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.
11 of 15
Initialization of a loop variable

test
condition False

True

Code to be repeated

Increment/decrement of a loop variable

Statement X

Figure 3.6: Flowchart for „while‟ loop


Example:
printf(“Odd numbers from 1 to 10 are:\n”);
i=1;
while(i<=10)
{
printf(“%d\n”,i);
i=i+2;
}
printf(“\nThank you for using this program”);

3.2.2 do–while loop


It is exit-controlled loop. Code inside the loop gets executed at least once.
The test-condition is checked at the end of loop for deciding whether to re-enter
the loop or to exit the loop. Syntax for do-while loop is given here.
Syntax:
initialization of a loop variable
do
{
code to be repeated
increment / decrement of a loop variable
} while(test-condition);
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.

Initialization of a loop variable

Code to be repeated

Increment/decrement of a loop variable

True test
condition

False

Statement X

Figure 3.7: Flowchart for „do–while‟ loop


Example:
printf(“Even numbers from 1 to 10 are:\n”);
i=2;
do
{
printf(“%d\n”,i);
i=i+2;
} while(i<=10);
printf(“\nThank you for using this program”);

3.2.3 for loop


It is also entry-controlled loop. The control of execution enters into the
loop if the outcome of test-condition is true. Advantage of for loop is that all the
important things for looping are made compact in a single statement. Syntax for
„for‟ loop is given here.

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.

Initialization of a loop variable

test
condition False

True

Code to be repeated

Increment/decrement of a loop variable

Statement X

Figure 3.8: Flowchart for „for‟ loop


Example:
printf(“Cubes of 1 to 10 are:\n”);
for(i=1;i<=10;i++)
{
printf(“%d\n”,i*i*i);
}
printf(“\nThank you for using this program”);

14 of 15
Sample Questions

1. State relational operators with example. [2M]


2. Implement a program to demonstrate logical AND operator. [4M]
3. State any four decision making statements. [2M]
4. Write a program to accept value of year as input from keyboard and print
whether it is leap year or not. [4M]
5. Explain nested if-else with example. [4M]
6. Give syntax of if-else ladder. [2M]
7. Write syntax of switch case statement. [2M]
8. Write a program using switch statement to check whether entered
character is VOWEL or CONSONANT. [6M]
9. Write a program to add, subtract, multiply and divide two numbers
accepted from user using switch-case. [4M]
10. State use of while loop with syntax. [2M]
11. Write a program to take input as a number and reverse it by while loop.
[6M]
12. Develop a program to accept an integer and print whether it is palindrome
or not. [4M]
13. Design a program to print a message 10 times. [4M]
14. Draw a flowchart of do-while loop and write a program to add numbers
until user enters zero. [6M]
15. State any two differences between while and do-while statement. [2M]
16. Write a program to sum all odd numbers between 1 to 20. [4M]
17. Write a program to calculate sum of all odd numbers between 1 to 20. [4M]
18. Explain do-while loop with example. [4M]
19. Illustrate use of break and continue statements with examples. [4M]

15 of 15

You might also like