0% found this document useful (0 votes)
3 views

do while loop

The lab manual covers programming fundamentals in C++, focusing on type casting and the do-while loop. It explains implicit and explicit type casting with code examples, demonstrating how data types can be converted automatically or manually. Additionally, it provides examples of using the do-while loop for calculating averages and implementing a number guessing game.

Uploaded by

iqraghouri440
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

do while loop

The lab manual covers programming fundamentals in C++, focusing on type casting and the do-while loop. It explains implicit and explicit type casting with code examples, demonstrating how data types can be converted automatically or manually. Additionally, it provides examples of using the do-while loop for calculating averages and implementing a number guessing game.

Uploaded by

iqraghouri440
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Lab Manual: Programming Fundamentals

University of Management and Technology,


Lahore Campus

Lab Manual
Instructor: Waqar Ashiq
Lecturer, Software Engineering Department
Email: [email protected]

Type casting and do while loop


Type Casting in C++
Type casting refers to the conversion of one data type to another in a program.
Typecasting can be done in two ways: automatically by the compiler and manually
by the programmer or user. Type Casting is also known as Type Conversion.

Type Conversion or Explicit Type Casting.

Implicit Type Casting or Implicit Type Conversion

o It is known as the automatic type casting.


o It automatically converted from one data type to another without any
external intervention such as programmer or user. It means the
compiler automatically converts one data type to another.
o All data type is automatically upgraded to the largest type without
losing any information.
o It can only apply in a program if both variables are compatible with
each other.

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;

int num = 20;


char ch = 'a';
int res = 20 + 'a';
cout << " Type casting char to int data type ('a' to 20): " << res << endl;

float val = num + 'A';


cout << " Type casting from int data to float type: " << val << endl;
return 0;
}

Explicit Type Casting or Explicit Type Conversion

o It is also known as the manual type casting in a program.


o It is manually cast by the programmer or user to change from one data
type to another type in a program. It means a user can easily cast one
data to another according to the requirement in a program.
o It does not require checking the compatibility of the variables.
o In this casting, we can upgrade or downgrade the data type of one
variable to another in a program.

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

cout << " \n Explicit Type Casting: " << endl;


// use cast () operator to convert int data to float
res = (float) 21 / 5;
cout << " The value of float variable (res): " << res << endl;

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

Example 1: Write a program to calculate the average of three


numbers and take a condition to calculate other set of
numbers .
#include <iostream>

using namespace std;

int main()

int s1,s2,s3;

double average;

char again;

do

cout<<"Enter the three scors";

cin>>s1>>s2>>s3;

average =(s1+s2+s3)/3;

cout<<"The average is "<<average <<"\n";

cout<<"Do you want to average another set? (Y/N)";

cin>>again;

while(again=='Y'|| again=='y');

return 0;

}
Lab Manual: Programming Fundamentals

Example 2: Write a C++ program to implement the Number Guessing Game. In


this game the computer chooses a random number between 1 and 100, and the
player tries to guess the number in as few attempts as possible. Each time the
player enters a guess, the computer tells him whether the guess is too high, too
low, or right. Once the player guesses the number, the game is over.
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

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++;

if (guess > num)


cout << "Too high!\n\n";
else if (guess < num)
cout << "Too low!\n\n";
Lab Manual: Programming Fundamentals

else
cout << "\nCorrect! You got it in " << tries << " guesses!\n";
} while (guess != num);

return 0;
}

You might also like