You are on page 1of 11

Birla Institute of Technology & Science, Pilani

Second Semester 2015-2016, Computer Programming [CS F111]


Week #5
Tutorial Sheet #1 Date:___/_____/________
Name: ____________________________ID: ______________________ Lec Sec/Lab
Sec:______/_____

Section 1 For loop:


We have seen how while loops work. You must have noticed a while loop runs as
long as the loop condition is met. In many case, you know precisely how many
time the loop should run and execute a block of statements that many times. In
such cases we use another kind loop called for loop.
The syntax of the for loop is:
for (initialization; terminating condition; increment/decrement){
statements;
}

There are three parts to for loop separated by semicolon within parenthesis.
The initialization step is executed first, and only once. Here you declare and
initialize any loop control variables. But having said that note that it is not a
mandatory section and hence one can skip it but the semicolon at the end of it is a
must.
Next, the condition is evaluated. If it is true, the body of the loop is executed. If it
is false, the body of the loop does not execute and the flow of control jumps to the
next statement just after the 'for' loop. Note that the condition ends with a
semicolon.
After the body of the 'for' loop executes, the flow of control jumps back up to
the increment/decrement statement. This statement allows you to update the loop
control variables. This statement can be left blank, as long as a semicolon appears
after the condition.

The condition is now evaluated again. If it is true, the loop executes and the process
repeats itself (body of loop, then increment step, and then again condition). After
the condition becomes false, the 'for' loop terminates.
The flowchart of for loop is

Example 1: Write a program to print integers starting from -5 to 5


using for loop
#include <stdio.h>
/* Program to print integers between -5 and 5 */
int main(){
int i;
for(int i = -5; i <= 5;i++){
printf(%d, i);
}
return 0;
}
Exercise 1: Print all the positive integers starting from 0 to a user
provided positive integer N.

Example 2 Write the flow chart to print the following pattern


*******
*******
*******
*******
*******
#include <stdio.h>
/* Program to print patterns*/
int main(){
int i;
for(i = 0; i < 5; i++){
printf("******\n");
}
return 0;
}
Exercise 2 Can you write the above loop using decrement operator
of the loop variable i?

Exercise 3. Write a for loop to compute the following summation


S = 1 + 3 + 5 + 7 + + (2n-1)

Exercise 4: Remember the program for two players game done last week.
Would use a for loop in that scenario. Justify your answer.

Exercise 5: Write a C code to print the multiplication table of a given


number using for loop.

Exercise 6 Trace the following loop


Statements

Variable values
i = 2456
sum = 0

for(i = 100;i > 95;i--){


i =
sum =
sum = sum + i;
i =
sum =
}
i =
sum =

Section 3 do-while loop


This loop is different from the two loops you have learnt i.e. for loop and while
loop. Now recall the exercise we have done on the two player game. In one of the
approaches, one will ask the second player to enter his/her choice before the loop
only once. Then depending upon whether there is match between the choice of
first player and second player, while loop will run. Interestingly, notice that we
have executed the content of the while loop once outside and then inside the while
loop repeatedly. A dowhile loop is the loop construct which allows todo similar
tasks easily.
Unlike for and while loops, which test the loop condition at the top of the loop,
the do...while loop in C programming checks its condition at the bottom of the
loop. A do...while loop is similar to a while loop, except the fact that it is
guaranteed to execute at least one time.
Syntax of do while loop is as follows:
do {
statements;
} while(condition);

The flowchart is given as follows:

A word of caution. Dont miss the semicolon after the while(condition)


at the end of loop.
So lets write the code for the two player game using do..while loop
Example 3: Implement the two player game using do while loop.
#include <stdlib.h>
/* Program to implement the guess game between two players */
int main()
{
int p1,p2,count=0;
printf(" Player1 please enter a value between 1-10 ");
scanf("%d",&p1); //read value provided by player1
//compare the values entered by p1 and p2
do{
printf(" Player2 please enter a value between 1-10 to guess Player1's
value ");
scanf("%d",&p2); //if the attempt fails, player 2 enters another value
count++; //increment the number of attempts
}while(p1 != p2);
//print finally the number of attempts taken by player2
printf(" Player2 have taken %d attempts", count);

return 0;
}

Note that we have to initialize the variable the count differently.

Exercise 7: Write a program to print the digits of an unsigned integer from


right to left on each line using a do-while loop.

Exericse 8: It is difficult to remember ASCII value corresponding to each


character. Write a program to take a character as input and print the ASCII
value corresponding to it. The program should continue until user says no
(i.e. enters N). Write this program using three different flavors:
a) using do while loop. Emphasis that do while loop is the best
option among the three loops.
b) Convert the above program using only while loop. Emphasis is on
converting a do while loop into a while/for loop.

Exercise 9: Write a program to do the following: Read in a series of


numbers. Print each number as you read in. When the sum of numbers
becomes 100 or more, print a message that a total of 100 has been
reached and stop processing. However, if you read in a number 17 before
reaching 100, print a message giving the sum so far and terminate the
program at that point.
Write this program using while loop and emphasize that while loop is the
best option here. However, then convert the same program using for and
do while loop.

You might also like