Control Statement Part-1

Download as pdf or txt
Download as pdf or txt
You are on page 1of 23

Outline

CONTROL STATEMENT
Introduction

types of control statement

Selection statement : if , switch

Iterative/looping statement: while, do-while, for

Unconditional jump statement: break, continue..

References

1
Introduction

 The statement that control the execution sequence of a program.

 The control statements are classified as:-

 1. selection statement: if, switch

 2. Iterative/ looping statement: while, do-while, for

 3. Unconditional jump statement: break, continue, goto.

2
Selection statement:
 Make a decision to select and execute statements based on the condition.
 If statement: called two way selection statement.
 Syntax:-
simple if if-else
if (condition) if (condition)
{ {
statement_1; statement_1;
…………….. ……………..
} }
else
{
statement_2;
……………..
}
3
Ladder else-if Nested-if

if (condition_1) if (condition_1)
{ {
statement_1; statement_1;
…………….. if(condition_2)
} {
else if(condition_2) statement_2;
{ ……………..
statement_2; }
…………….. else
} {
else if (condition_3) statement_3;
{ ……………..
statement_3; }
…………….. }
} else
else {
{ statement_4;
statement_4; ……………..
…………….. }
4 }
ASCII Value

Char *ASCII Value


A-Z 65-90
a-z 97-122
0-9 47-57
Special symbol 0-47, 58-64, 91-96, 123-127

5 *ASCII: American standard code for information interchange


Switch Statement:
 It is a multi-way switch(var_name or integer_expression)
(n-way) selection {
statement. case constant_1:
statement_1;
 Syntax:- break;
case constant_2:
statement_2;
break;
.
.
default:
statement;
}
6
Simple switch

main() main()
{ {
int i=2; int i=10;
switch(i) switch(i)
{ {
case 1: case 10:
printf(“i am in case 1”); printf(“i am in case 10\n”);
break;
case 2: case 15:
printf(“i am in case 2”); printf(“i am in case 15\n”);
break; break;
default: default:
printf(“i am default\n”); printf(“i am default\n”);
} }
} }

O/P: i am case 2 O/P: i am in case 10


i am in case 15
7 because of missing break after each
case.
example example

main() main()
{ {
int i=10; char ch;
switch(i) printf(“enter any alphabets a,b,c”);
{ scanf(“%c”,&ch);
case 10: switch(ch)
printf(“case 10\n”); {
break; case ‘a’:
case 8+2: case ‘A’:
printf(“case 8+2\n”); printf(“XYZ\n”);
break; break;
default: case ‘b’:
printf(“no matching case\n”); case ‘B’:
} printf(“PQR\n”);
} break;
default:
O/P: error because duplicate case printf(“i am default\n”);
}
} // do’t have any break so
8 control goto case A and execute it.
Syntax of nested switch statement
switch(x)
{
case 1:
switch(y)
{
case 0:
statement;
break;
case 1:
statement;
break;
}
break;
case 2:
statement;
break;
default:
statement;
break;
}
9
Switch Statement:….
 Switch can operate for int & char type data.
 Case needn't necessarily be arrange in ascending order.
 Logical operator can’t be used in case.
 Case 3+7 is correct but a+b is incorrect
 Break statement when used in a switch takes the control outside
the switch.

10
Looping Statements

 Some time there is a situation to execute statements repeatedly for


a number of times or until the condition satisfies.
1. While()
 In this, condition is checked first and if the condition is true the
block of statement will execute & control returns to condition.
 It is called “entry control loop”.

11
Syntax and example:
while(condition) main()
{ {
statement_1; int i= 1;
while(i<=10)
…….
{
statement_n;
printf(“%d”,i);
} i++; //i+ = 1
}
}
O/P:

12
Example:
main()
{
int i= 1;
while(i<=10)
{
printf(“%d”,i);
}
}

o/p: infinite loop


because i remains
equal to 1

13
Example:

14
Example:

O/P: value of i is not getting increment , the control would keep


rotating within the loop.
15
Looping Statements

2. do-while()
 It is called “exit control loop”.
 The statements will execute for at least once, through the condition
will not to be evaluate to execute the block for first time.
Syntax
do
{
statement_1;
…………….
……………..

} while(condition);
16
Example:

#include<stdio.h>
main()
{
int i= 0;
do
{
printf("\n RRSDCE");
i++;
}while(i<5);
}
17
Difference between while & do-while

while() do-while()
test the condition before executing test the condition after having
any of the statement within the executed the statements within the
while loop. loop.
entry control loop exit control loop

18
List of programs:

 Automorphic number.
 Decimal to binary.
 Perfect number.
 Armstrong number.
 Palindrome number.
 Prime number.
 Fibonacci number.
 Factorial of a number.

19
20
Thank You

21
Automorphic number:

 Square of the given number contains the number in the end.

 Example_1: 76  (76)2 = 5776


 Example_2: 6  62 = 36
 Example_3: 5  52 = 25

22
Decimal to binary:

base = 1 binary = 0 no1 = 0

num r base binary 2 15 1


15 - 1 0 2 7 1
7 1 10 1 2 3 1
3 1 100 11 1
1 1 1000 111
15  1111
0 1 10000 1111

23

You might also like