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

Control Structures

control statements in c

Uploaded by

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

Control Structures

control statements in c

Uploaded by

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

Reg.no.

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

SEQUENCE SELECTION REPETITION


 It selects a statement to execute
 Statements are executed in a
on the basis of condition.  Statements are executed more
specified order.
 Statement is executed when the than one time on the basis of
 No statement is skipped
condition is true and ignored condition.
 No statement is executed
when it is false  E.g: while, do-while and for
more than once.
 E.g: if, if- else & switch loops.
Flow Chart
Flow Chart
Flow Chart

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

if statement Flow Chart Example Program


-used to execute the code if a
condition is true. #include<stdio.h>
-also called one-way selection main()
{
statement. int num;
Syntax
if(condition) printf( “Give a positive integer : “);
{ scanf(“%d” , &num);
//code to be executed
} if (num % 2 = = 0 )
Working {
 If the condition is evaluated to printf( “ %d is Even “ , num);
nonzero (true) then if block }
statement(s) are executed.
}
 If the condition is evaluated to
zero (false) then Control passes to
the next statement following it.

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

Nested - if statement Flow Chart


 Placing if Statement inside another if
Statement is called “Nested if”
 Sometimes we have to check further
even when the condition is TRUE.
 In these situation, we can use nested
if statements
Syntax
if(condition)
{
if(condition)
{
//code to be executed
}
}

Assignment Problems Example : to find first number is large among the


1. Program to find second number is large given three numbers
among the given three numbers
2. Program to find third number is large if (x>y)
among the given three numbers {
3. Program to find largest among the given if (x>z)
three numbers {
Printf( “ x is large “ );
}
}

C Programming Notes
Prepared by : B.Tech / I SEM /CSE-B & ECE-C /2019-2020
Reg.no. 19A21A5 5
UNIT-II: CONTROL STRUCTURES

If – else statement Flow Chart


 Used to execute the code of if block
when condition is true
 Used to execute the code of else
block when condition is false
 also called two-way selection
statement
Syntax
if(condition)
{
//code to be executed
}
else
{
//code to be executed
}

Assignment Problems Example : to find number is even or


1. Program to find whether the given number is
positive or negative. if (x%2==0)
2. Program to find a person eligible to vote or {
not. printf( “ x is even “);
}
3. Program to find largest among the given two
else
numbers {
4. Program to find smallest among the given printf( “ x is odd “);
two numbers }

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.

Syntax Flowchart Example

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

for(initialization; condition; incremnt/decremnt)


{
Statement-1;
Statement-2;
.
Statement-N;
}

for loop Working


Example

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

1. Write a program in C to display the first 10 natural numbers


2. Write a C program to find the sum of first 10 natural numbers
3. Write a program in C to display N terms of natural number and their sum
4. Write a program in C to read 10 numbers from keyboard and find their sum and average
5. Write a program in C to display the multiplication table of a given integer
6. Write a program in C to display the n terms of odd natural number and their sum
7. Write a program in C to display the pattern like right angle triangle using an asterisk
*
**
***
****
8. Write a program in C to display the first n terms of Fibonacci series.
Fibonacci series 0 1 2 3 5 8 13
9. Write a program in C to display the number in reverse order
10. Write a program in C to check whether a number is a palindrome or not

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

UNCONDITIONAL CONTROL STATEMENTS


 Used to transfer the control to any part of the program without checking a condition
 The following are types unconditional statements in C
1. break
2. continue
3. goto
4. return
5. exit() function
1. break Statement
 break is a keyword which terminates the closest enclosing statement ( while, do-while, for & switch & if)
 allows us to jump out of a loop or a block instantly
 Syntax break ;
 Example

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

Difference between break, continue and exit()

break continue exit()


Takes the control at the end of Takes the control at the beginning Terminates the C program
the block of the block for next iteration
break; continue; exit(0);
Used in loops, switch and if Used in loops and if Used anywhere in the program

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

You might also like