Controlflowstatements 1
Controlflowstatements 1
STATEMENTS
IN C PROGRAMMING LANGUAGE
WHAT IS CONTROL FLOW STATEMENTS
2
TYPES OF CONTROL FLOW STATEMENTS
• Jumping Statements
3
CONTROL FLOW STATEMENTS
• Branching / Decision • Looping / • Jumping Statements
Making Statements Iterative • Break Statement
• Statements • Continue Statement
• Entry Control • Goto Statement
If Statements Loops
• Simple If • While Loop
• If else • For Loop
• Nested if • Exit Control Loop
• else if ladder • Do While Loop
• Switch Case Statement 4
• Conditional Operator
BRANCHING STATEMENTS
5
TYPES OF CONDITIONAL STATEMENTS
• If Statement
• Simple If
• If else Statement
• Nested If Statement
• else If Ladder
void main()
{
int a=5;
if(a%2==0)
{
printf(“number %d is even.”,a);
}
}
8
IF STATEMENT – IF ELSE STATEMENTS
false statements;
} 9
EXAMPLE
void main()
{
int a=5;
if(a%2==0){
printf(“number %d is even”,a);
}
else{
printf(“number %d is odd”,a);
}
} 10
IF STATEMENT – NESTED IF STATEMENTS
11
IF STATEMENT – NESTED IF
STATEMENTS - SYNTAX
if(condition){
if(condition){
true statement;
}
else {
false statement;
}
}
else{
statements;
12
}
EXAMPLE
void main()
{
else{
int a,b,c; if(b>c){
a=5, b=6, printf(“b is big”);
c=8;
}
if(a>b){ else{
if(a>c){
printf(“a is
} big”); printf(“c is big”);
else{ }
} getch();
printf(“c is
big”);
} } 13
IF STATEMENT – ELSE IF LADDER
14
IF STATEMENT – ELSE IF LADDER - SYNTAX
if(condition){
Statement;
}
else if(condition){
Statement;
}
else{
Statement; 15
}
EXAMPLE
switch(expression){
case exp1:
statement;
break;
case exp2:
statement;
break;
default;
statement;
} 18
EXAMPLE
void main()
{
char c=‘a’; default:
switch(c) printf(“character %c is
{
consonant ”,c);
case ‘a’:
case ‘e’: }
case ‘i’: getch();
case ‘o’: }
case ‘u’:
printf(“character %c is
vowel”,c); 19
break;
CONDITIONAL OPERATOR
void main()
{
int a,b,c,d;
a=5, b=6, c=7;
d=(a>b ? ( a>c? a:c):(b>c?b:c));
printf(“greater number is ::%d”,d);
getch();
}
21
ITERATIONS / LOOPING STATEMENTS
void main()
{
int I;
clrscr();
i=1;
while(i<=10){
printf(“%d\t”,i); i+
+;
}
getch();
24
}
ENTRY CONTROL LOOP – FOR LOOP
void main()
{
int i;
clrscr();
i=1;
for(i=1;i<=20; i+=2)
{
printf(“%d\t”,i);
}
getch();
26
}
EXIT CONTROL – DO WHILE LOOP
} while (condition);
EXAMPLE
void main()
{
int I;
clrscr();
i=1;
do
{
printf(“%d\t”,i); i+
+;
}while(i<=5);
getch();
} 28
JUMPING STATEMENTS
• goto
29
BREAK STATEMENT
}
EXAMPLE
void main()
{
int i;
for(i=1;i<=10; i++){
If(i==5){
break;
}
else{
printf(“%d\t”,i);
}
}
getch(); 31
}
CONTINUE STATEMENT
continue;
EXAMPLE
void main()
{
int i;
for(i=1;i<=10; i++){
If(i==5){
continue;
}
else{
printf(“%d\t”,i);
}
}
getch(); 33
}
GOTO STATEMENT
• The goto statement is a jump statement which jumps from one point
to another point within a function or program. The goto statement is
marked by label statement. Label statement can be used anywhere in
the function above or below the goto statement. Simple break
statement cannot work here properly. In this situation, goto
statement is used.
• Syntax:
if(condition){
goto err;
}
err: 34
statements;
EXAMPLE
void main()
{
int i;
for(i=1;i<=10; i++){
If(i==5){
goto err:
}
else{
printf(“%d\t”,i);
}
}
err:
printf(“Error”); 35
}
THANK YOU
36