do while loop
do while loop
Lab Manual
Instructor: Waqar Ashiq
Lecturer, Software Engineering Department
Email: [email protected]
Code:
#include <iostream>
using namespace std;
int main ()
{
Lab Manual: Programming Fundamentals
short x = 200;
int y;
y = x;
cout << " Implicit Type Casting " << endl;
cout << " The value of x: " << x << endl;
cout << " The value of y: " << y << endl;
Code:
#include <iostream>
using namespace std;
Lab Manual: Programming Fundamentals
int main ()
{
// declaration of the variables
int a, b;
float res;
a = 21;
b = 5;
cout << " Implicit Type Casting: " << endl;
cout << " Result: " << a / b << endl; // it loses some information
return 0;
}
Lab Manual: Programming Fundamentals
Do while Loop:
• do-while: a posttest loop – execute the loop, then test the expression
• General Format:
do
statement; // or block in { }
while (expression);
• Note that a semicolon is required after (expression)
Example:
Code:
int x = 1;
do
{
cout << x << endl;
} while(x < 0);
Although the test expression is false, this loop will execute one time because do-
while is a posttest loop.
Lab Manual: Programming Fundamentals
int main()
int s1,s2,s3;
double average;
char again;
do
cin>>s1>>s2>>s3;
average =(s1+s2+s3)/3;
cin>>again;
while(again=='Y'|| again=='y');
return 0;
}
Lab Manual: Programming Fundamentals
int main()
{
int num, guess, tries = 0;
srand(time(0)); //seed random number generator
num = rand() % 100 + 1; // random number between 1 and 100
cout<<num;
cout << "Guess My Number Game\n\n";
do
{
cout << "Enter a guess between 1 and 100 : ";
cin >> guess;
tries++;
else
cout << "\nCorrect! You got it in " << tries << " guesses!\n";
} while (guess != num);
return 0;
}