Control Structures
Control Structures
19A21A5 1
UNIT-II: CONTROL STRUCTURES
RELATIONAL EXPRESSIONS
Expressions in which “relational operators “are used OPERATOR EXPESSION OUTPUT
Relational operators also called comparison operators
There are six relational operator (<, >, <=, >=, ==, !=)
All operators are binary and have equal precedence
Output of all operators are Boolean ( TRUE or FALSE)
If the output of relational expression is
FALSE- then returns zero (0)
TRUE- then non-zero (1) ( +ve or –ve)
SYLLABUS
C Programming Notes
Prepared by : B.Tech / I SEM /CSE-B & ECE-C /2019-2020
Reg.no. 19A21A5 2
UNIT-II: CONTROL STRUCTURES
CONTROL STRUCTURES
Used to “alter the flow of execution” of the program (or)
Determines the order in which statements are executed.
TYPES : programs are written using the following three types of control structures in C
1. Sequence
2. Selection / Decision
3. Repetition/ Looping / Iteration
C Programming Notes
Prepared by : B.Tech / I SEM /CSE-B & ECE-C /2019-2020
Reg.no. 19A21A5 3
UNIT-II: CONTROL STRUCTURES
BRANCHING/SELECTION/DECISION/CONDITIONAL
STATEMENTS
CONDITIONAL STATEMENTS
Help us to make a decision based on certain condition.
Condition is relational or logical expression which will have Boolean value true or false.
(or) Used to execute/transfer the control from one part of the program to another based on a condition
There are following types of conditional statements in C.
1. if statement
1.1 nested if
2. if-else statement
2.1 if-else-if ladder
3. switch statement
Assignment Problems
1. Program to read two integers and compare the first integer with second , if the first number is greater
than second print “ greater than the second “ or if smaller “ smaller than the second “ or “both are
identical “.
2. Program to find the given number is positive
3. Program to find the given number is negative
4. Program to find given is positive or negative or zero
5. Program to find given number is odd
6. Program to find given number is even
7. Program to find given number is odd or even
C Programming Notes
Prepared by : B.Tech / I SEM /CSE-B & ECE-C /2019-2020
Reg.no. 19A21A5 4
UNIT-II: CONTROL STRUCTURES
C Programming Notes
Prepared by : B.Tech / I SEM /CSE-B & ECE-C /2019-2020
Reg.no. 19A21A5 5
UNIT-II: CONTROL STRUCTURES
C Programming Notes
Prepared by : B.Tech / I SEM /CSE-B & ECE-C /2019-2020
Reg.no. 19A21A5 6
UNIT-II: CONTROL STRUCTURES
If – else – if ladder or Nested else-if Example
It is also called a multi-way selection statement. #include<stdio.h>
When a series of the decision are involved in a main()
statement we use this statement. {
int a;
The condition is tested from the top (of the ladder)
to downwards. printf("Enter a Number: ");
As soon as the true condition will found, the scanf("%d", &a);
statement associated with it is executed.
Syntax if(a > 0)
{
if (condition 1)
printf("Given Number is Positive");
{
}
// block 1
}
else if(a == 0)
else if (codition 2)
{
{
printf("Given Number is Zero");
// block 2
}
}
else if(codition 3)
else if(a < 0)
{
{
// block 3
printf("Given Number is Negative");
}
}
else
{
}
// default block
}
Assignment Problems
1. Program to find largest among the given two numbers
2. Program to find smallest among the given two numbers
3. Program to find the value of Y using
C Programming Notes
Prepared by : B.Tech / I SEM /CSE-B & ECE-C /2019-2020
Reg.no. 19A21A5 7
UNIT-II: CONTROL STRUCTURES
switch Statement
Also called multi-way conditional statement
Tests the value of a variable and compares it with multiple cases.
Once the case match is found, a block of statements of a case is executed.
Each case in a block of a switch has unique name/number.
The value compared with all the cases inside the switch block until the match is found.
If there is no match found, then default block is executed (if present).
break statements are used to exit the switch block.
It isn't necessary to use break after each block
But if we do not use it, then all the consecutive blocks of code will get executed after the matching block.
switch(var/exp)
{
case value-1:
block-1; printf("Enter character (R/G/B): ");
break; color= getchar();
case value-2: switch (color)
block-2; {
break; case 'R':
printf ("Red") ;
case value-n: break;
case 'G':
block-n; printf("Green");
break; break;
default: case 'B':
default-block; printf("Blue");
break;
Assignment Problems
1. Program to find the value of Y using
2. Program to print name of color according the given letter from VIBGYOR, the color of rainbow
3. Program to find number of days in a month using C
4. Program to create a simple calculator
C Programming Notes
Prepared by : B.Tech / I SEM /CSE-B & ECE-C /2019-2020
Reg.no. 19A21A5 8
UNIT-II: CONTROL STRUCTURES
LOOPING/REPETITIVE/ITERATIVE STATEMENTS
A loop is used for executing a block of statements repeatedly until a given condition returns false.
There are three types of loop statements
1. while
2. do-while
3. for
1. while Loop
Used for executing a block of statements repeatedly until a given condition returns false
Also known as entry controlled or top tested loop
If the condition is not true first time the control ever enter into loop.
Syntax Flow chart
Initialization;
while ( condition)
{
Statement-1;
Statement-2;
.
Statement-N;
Increment/decrement;
}
2. do-while Loop
It is more like a while statement, except that it tests the condition at the end of the loop body
Also known as exit controlled or bottom tested loop
If the condition is not true first time the control enters into loop.
Syntax Flow chart
Initialization;
do
{
Statement-1;
Statement-2;
.
Statement-N;
Increment/decrement;
} while ( condition);
C Programming Notes
Prepared by : B.Tech / I SEM /CSE-B & ECE-C /2019-2020
Reg.no. 19A21A5 9
UNIT-II: CONTROL STRUCTURES
3. for Loop
Used for executing a block of statements repeatedly until a given condition returns false.
This is one of the most frequently used loop
Syntax Flow chart
C Programming Notes
Prepared by : B.Tech / I SEM /CSE-B & ECE-C /2019-2020
Reg.no. 19A21A5 10
UNIT-II: CONTROL STRUCTURES
Example program
While loop Do –while loop For loop
#include <stdio.h> #include <stdio.h>
main() main() #include <stdio.h>
{ { main()
int i, n; int i, n; {
printf("Enter any number: "); printf("Enter any number: "); int i, n;
scanf("%d", &n); scanf("%d", &n); printf("Enter any number: ");
scanf("%d", &n);
printf("Natural numbers :”); printf("Natural numbers :”);
printf("Natural numbers :”);
i=1; i=1;
while(i<=n) do for(i=1; i<=n; i++)
{ { {
printf("%d", i); printf("%d", i); printf("%d", i);
i++; i++; }
} }
} } while(i<=n);
} }
Output
Assignment Problems
C Programming Notes
Prepared by : B.Tech / I SEM /CSE-B & ECE-C /2019-2020
Reg.no. 19A21A5 11
UNIT-II: CONTROL STRUCTURES
Nested loops
Loop inside another loop is called a nested loop.
The depth of nested loop depends on the complexity of a problem.
We can have any number of nested loops as required.
Consider a nested loop where the outer loop runs n times and consists of another loop inside it.
The inner loop runs m times. Then, the total number of times the inner loop runs during the program execution is
n*m.
Nested while loop Nested do-while loop Nested for loop
while (condition1) do for (initialize; condition; incre/decre)
{ { { statement(s);
statement(s); statement(s); for (initialize; condition; incr/decr)
while (condition2) do {
{ { statement(s);
statement(s); statement(s); ... ... ...
... ... ... ... ... ... }
} } while (condition2); ... ... ...
... ... ... ... ... ... }
} } while (condition1);
C Programming Notes
Prepared by : B.Tech / I SEM /CSE-B & ECE-C /2019-2020
Reg.no. 19A21A5 12
UNIT-II: CONTROL STRUCTURES
2. continue Statement
continue is a keyword
Passes control to the next iteration of the enclosing iteration statement( while, do-while, for & if)
allows us to takes the control beginning of a loop instantly for next iteration
Sometimes desirable to skip some statements inside the loop. In such cases, continue is used
Syntax continue ;
Example
C Programming Notes
Prepared by : B.Tech / I SEM /CSE-B & ECE-C /2019-2020
Reg.no. 19A21A5 13
UNIT-II: CONTROL STRUCTURES
3. goto statement
Also referred to as unconditional jump statement.
Used to jump from anywhere to anywhere within a function.
Transfers program control directly to a labeled statement
Syntax
goto label;
..
.
label: statement;
Example
4. return statement
Terminates execution of a method and returns control to calling method, and may return a value if provided.
5. exit () function
The exit function, declared in the standard include file <stdlib.h>
Terminates a C program.
The value supplied as an argument to exit is returned to the operating system
C Programming Notes
Prepared by : B.Tech / I SEM /CSE-B & ECE-C /2019-2020
Reg.no. 19A21A5 14
UNIT-II: CONTROL STRUCTURES
C Programming Notes
Prepared by : B.Tech / I SEM /CSE-B & ECE-C /2019-2020