control structuresin c
TRANSCRIPT
ConditionOperator Meaning Example
== Equal to count == 10
!= Not equal to flag != DONE
< Less than a < b
<= Less than or equal to <= LIMIT
> Greater than pointer > end_of_list
>= Greater than or equal to lap >= start
Dept. of CS&E and IT, DBCET,
Guwahati2
Control Structures in C� These include
� if else,
� while,
� do-while,
� for, and a selection statement called
Dept. of CS&E and IT, DBCET, Guwahati3
� for, and a selection statement called
� switch.
if-else
� The if-else statement can exist in two forms: with or without the else. The two forms are:
if(expression)statement
Dept. of CS&E and IT, DBCET, Guwahati4
statement
� orif(expression)statement1
elsestatement2
if-else� If
if (condition)
statement1;
else
statement2;
Dept. of CS&E and IT, DBCET, Guwahati5
int a, b;
// ...
if(a < b) a = 0;
else b = 0;
Nested ifNested ifNested ifNested if
if (i == 10) {
if (j < 20) a = b;
if (k > 100) c = d; // this if is
else a = c; // associated with this else
}
Dept. of CS&E and IT, DBCET, Guwahati6
}
else a = d; // this else refers to if(i == 10)
ifififif----elseelseelseelse----if Ladderif Ladderif Ladderif Ladder
if(condition)statement;
else if(condition)statement;
else if(condition)statement;
Dept. of CS&E and IT, DBCET, Guwahati7
statement;...elsestatement;
ifififif----elseelseelseelse----if Ladderif Ladderif Ladderif Ladder// Demonstrate if-else-if statements (IfElse.c).#include <stdio.h>main () {int no; printf(“\nEnter any number from 1 to 12 to know the month”);scanf(“%d”,&no);
Dept. of CS&E and IT, DBCET, Guwahati8
scanf(“%d”,&no);
If (no==1)
printf(“\n Month is January”);
else if (no==2)
printf(“\n Month is February”);
else if (no==3)
printf(“\n Month is March”);
else if (no==4)
printf(“\n Month is April”);
else if (no==5)
printf(“\n Month is May”);
else if (no==6)
printf(“\n Month is June”);
else if (no==7)
printf(“\n Month is July”);
Dept. of CS&E and IT, DBCET, Guwahati9
printf(“\n Month is July”);
else if (no==8)
printf(“\n Month is August”);
else if (no==9)
printf(“\n Month is September”);
else if (no==10)
printf(“\n Month is October”);
else if (no==11)
printf(“\n Month is November”);
else if (no==12)
printf(“\n Month is December”);
else printf(“ \n Invalid Entry”);
}
switchswitchswitchswitch
The control statement that allows us to make a decision
from the number of choices is called a switch, or more
correctly a switch-case-default, since these three
keywords go together to make up the control statement.
Dept. of CS&E and IT, DBCET, Guwahati10
keywords go together to make up the control statement.
They most often appear as follows:
switchswitchswitchswitch
switch ( integer expression) {case value1:
// statement sequencebreak;
case value2:// statement sequencebreak;
Dept. of CS&E and IT, DBCET, Guwahati11
break;...case valueN:
// statement sequencebreak;
default:// default statement sequence
}
switch� The integer expression following the keyword switch is anyC expression that will yield an integer value.
� It could be an integer constant like 1, 2 or 3, or anexpression that evaluates to integer.
� The keyword case is followed by an integer or a character
Dept. of CS&E and IT, DBCET, Guwahati12
� The keyword case is followed by an integer or a characterconstant.
� Each constant in each case must be different from all theothers.
switchswitchswitchswitch// A simple example of the switch(switch.c)#include <stdio.h>main() {int i;for(i=0; i<6; i++)switch(i) {
case 0:printf("i is zero.\n");
break;case 1:
printf("i is one.\n");break;
Dept. of CS&E and IT, DBCET, Guwahati13
break;case 2:
printf("i is two.\n");break;
case 3:printf("i is three.\n");break;
default:printf("i is greater than 3.\n");
} // switch} // main
main()
{
char c = 'x' ;
switch ( c )
{
case 'v' :
printf ( "I am in case v \n" ) ;
break ;
case 'a' :
printf ( "I am in case a \n" ) ;
Dept. of CS&E and IT, DBCET, Guwahati14
printf ( "I am in case a \n" ) ;
break ;
case 'x' :
printf ( "I am in case x \n" ) ;
break ;
default :
printf ( "I am in default \n" ) ;
}
}
Nested switchNested switchNested switchNested switch
switch(count) {case 1:switch(target) { // nested switchcase 0:
printf("target is zero");
Dept. of CS&E and IT, DBCET, Guwahati15
printf("target is zero");break;
case 1: // no conflicts with outer switchprintf("target is one");break;
} // switch(target)break;case 2: // ...
switch Versus ifswitch Versus ifswitch Versus ifswitch Versus if----else Ladder else Ladder else Ladder else Ladder
� There are some things that we simply cannot dowith a switch. These are:�A float expression cannot be tested using aswitch
Dept. of CS&E and IT, DBCET, Guwahati16
switch
�Cases can never have variable expressions (forexample it is wrong to say case a +3 :
�Multiple cases cannot use same expressions.
Loop structure� The repetitive operation in a program is done through loop-
� They are: � (a) Using a for statement
� (b) Using a while statement
� (c) Using a do-while statement
Dept. of CS&E and IT, DBCET, Guwahati17
� (c) Using a do-while statement
for loopfor loopfor loopfor loop� for loop allows us to specify the following three things in a single line-
� Setting a loop counter to an initial value.
� Testing the loop counter to determine whether its value has reached the number of iterations desired.
Dept. of CS&E and IT, DBCET, Guwahati18
� Increasing the value of loop counter each time the program segment within the loop has been executed.
� General Form:for(initialization; condition; iteration){// body
}
for// Using the comma (comma.c)#include <stdio.h>main() {
int a, b;for(a=1, b=4; a<b; a++, b--) {printf("a = %d \n", a);
Dept. of CS&E and IT, DBCET, Guwahati19
printf("a = %d \n", a);printf("b = %d \n", b);
}}
for// Demonstrate the for loop (loop.c).
#include <stdio.h>
main() {
int n;
for(n=10; n>0; n--)
Dept. of CS&E and IT, DBCET, Guwahati20
for(n=10; n>0; n--)
printf("tick %d \n",n);
}
While loopWhile loopWhile loopWhile loop� While loop� The general form of while is as shown below:
initialize loop counterwhile(test loop counter using a condition) {statement block;increment/decrement loop counter;
Dept. of CS&E and IT, DBCET, Guwahati21
increment/decrement loop counter;}
� The statements within the while loop would keep on getting executed till the condition being tested remains true.
while// Demonstrate the while loop (while.c).#include <stdio.h>main() {
int n = 10;while(n > 0) {
printf("tick %d \n",n);
Dept. of CS&E and IT, DBCET, Guwahati23
printf("tick %d \n",n);n--;
} // while} // main
do-while loop The general form of do-while
initialize loop counter
do
{ statement block;
Dept. of CS&E and IT, DBCET, Guwahati24
statement block;increment/decrement loop counter;
} while(test loop counter using a condition);
dodododo----while loopwhile loopwhile loopwhile loop// Demonstrate the do-while loop (dowhile.c).#include <stdio.h>main() {
int n = 10;do {
printf("tick %d \n",n);
Dept. of CS&E and IT, DBCET, Guwahati25
printf("tick %d \n",n);n--;
} while(n > 0);} // main
While Vs do-while loops� There is a minor difference between the working ofwhile and do-while loops.
� This difference is the place where the condition is tested. � The while tests the condition before executing any of the statements within the while loop.
Dept. of CS&E and IT, DBCET, Guwahati26
statements within the while loop. � The do-while tests the condition after having executed the statements within the loop.
� Statements under do-while loop will get executed atleast once and not so incase of while loop
Nested LoopsNested LoopsNested LoopsNested Loops� Like all other programming languages, C allows loops to be nested. That is, one
loop may be inside another. For example, here is a program that nests for loops:
// Loops may be nested (nestedfor.c).#include <stdio.h>main() {int i, j;for(i=0; i<10; i++) {
Dept. of CS&E and IT, DBCET, Guwahati27
for(i=0; i<10; i++) {for(j=i; j<10; j++)
printf(".");printf("\n");
}}
JumpJumpJumpJump
� C supports four jump statements: � break,
� continue,
� return
Dept. of CS&E and IT, DBCET, Guwahati28
� goto.
� These statements transfer control to another part of your program.
breakbreakbreakbreak
�In C, the break statement has two uses. �First, it terminates a statement sequence in a switch statement.
�Second, it can be used to exit a loop.
Dept. of CS&E and IT, DBCET, Guwahati29
�Second, it can be used to exit a loop.
break statement� We often come across situations where we want to jump out of a loop instantly, without waiting to get back to the conditional test.
� The keyword break allows us to do this.
� When break is encountered inside any loop, control
Dept. of CS&E and IT, DBCET, Guwahati30
� When break is encountered inside any loop, controlautomatically passes to the first statement after the loop.
break
// Using break to exit a loop (break.c).
#include <stdio.h>
main() {
int i;
for(i=0; i<100; i++) {
Dept. of CS&E and IT, DBCET, Guwahati31
for(i=0; i<100; i++) {
if(i == 10) break; // terminate loop if i is 10
printf("i: %d \n", i);
}
printf("Loop complete.");
}
break// Using break to exit a while loop (break2.c).#include <stdio.h>
main() {int i = 0;while(i < 100) {if(i == 10) break; // terminate loop if i is 10printf("i: %d \n", i);
Dept. of CS&E and IT, DBCET, Guwahati32
printf("i: %d \n", i);i++;
}printf("Loop complete.");
}
break statement example
main( ) { int i = 1 , j = 1 ;
while ( i++ <= 100 )
{
while ( j++ <= 200 )
{ In this program when j equals 150,
break takes the control outside
Dept. of CS&E and IT, DBCET, Guwahati33
if ( j == 150 )
break ;
else
printf ( "%d %d\n", i, j ) ;
}
}
}
break takes the control outside
the inner while only, since it is
placed inside the inner while.
continuecontinuecontinuecontinue� continue go immediately to next iteration of loop� In while and do-while loops, a continue statement causes control to be transferred directly to the conditional expression that controls the loop.
� In a for loop, control goes first to the iteration portion of the for statement and then to the conditional expression.
Dept. of CS&E and IT, DBCET, Guwahati34
In a for loop, control goes first to the iteration portion of the for statement and then to the conditional expression.
continue// Demonstrate continue (continue.c).#include <stdio.h>main() {
int i;for(i=0; i<10; i++) {
printf("%d ", i);
Dept. of CS&E and IT, DBCET, Guwahati35
printf("%d ", i);if (i%2 == 0) continue;printf("\n");
}}
returnreturnreturnreturn� The return statement is used to explicitly return from a method. That is, it causes program control to transfer back to the caller of the method.
� The following example illustrates this point. Here, return causes execution to return to the C, since it is the
Dept. of CS&E and IT, DBCET, Guwahati36
return causes execution to return to the C, since it is the run-time system that calls main( ).
return// Demonstrate return (return.c).
#include <stdio.h>
main() {
int t = 1;
printf("Before the return.");
Dept. of CS&E and IT, DBCET, Guwahati37
printf("Before the return.");
if(t==1) return; // return to caller
printf("This won't execute.");
}
goto� It is possible to jump to any statement within the same function using goto.
� A label is used to mark the destination of the jump.
goto label1;
Dept. of CS&E and IT, DBCET, Guwahati38
::label1: