0% found this document useful (0 votes)
19 views15 pages

Unit II Control Structures 2

Unit II focuses on control structures in C programming, covering decision-making, branching, and looping statements. Students will learn to develop C programs that utilize input-output functions, arithmetic expressions, and various control structures like if-else, switch-case, and loops. The unit includes practical examples and a specification table for assessing learning outcomes.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views15 pages

Unit II Control Structures 2

Unit II focuses on control structures in C programming, covering decision-making, branching, and looping statements. Students will learn to develop C programs that utilize input-output functions, arithmetic expressions, and various control structures like if-else, switch-case, and loops. The unit includes practical examples and a specification table for assessing learning outcomes.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Unit II

Control Structures
14 Hours
16 Marks
Industry/ Employer expected outcome of this course:
- Develop C programs that address issues with processing strings, mathematic
operations and data structures.

Course Outcomes addressed by Unit:


- Develop C program using input - output functions and arithmetic expressions.
- Develop C program involving branching and looping statements.

Theory Learning Outcomes:


2.1. Write a ‘C’ program using decision making statements
2.2. Use loop statements in C program to solve iterative problems
2.3. Use appropriate statement to alter the program flow in the loop

Topics and Subtopics:


2.1. Conditional statements: Relational operators, logical operators, if statement, if-else
statements, nested if-else statements, if-else ladder, switch statement
2.2. Looping statements: While loop, Do…While loop, For loop
2.3. Branching Statements: goto statement, Use of break and continue statements

Suggested specification table:


Distribution of Theory Marks
Remember Level Understand Level Apply and Above Level Total Marks
04 04 08 16
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.

2.1 Conditional Statements

Relational operators are already discussed in 1.5.2 and logical operators


are discussed in 1.5.3

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 and branching statements
- Looping statements

Relational operators and logical operators are prominently used along


with these control structures.
1 of 15
2.1.1 Decision making and branching statements
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

2.1.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.1.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 2.1: Flowchart for simple if statement

2 of 15
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”);

2.1.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 2.2: Flowchart for if–else


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

2.1.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;

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.

4 of 15
test
condition
1 False

True test
condition
1 False
Statement A

True

Statement B

Statement N

Statement X

Figure 2.3: Flowchart for else–if ladder

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

5 of 15
2.1.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;
}
else
{
Statement D;
}
}
Statement X;

True test False


condition
1

True test False test


True False
condition condition
2 3

Statement A Statement B Statement C Statement D

Statement X

Figure 2.4: Flowchart for nested if–else


6 of 15
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.

Example:
if(gender==„F‟)
{
if(age>=60)
{
printf(“Eligible for Senior Citizen Women Concession”);
}
else
{
printf(“Eligible for Ladies Concession”);
}
}
else
{
if(age>=60)
{
printf(“Eligible for Senior Citizen Concession”);
}
else
{
printf(“Not Eligible for Concession”);
}
}
printf(“\nThank you for using this program”);

2.1.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;
7 of 15
----
----
default:
Statement N;
}
Statement X;

If 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.

choice-
variable

value1 Statement A

value2 Statement B

default
Statement N

Statement X

Figure 2.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:
8 of 15
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:
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);

9 of 15
break;
case 4:
c=(double)a/b;
printf(“Division = %lf”,c);
break;
default:
printf(“Wrong choice”);
}
printf(“\nThank you for using this program”);

2.1.1.3 Conditional operator


Conditional operator is discussed in 1.5.6.

2.1.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.
Syntax:
goto label-name;
Example:
START:
printf(“At start\n”);
i++;
if(i<n)
{
goto START;
}

2.2 Looping statements

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

2.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
10 of 15
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.

Initialization of a loop variable

test
condition False

True

Code to be repeated

Increment/decrement of a loop variable

Statement X

Figure 2.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;

11 of 15
}
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;

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 2.7: Flowchart for „do–while‟ loop


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

2.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.

Syntax:
for(initialization;test-condition;increment/decrement)
{
code to be repeated
}
Statement X;

Initialization of a loop variable

test
condition False

True

Code to be repeated

Increment/decrement of a loop variable

Statement X

Figure 2.8: Flowchart for „for‟ loop


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
13 of 15
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.
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”);

2.3 Use of break and continue

2.3.1 ‘break’ statement


We have already seen use of break statement for jumping out of the switch
statement. In addition to this, break statement can be used jumping out of a loop
(let it be while, do-while or for loop). When break statement is executed in a loop,
the control of execution jumps out of the loop immediately without executing
further statements in the loop or further iterations of the loop.
Example:
for(i=2;i<n-1;i++)
{
if(n%i==0)
{
printf(“The number is not prime”);
break;
}
}
printf(“\nYou have jumped out of loop”);
/* Here whenever the result of n%i is zero, execution of loop will
stop */

2.3.2 ‘continue’ statement


The „continue‟ statement can be used for skipping the remaining part of
the current iteration of loop (let it be while, do-while or for loop) and continues to
next iteration of the loop.
Example:
printf(“The odd numbers from 101 to 150 are\n”);
for(i=101;i<=150;i++)
{
if(i%2==0)
{
continue;
}
printf(“%d ”,i);
}
/* Here it will skip remaining part of each iteration when i%2
becomes zero */
14 of 15
printf(“\nThank you for using this program”);

Sample Questions

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


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

15 of 15

You might also like