Chapter 6 Loops in Program
Chapter 6 Loops in Program
Chapter 6 Loops in Program
Loops in program
to
execute
part
of
the
program
repeatedly, either for a predefined number of times or until some condition becomes true. In such cases we can use the loop control constructs like for, do - while, or while. Syntax of all three loop control constructs are given below and the use of all of them are illustrated with some examples. 6.2 for syntax: for initiali!ation expression" condition test expression" modification expression# $ %& body of the for loop &% ' (ll three expressions are optional. If the body of the loop contains only one statement, the braces are also optional. )or loops may also be nested. *isting +.,.- illustrates the use of for loop for reading a string having embedded blanks. %& program to illustrate use of for loop &% . include /stdio.h0 int main void# $ int index" char name1234, ch" printf 5enter a string with embedded blanks: 5#" printf 5reading will be terminated with 67 in first column : 5#" ch 8 getchar #" for index 8 3" ch 98 :;)" index<<# $ name1index4 8 ch" ch 8 getchar #" ' name1index4 8 =>3=" printf 5String read is>n?s>n5, name#" return 3" '
@ gcc *isting+A,A-.c @ .%a.out enter a string with embedded blanks: reading will be terminated with 67 in first column : Beritage Institute of Cechnology Dolkata 67 String read is Beritage Institute of Cechnology Dolkata @ Listing 6.2.1 program using for loop for reading a string
Che initiali!ation expression may use comma operator. Che update expression may also use comma operator. Chus, effectively, multiple initiali!ation and multiple updating is possible. Ee illustrate the use of comma operator for reversing the content of a string.
%& program to illustrate multiple initiali!ation and updatation &% . include /string.h0 . include /stdio.h0 int main void# $ int i, F, n" char ch, name1234" printf 5enter a string: to be terminated by 67 in first column>n5#" ch 8 getchar #" for i83" ch 98 :;)" i<<# $ if ch 98 =>n=# name1i4 8 ch" else i--" %& this discards the new line charcter &% ch 8 getchar #" ' name1i4 8 =>3=" %& string reading is complete, now reverse it &% printf 5Che original string is:>n?s>n5, name#" n 8 strlen name#"
for i83, F8 n--" i / F" i<<, F--# $ ch8 name1i4" name1i4 8 name1F4" name1F4 8 ch" ' printf 5Che reversed string is:>n?s>n5, name#" return 3" ' @ gcc *isting+A,A,.c @ .%a.out enter a string: to be terminated by 67 in first column Gengal :ngineering Hollege Che original string is: Gengal :ngineering Hollege Che reversed string is: egelloH gnireenign: lagneG @ .%a.out enter a string: to be terminated by 67 in first column madam Che original string is: madam Che reversed string is: madam @ Listing 6.2.2 6.3 While loop syntax: while expression# $ %& body of the loop ' Program using for loop for string reversal
&%
Che expression is evaluated first , if value of the expression is true, i.e., non-!ero, then the body of the loop will be executed" if value of the expression is false, control goes to the statement following the body of the loop. If initial value of the expression is false, body of the loop will not be executed at all.
%& program using while loop &% %& this program reads a string character by character until it finds new line &% . include /stdio.h0 int main void# $ char name1234, ch" int i, F, n" printf 5enter a string, blanks are permitted: >n5#" i83" F83" ch 8 getchar #" while ch 98 =>n=# $ name1i4 8 ch" i<<" if ch 88 = =# F<<" ch8getchar #" ' name1i4 8 =>3=" printf 5Io. of characters read 8 ?3Jd>n5, i#" printf 5Io of blanks read 8 ?3Jd>n5, F#" printf 5?s>n5, name#" return 3" ' @ gcc *isting+AJA-.c @ .%a.out enter a string, blanks are permitted: Gengal :ngineering Hollege Io. of characters read 8 3,K Io of blanks read 8 33L Gengal :ngineering Hollege @ .%a.out enter a string, blanks are permitted: Gengal:ngineeringHollege Io. of characters read 8 3,L Io of blanks read 8 333 Gengal:ngineeringHollege @ Listing 6.3.1 Program using while
Iote: It may please be noted that a IM** character is to be inserted at the end of a character array to make it a string. If the null character is not inserted, it is not a string.
6.4
do
while
syntax: do $ %& expressions &% ' while expression#" Che block of statements is first executed and then the
expression is evaluated. If the expression is true, the program enters into the block again. Chis process continues as long as the expression is true. Che main difference between while and do while is that, in do while the block is executed at least once, where as in while, the block may not be executed at all. (lso note that there is a semi colon after the expression in do while. %& program using do while loop &% %& this program reads a string character by character until it finds new line &% . include /stdio.h0 int main void# $ char name1234, ch" int i, F, n" printf 5enter a string, blanks are permitted: >n5#" i83" F83" do $ ch 8 getchar #" name1i4 8 ch" i<<" if ch 88 = =# F<<" 'while ch 98 =>n=#" name1i4 8 =>3=" printf 5Io. of characters read 8 ?3Jd>n5, i#" printf 5Io of blanks read 8 ?3Jd>n5, F#" printf 5?s>n5, name#" return 3" '
@ gcc *isting+ANA-.c @ .%a.out enter a string, blanks are permitted: Gengal :ngineering Hollege Io. of characters read 8 3J3 Io of blanks read 8 33L Gengal :ngineering Hollege @ .%a.out enter a string, blanks are permitted: Gengal:ngineeringHollege Io. of characters read 8 3,L Io of blanks read 8 333 Gengal:ngineeringHollege @ Listing 6.4.1 Program using do while loop
elements of this array from the key board. Che reading should terminate either if 23 characters are read or :;) of file condition occurs. Iote that end of file is sensed if you enter 67 at the beginning of a line. Oeplace the last P>3Q and print the content with the command Rrintf S?s>nT, string name#" ;bserve the result if IM** character is not inserted in the array. character by
+., it
Erite a program to read the contents of a file and display on the screen. Mse file handling by file redirection, i.e.,
program should read from standard input file and during execution we use redirection as .%a.out / file-
+.J
string is a palindrome or not. ( palindrome is a string which appears to be same if we read it in forward direction or backward direction. +.N Erite a program to read a string having embedded blanks and generate a new string after removing all the blanks.
Uo to Hontents