Control Statement Part-1
Control Statement Part-1
Control Statement Part-1
CONTROL STATEMENT
Introduction
References
1
Introduction
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
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”);
} }
} }
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
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);
}
}
13
Example:
14
Example:
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:
22
Decimal to binary:
23