Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming Fundamental

Similar presentations


Presentation on theme: "Programming Fundamental"— Presentation transcript:

1 Programming Fundamental
Instructor Name: Muhammad Safyan Lecture-8

2 Lecture outline While loop cases Do while loop

3 Case-3Flag-controlled while loops
uses a Boolean variable to control the loop The Boolean variable is set to either true or false  while the expression evaluates to true, the loop statement continues The Boolean variable must be set/reset within the while statement.  

4

5 Number Guessing Game Generate a random number between 0 and 1000.
If the user guesses the number correctly, the program outputs an appropriate message. Otherwise, the program checks whether the guessed number is less than the random number. And display message accordingly. uses the function rand of the header file cstdlib to generate a random number. rand() returns an int value

6 using namespace std; #include<iostream> #include <conio.h> #include <cstdlib> main() { int i=rand(); cout<<i; getch(); }

7 Number Guessing Game To convert it to an integer greater than or equal to 0 and less than 100. rand() % 100 It is possible that every time you run your program, the function rand gives the same random number. In this case, you can use the function time, of the header file ctime, to include the time. The function time returns time as a long value. The following expression uses both the functions rand and time to generate a random integer greater than or equal to 0 and less than 100: (rand() + time(0)) % 100;

8 #include <iostream > #include <cstdlib >
#include <conio.h > using namespace std; int main() { int num; int guess; bool done; num = (rand() + time(0)) % 100; done = false; while (!done) cout << "Enter an integer greater" << " than or equal to 0 and " << "less than 100: " <<endl; cin >> guess; cout << endl; if (guess == num) { cout << "You guessed the correct " << "number." << endl; done = true; } else if (guess < num) cout << "Your guess is lower " << "than the number.\n" << "Guess again!" << endl; cout << "Your guess is higher " getch();

9 Case 4: EOF-controlled while loop
The while statement executes until there are no more data items If the program has reached the end of the input data or enters into the fail state, the input stream variable returns false;  in other cases, the input stream variable returns true The first item is read before the while statement and if the input stream variable is true, the while statement is entered; additional items are read within the while statement.   Note. In the DOS environment, the end-of-file marker is entered using ctrl+z.

10 Case 4: EOF-controlled while loop
Until now, we have used an input stream variable, such as cin, and the extraction operator, >>, to read and store data into variables. However, the input stream variable can also return a value after reading data. If the program has reached the end of the input data, the input stream variable returns the logical value false

11 Case 4: EOF-controlled while loop
Example:       int N, sum;     cout << "Enter the numbers to be added (for end enter ctrl+z):" << endl;     sum = 0;     cin >> N;     while (cin)     {        sum = sum + N;         cin >> N;     }    cout << "The sum of the entered numbers is " << sum << endl;

12 Sample Program 3 Problem statement:
Calculate the factorial of a given number. Solution: The factorial of a number N is defined as: N(N-1)(N-2)………….3.2.1

13

14 Practice Problem Problem-1
Write a program that determine whether given number is prime or not? (without using break statement) Problem-2 Write a program for Counting Digits and displaying their total count at the end of the program. digits digits - 1 digit Problem-3 Write a program of Euclid’s for gcd. Read the properties first and then start your coding. Properties 1. gcd(a,a)=a 2. If a > b, then gcd(a,b) = gcd(a‐b,b) int main() { int N; cin >> N; int ndigits = 0; // Inv: ndigits contains the number of digits in the // tail of the number, N contains the remaining // part (head) of the number while (N > 9) { ndigits = ndigits + 1; N = N/10; // extracts one digit } cout << ndigits + 1 << endl; ============== #include <iostream> #include <conio> int a, b; cin >> a >> b; // Let a=A, b=B // gcd(A,B) = gcd(a,b) while (b != 0) { int r = a%b; a = b; b = r; // Guarantees b < a (loop termination) cout << a << endl;

15 More on Expressions in while Statements
while loop is controlled by a single variable. However, there are situations when the expression in the while statement may be more complex. In Guessing magic number program gives as many tries as the user needs to guess the number. Suppose you want to give the user no more than five tries to guess the number. If the user does not guess the number correctly within five tries, then the program outputs the random number generated by the program as well as a message that you have lost the game

16 main() { int noOfGuesses=0; int guess; bool done=false; int num = (rand() + time(0)) % 100; while ((noOfGuesses < 5) && (!done)) cout << "Enter an integer greater than or equal to 0 and " << "less than 100: "; cin >> guess; cout << endl; noOfGuesses++; if (guess == num) cout << "Winner!. You guessed the correct number." << endl; done = true; } else if (guess < num) cout << "Your guess is lower than the number.\n" << "Guess again!" << endl; else cout << "Your guess is higher than the number.\n" }//end while getch(); }

17 Do While

18 Do-While Statement . To ensure that a block of statement is executed at least once do { execute statement; } while ( condition ) ; This structure describes ‘execute the statements enclosed in brace do clause' when the condition in while clause is true. Broadly speaking, in while loop, the condition is tested at the beginning of the loop before the body of the loop is performed. Whereas in do-while loop, the condition is tested after the loop body is performed. Therefore, in do-while loop, the body of the loop is executed at least once.

19 Let’s consider the example of guessing a character
Let’s consider the example of guessing a character. We have a character in the guessed by the user. Let’s call it ‘z’. The program allows five tries user to guess the character. We declare a variable tryNum to store character for guessing. he store this character in a variable c. declare the variable c of type char. The data type char is used to store a single character. We assign a character to a variable of char type by putting the character in ngle q ent statement to assign a value to a char variable will as c = ‘a’. Note that there should be a single character in single quotes. The tement like c = ‘gh’ will be a syntax error. re we use the do-while construct. In the do clause we prompt the user to enter a haracter. er, we compare it with our character i.e e as ours e add 1 to tryNum variable. yNum is less than or equal true, then the body of the do clause is repeated gain. We do this only when the condition (tryNum <= 5) remains true. If it is therwise, the control goes to the first statement after the do-while loop. hed in first or second try, then we should exit the loop. We know that e loop is terminated when the condition tryNum <= 5 becomes false, so we assign a value which is greater than 5 to tryNum after displaying the message. Now the program to be (chances) to the the number of tries. The program prompts the user to enter a W We d ch si uotes. Thus the assignm be sta He c After getting character in variable c from us ‘z’. We use if\else structure for this comparison. If the character is the sam then we display a message to congratulate the user else w And then in while clause, we test the condition whether tr to 5 (tryNum <= 5). If this condition is a o If guess is matc th

20

21


Download ppt "Programming Fundamental"

Similar presentations


Ads by Google