CS 010 - Introduction to Computer Science I
Lab 9. For and Do While Loops



Points(10 overall)

Collaboration policy:

You will be working in pairs during labs. Pairs will be randomly selected and will be announced by the TA. Each week you will have a new random partner. You will discuss the programs with your partner, but you will still be typing in your own code to show to the TA. You can help each other debug, give plenty of suggestions and hints, **explain** why things work or don't work, etc.


Lab Objectives

To gain experience in


Exercise 1. for loop structure

Write a program that allows the user to enter two integers. Then print out all the values between (and including) these two integers. Do not assume that the user will enter the smallest integer first. For example, if the user enters -7 and 2, then the program would print: -7, -6, -5, -4, -3, -2, -1, 0, 1, 2. If the user enters 2 and then -7, your program would print the same values. You should error check each value that is entered to make sure that they entered the correct type. If the user types in bad input, your program should have them re-enter the input until it makes sense.

Additional requirements:

Helpful functions (provided in the iostream library):


Exercise 2: do while loop structure

The only difference between a while and a do while loop is when the condition is checked. With the while loop the condition is checked before the loop ever executes. With the do while loop, the condition is checked after the loop body is executed. Typically, a do while loop is used when you want to guarantee that the loop will iterate (execute) at least once.

Write a simple 2-player dice game. This game will begin with a roll of 5 dice. Player 1 has a choice of whether to keep this roll or roll all five dice again up to 3 more times. After Player 1's turn is over, the 5 dice are rolled again. Player 2 then has a choice of whether to keep this roll or roll all five dice again up to 3 more times. The player with the highest 5-dice total in their last roll is the winner.

Example game play:

Player 1 rolls:

Roll 1
Dice 1: 2
Dice 2: 6
Dice 3: 4
Dice 4: 4
Dice 5: 1

Player 1 roll again? (yes or no) yes

Roll 2
Dice 1: 6
Dice 2: 4
Dice 3: 5
Dice 4: 5
Dice 5: 6

Player 1 roll again? (yes or no) no

Player 2 rolls:

Roll 1
Dice 1: 3
Dice 2: 4
Dice 3: 5
Dice 4: 2
Dice 5: 1

Player 2 roll again? (yes or no) yes

Roll 2
Dice 1: 1
Dice 2: 4
Dice 3: 3
Dice 4: 2
Dice 5: 2

Player 2 roll again? (yes or no) yes

Roll 3
Dice 1: 3
Dice 2: 2
Dice 3: 1
Dice 4: 6
Dice 5: 2

Player 2 roll again? (yes or no) yes

Roll 4
Dice 1: 6
Dice 2: 4
Dice 3: 5
Dice 4: 6
Dice 5: 3

Player 1 Total: 26
Player 2 Total: 24
Player 1 Wins

Additional requirements:

Helpful functions or expressions: