SlideShare a Scribd company logo
1 of 39
Download to read offline
Control Structures in C
Dept. of CS&E and IT, DBCET, Guwahati1
Condition
Operator 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,
Guwahati
2
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
or
if(expression)
statement1
else
statement2
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;
.
.
.
else
statement;
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 sequence
break;
case value2:
// statement sequence
break;
Dept. of CS&E and IT, DBCET, Guwahati11
break;
.
.
.
case valueN:
// statement sequence
break;
default:
// default statement sequence
}
switch
The integer expression following the keyword switch is any
C expression that will yield an integer value.
It could be an integer constant like 1, 2 or 3, or an
expression 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 character
constant.
Each constant in each case must be different from all the
others.
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 switch
case 0:
printf("target is zero");
Dept. of CS&E and IT, DBCET, Guwahati15
printf("target is zero");
break;
case 1: // no conflicts with outer switch
printf("target is one");
break;
} // switch(target)
break;
case 2: // ...
switch Versus ifswitch Versus ifswitch Versus ifswitch Versus if----else Ladderelse Ladderelse Ladderelse Ladder
There are some things that we simply cannot do
with a switch. These are:
A float expression cannot be tested using a
switch
Dept. of CS&E and IT, DBCET, Guwahati16
switch
Cases can never have variable expressions (for
example 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 counter
while(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 loop
Dept. of CS&E and IT, DBCET, Guwahati22
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 of
while 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, control
automatically 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 10
printf("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 %dn", 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:
goto
// Using continue with a label (goto.c).
#include <stdio.h>
main() {
int i,j;
for (i=0; i<10; i++) {
for(j=0; j<10; j++) {
if(j > i) {
printf("n");
Dept. of CS&E and IT, DBCET, Guwahati39
printf("n");
goto outer;
}
printf(" %d", (i * j));
}
outer: printf(".. outer ..n");
}
}

More Related Content

What's hot

C Language - Switch and For Loop
C Language - Switch and For LoopC Language - Switch and For Loop
C Language - Switch and For LoopSukrit Gupta
 
6 c control statements branching &amp; jumping
6 c control statements branching &amp; jumping6 c control statements branching &amp; jumping
6 c control statements branching &amp; jumpingMomenMostafa
 
conditional statements
conditional statementsconditional statements
conditional statementsJames Brotsos
 
If statements in c programming
If statements in c programmingIf statements in c programming
If statements in c programmingArchana Gopinath
 
Programming basics
Programming basicsProgramming basics
Programming basics246paa
 
Kamil witecki asynchronous, yet readable, code
Kamil witecki asynchronous, yet readable, codeKamil witecki asynchronous, yet readable, code
Kamil witecki asynchronous, yet readable, codeKamil Witecki
 
Simple if else statement,nesting of if else statement &amp; else if ladder
Simple if else statement,nesting of if else statement &amp; else if ladderSimple if else statement,nesting of if else statement &amp; else if ladder
Simple if else statement,nesting of if else statement &amp; else if ladderMoni Adhikary
 
1 introducing c language
1  introducing c language1  introducing c language
1 introducing c languageMomenMostafa
 
Looping Statement And Flow Chart
 Looping Statement And Flow Chart Looping Statement And Flow Chart
Looping Statement And Flow ChartRahul Sahu
 
Conditional and control statement
Conditional and control statementConditional and control statement
Conditional and control statementnarmadhakin
 
Python Unit 3 - Control Flow and Functions
Python Unit 3 - Control Flow and FunctionsPython Unit 3 - Control Flow and Functions
Python Unit 3 - Control Flow and FunctionsDhivyaSubramaniyam
 

What's hot (20)

C Language - Switch and For Loop
C Language - Switch and For LoopC Language - Switch and For Loop
C Language - Switch and For Loop
 
6 c control statements branching &amp; jumping
6 c control statements branching &amp; jumping6 c control statements branching &amp; jumping
6 c control statements branching &amp; jumping
 
conditional statements
conditional statementsconditional statements
conditional statements
 
Session 3
Session 3Session 3
Session 3
 
If statements in c programming
If statements in c programmingIf statements in c programming
If statements in c programming
 
Lecture 10 - Control Structures 2
Lecture 10 - Control Structures 2Lecture 10 - Control Structures 2
Lecture 10 - Control Structures 2
 
7 functions
7  functions7  functions
7 functions
 
L05if
L05ifL05if
L05if
 
Lecture 12 - Recursion
Lecture 12 - Recursion Lecture 12 - Recursion
Lecture 12 - Recursion
 
Programming basics
Programming basicsProgramming basics
Programming basics
 
Kamil witecki asynchronous, yet readable, code
Kamil witecki asynchronous, yet readable, codeKamil witecki asynchronous, yet readable, code
Kamil witecki asynchronous, yet readable, code
 
C programming slide c04
C programming slide c04C programming slide c04
C programming slide c04
 
Simple if else statement,nesting of if else statement &amp; else if ladder
Simple if else statement,nesting of if else statement &amp; else if ladderSimple if else statement,nesting of if else statement &amp; else if ladder
Simple if else statement,nesting of if else statement &amp; else if ladder
 
Bsit1
Bsit1Bsit1
Bsit1
 
1 introducing c language
1  introducing c language1  introducing c language
1 introducing c language
 
Lecture 9- Control Structures 1
Lecture 9- Control Structures 1Lecture 9- Control Structures 1
Lecture 9- Control Structures 1
 
Looping Statement And Flow Chart
 Looping Statement And Flow Chart Looping Statement And Flow Chart
Looping Statement And Flow Chart
 
control statement
control statement control statement
control statement
 
Conditional and control statement
Conditional and control statementConditional and control statement
Conditional and control statement
 
Python Unit 3 - Control Flow and Functions
Python Unit 3 - Control Flow and FunctionsPython Unit 3 - Control Flow and Functions
Python Unit 3 - Control Flow and Functions
 

Similar to Control structuresin c (20)

Fundamental of Information Technology - UNIT 8
Fundamental of Information Technology - UNIT 8Fundamental of Information Technology - UNIT 8
Fundamental of Information Technology - UNIT 8
 
Control Structures in C
Control Structures in CControl Structures in C
Control Structures in C
 
CONTROLSTRUCTURES.ppt
CONTROLSTRUCTURES.pptCONTROLSTRUCTURES.ppt
CONTROLSTRUCTURES.ppt
 
C Programming: Control Structure
C Programming: Control StructureC Programming: Control Structure
C Programming: Control Structure
 
CONTROL FLOW in C.pptx
CONTROL FLOW in C.pptxCONTROL FLOW in C.pptx
CONTROL FLOW in C.pptx
 
computer programming Control Statements.pptx
computer programming Control Statements.pptxcomputer programming Control Statements.pptx
computer programming Control Statements.pptx
 
COM1407: Program Control Structures – Repetition and Loops
COM1407: Program Control Structures – Repetition and Loops COM1407: Program Control Structures – Repetition and Loops
COM1407: Program Control Structures – Repetition and Loops
 
Elements of programming
Elements of programmingElements of programming
Elements of programming
 
Loops in c
Loops in cLoops in c
Loops in c
 
Control structure of c
Control structure of cControl structure of c
Control structure of c
 
Control structures in c
Control structures in cControl structures in c
Control structures in c
 
Ch3 repetition
Ch3 repetitionCh3 repetition
Ch3 repetition
 
Control statements-Computer programming
Control statements-Computer programmingControl statements-Computer programming
Control statements-Computer programming
 
C.pdf
C.pdfC.pdf
C.pdf
 
Loops
LoopsLoops
Loops
 
Flow of control ppt
Flow of control pptFlow of control ppt
Flow of control ppt
 
3. control statement
3. control statement3. control statement
3. control statement
 
C Programming Unit-2
C Programming Unit-2C Programming Unit-2
C Programming Unit-2
 
Decision Making and Branching
Decision Making and BranchingDecision Making and Branching
Decision Making and Branching
 
What is c
What is cWhat is c
What is c
 

More from Vikash Dhal

Preprocessor directives
Preprocessor directivesPreprocessor directives
Preprocessor directivesVikash Dhal
 
Implementation of strassens
Implementation of  strassensImplementation of  strassens
Implementation of strassensVikash Dhal
 
Packet switching
Packet switchingPacket switching
Packet switchingVikash Dhal
 
Strinng Classes in c++
Strinng Classes in c++Strinng Classes in c++
Strinng Classes in c++Vikash Dhal
 
File handling in c
File handling in c File handling in c
File handling in c Vikash Dhal
 

More from Vikash Dhal (7)

C fundamentals
C fundamentalsC fundamentals
C fundamentals
 
Preprocessor directives
Preprocessor directivesPreprocessor directives
Preprocessor directives
 
Implementation of strassens
Implementation of  strassensImplementation of  strassens
Implementation of strassens
 
Packet switching
Packet switchingPacket switching
Packet switching
 
Raid
RaidRaid
Raid
 
Strinng Classes in c++
Strinng Classes in c++Strinng Classes in c++
Strinng Classes in c++
 
File handling in c
File handling in c File handling in c
File handling in c
 

Control structuresin c

  • 1. Control Structures in C Dept. of CS&E and IT, DBCET, Guwahati1
  • 2. Condition Operator 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, Guwahati 2
  • 3. 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.
  • 4. 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 or if(expression) statement1 else statement2
  • 5. 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;
  • 6. 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)
  • 7. 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; . . . else statement;
  • 8. 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);
  • 9. 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”); }
  • 10. 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:
  • 11. switchswitchswitchswitch switch ( integer expression) { case value1: // statement sequence break; case value2: // statement sequence break; Dept. of CS&E and IT, DBCET, Guwahati11 break; . . . case valueN: // statement sequence break; default: // default statement sequence }
  • 12. switch The integer expression following the keyword switch is any C expression that will yield an integer value. It could be an integer constant like 1, 2 or 3, or an expression 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 character constant. Each constant in each case must be different from all the others.
  • 13. 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
  • 14. 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" ) ; } }
  • 15. Nested switchNested switchNested switchNested switch switch(count) { case 1: switch(target) { // nested switch case 0: printf("target is zero"); Dept. of CS&E and IT, DBCET, Guwahati15 printf("target is zero"); break; case 1: // no conflicts with outer switch printf("target is one"); break; } // switch(target) break; case 2: // ...
  • 16. switch Versus ifswitch Versus ifswitch Versus ifswitch Versus if----else Ladderelse Ladderelse Ladderelse Ladder There are some things that we simply cannot do with a switch. These are: A float expression cannot be tested using a switch Dept. of CS&E and IT, DBCET, Guwahati16 switch Cases can never have variable expressions (for example it is wrong to say case a +3 : Multiple cases cannot use same expressions.
  • 17. 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
  • 18. 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 }
  • 19. 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); } }
  • 20. 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); }
  • 21. While loopWhile loopWhile loopWhile loop While loop The general form of while is as shown below: initialize loop counter while(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.
  • 22. While loop Dept. of CS&E and IT, DBCET, Guwahati22
  • 23. 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
  • 24. 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);
  • 25. 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
  • 26. While Vs do-while loops There is a minor difference between the working of while 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
  • 27. 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"); } }
  • 28. 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.
  • 29. 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.
  • 30. 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, control automatically passes to the first statement after the loop.
  • 31. 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."); }
  • 32. 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 10 printf("i: %d n", i); Dept. of CS&E and IT, DBCET, Guwahati32 printf("i: %d n", i); i++; } printf("Loop complete."); }
  • 33. 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 %dn", i, j ) ; } } } break takes the control outside the inner while only, since it is placed inside the inner while.
  • 34. 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.
  • 35. 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"); } }
  • 36. 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( ).
  • 37. 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."); }
  • 38. 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:
  • 39. goto // Using continue with a label (goto.c). #include <stdio.h> main() { int i,j; for (i=0; i<10; i++) { for(j=0; j<10; j++) { if(j > i) { printf("n"); Dept. of CS&E and IT, DBCET, Guwahati39 printf("n"); goto outer; } printf(" %d", (i * j)); } outer: printf(".. outer ..n"); } }