Nothing Special   »   [go: up one dir, main page]

Ip Iy Iis Unit 2

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 13

UNIT – II

CONTROL STRUCTURES
1. What is Control Statement? Explain Branching Statements?
Control Flow Statement: In most of C programs, the instructions that were executed in the same
order in which they appeared in the program. In some situations the statement(s) should be
executed either in conditional way or in iterative way. To handle such types of statement(s), some
flow controls are required. These are called as Control Statements.
The control statements are 5 types:
1. Sequential Control Statements
2. Decision / Branching Control Statements
3. Loop / repetitive / Iterative Control Statements
4. Case Control Statements
5. Jump Control Statements.
1. Sequential Control Statements: These statements are evaluated by the system in the order of
their appearance in program.
2. Branching (Decision Making) Statements: These statements allow the system to execute a
block of statements by having its own decision.

➢ Simple if

➢ If else

➢ Nested if else
Simple if Statement: When only one condition occurs in a statement, then simple if statement is
used having one block.
Syntax: FLOW CHART:
if (condition)
{
True statement block;
}
Statement – x;

// C Program to demonstrate the syntax of if statement


#include <stdio.h>
int main()
{
int number = 9;
// if statement with true condition
if (number < 10)
{
printf("%d is less than 10", number);
}
// if statement with false condition
if (number > 20)
{
printf("%d is greater than 20", number);
}
return 0;
}
If – else Statement: This statement also has a single condition with two different blocks. One is
true block and another one is false block.
Syntax:
if (condition) FLOW CHART:
{
True statement block;
}
else
{
False statement block;
}
Statement – x;
Example:

Write a program to find the given number is even or odd.

#include<stdio.h>

void main()

{
int number;

printf("Enter an integer: ");

scanf("%d",&number);

if( number%2 == 0 )

printf("%d is an even integer.",number);

else

printf("%d is an odd integer.",number);

Output:

Enter an integer: 7

7 is an odd integer.

Nested if – else Statements: When an if statement occurs within another if statement, then it is
called nested if statement. It is some complex than the simple if or if – else statements, but very
useful to solve real problems.

Example: To check whether a number is positive, negative or zero.


#include<stdio.h>
#include<conio.h>
int main()
{
int number;
printf("enter a number:");
scanf("%d",&number);
if(number>=0)
{
if(number>0)
{
// executes when number is
positive
printf("enter a postive
number");
}
else
{
//executes when number is
zero
printf("u entered a zero\n");
}
}
else{
//executes when number is negative
printf("u entered a negative");
}
getch();
return 0;
}

Ladder if (Or) else if Statement: When in a complex problem number of conditions arise in a
sequence, then we can use Ladder- if or else if statement to solve the problem in a simple
manner. In this the condition will be checked, if it is true then action will be taken, otherwise
further next condition will be checked and this process will continue till the end of the condition.
Syntax:
if(expression 1)
{
statement-block1;
}
else if(expression 2)
{
statement-block2;
}
else if(expression 3)
{
statement-block3; }
else
{
Statement-block4;
}
Example :
C Program to print grade of a student using If Else Ladder Statement
#include<stdio.h>
void main()
{
int marks;
printf("Enter your marks ");
scanf("%d",&marks);
if(marks < 0 II marks > 100)
{
printf("Wrong Entry");
}
else if(marks<50)
{
printf("Grade F");
}
else if(marks>=50 && marks<60)
{
printf("Grade D");
}
else if(marks >= 60 && marks<70)
{
printf("Grade C");
}
else if(marks>=70 && marks<80)
{
printf("Grade B");
}
else if(marks >= 80 && marks < 90)
{
printf("Grade A");
}
else
{
printf("Grade A+");
}
}
Output: Enter your marks 78
Grade B
3. Loop / repetitive / Iterative Control Statements:
The repetition is done until condition becomes condition true. A loop declaration and execution
can be done in following ways.

➢ Check condition to start a loop

➢ Initialize loop with declaring a variable.

➢ Executing statements inside loop.

➢ Increment or decrement of value of a variable.


Types of looping statements: Basically, the type of looping statements depends on the condition
checking mode. Condition checking can be made in two ways as: Before loop and after loop. So,
there are 2(two) types of looping statements.
• Entry controlled loop
• Exit controlled loop
Entry controlled loop: In such type of loop, the test condition is checked first before the loop is
executed.
Some common examples of these looping statements are:

➢ while loop
➢ for loop
Exit controlled loop: In such type of loop, the loop is executed first. Then condition is checked
after block of statements are executed. The loop executed atleast one time compulsorily.
Some common example of this looping statement is:

➢ do-while loop
while Loop: This is an entry controlled looping statement. It is used to repeat a block of
statements until condition becomes true.
Syntax:
while(condition)
{
statements; increment/decrement;
}
In above syntax, the condition is checked first. If it is true, then the program control flow goes
inside the loop and executes the block of statements associated with it. At the end of loop
increment or decrement is done to change in variable value. This process continues until test
condition satisfies.
do
{
printf("Enter a number\n");
scanf("%d",&num);
sum+=num;
} while(num!=0);
printf("sum=%d",sum);
getch();
}

OUTPUT :
Enter a number 3
Enter a number 4
Enter a number 0
Sum = 7
 The case labels are not necessary to write in ascending order.
➢ The case labels must be unique.
➢ The block may contain zero (or) one (or) more statements.
➢ The block contains more than one statement there is no necessary of using braces.
➢ This expression or case labels are only of integer or character data type
➢ It is similar to if…..else statement (or) else …. If ladder.
➢ The default statement is not used and there is no match is found the control transfer to the
next statement of the program and there is no output.
Example: Write a program to perform all arithmetic operations in c. OR Program to create a
simple calculator
# include<stdio.h>
void main()
{
char op;
int a,b;
printf("Enter an operator (+, -, *, /): \n");
scanf("%c", &op);
printf("Enter two operands: ");
scanf("%d %d",&a, &b);
switch(op)
{
case '+': printf(“Sum=%d”, a+b);
break;
case '-': printf(“Difference=%d”, a-b);
break;
case '*': printf(“Product=%d”, a*b);
break; case '/': printf(“Quotient=%d”, a/b);
break;
case '%': printf(“Remainder=%d”, a%b);
break;
default: printf("Operator is not correct");
}
}

Output: Enter an operator (+, -, *,):


Enter two operands: 32 12
Difference=20

Jumping statements (Un-Conditional statements)


Jumping statements are used to transfer the program’s control from one location to another,
these are set of keywords which are responsible to transfer program’s control within the same
block or from one function to another.
There are 3 jumping statements in C language:
1. break statement
2. continue statement
3. goto statement
1. break statement:
The break statement has the following two usages –
 When a break statement is encountered inside a loop, the loop is immediately terminated and
the program control resumes at the next statement following the loop.
 It can be used to terminate a case in the switch statement
If you are using nested loops, the break statement will stop the execution of the innermost loop
and start executing the next line of code after the block.
Syntax:
:break;

Example :
#include<stdio.h>
void main ()
{
int i,a ;
for(i=1;i<=10;i++)
{
if(i==5)
{
break;
}
printf("value of a: %d\n", a);
}
}
Output:
value of a: 1
value of a: 2
value of a: 3
value of a: 4
2. continue statement:
The continue statement used to bring the program control to the beginning of the loop. The
continue statement skips some lines of code inside the loop and continues with the next iteration.
For the for loop, continue statement causes the conditional test and update portions of the loop
to execute. For the while and do...while loops, continue statement causes the program control to
pass to the conditional tests.
Syntax
continue;
Example
#include <stdio.h>
void main ()
{
int i,a ;
for(i=1;i<=10;i++)
{
if(i==5)
{
continue;
}
printf("value of a: %d\n", a);
}
}
Output:
value of a: 1
value of a: 2
value of a: 3
value of a: 4
value of a: 6
value of a: 7
value of a: 8
value of a: 9
value of a: 10
3. goto statement:
A goto statement provides an unconditional jump from the 'goto' to a labeled statement in the
same function.
Syntax:
goto label;
… label: statement;
Here label can be any plain text except C keyword and it can be set anywhere in the C program
above or below goto statement.

Example
#include<stdio.h>
int main ()
{
int a = 11;
LABEL1:do
{
if( a == 15)
{
a = a + 1;
goto LABLE1;
}
printf("value of a: %d\n", a);
a++;
}while( a < 20 );
}
Output:
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 16
value of a: 17
value of a: 18
value of a: 19

You might also like