Programming Fundamental

Slides:



Advertisements
Similar presentations
Chapter 5: Control Structures II (Repetition)
Advertisements

While Loops Programming. COMP102 Prog Fundamentals I: while Loops/Slide 2 Shortcut Assignments l C++ has a set of shortcut operators for applying an operation.
True or false A variable of type char can hold the value 301. ( F )
Computer Science 1620 Loops.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 5 Looping.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
1 CS 105 Lecture 4 Selection Statements Wed, Jan 26, 2011, 6:05 pm.
Chapter 5: Control Structures II (Repetition)
What is the out put #include using namespace std; void main() { int i; for(i=1;i
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
CHAPTER 5 CONTROL STRUCTURES II (Repetition). In this chapter, you will:  Learn about repetition (looping) control structures  Explore how to construct.
Chapter 5: Control Structures II (Repetition)
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 5: Control Structures II (Repetition)
Chapter 5: Control Structures II (Repetition)
Outlines Chapter 3 –Chapter 3 – Loops & Revision –Loops while do … while – revision 1.
CHAPTER 5: CONTROL STRUCTURES II INSTRUCTOR: MOHAMMAD MOJADDAM.
EGR 2261 Unit 5 Control Structures II: Repetition  Read Malik, Chapter 5.  Homework #5 and Lab #5 due next week.  Quiz next week.
More on Input Output Input Stream : A sequence of characters from an input device (like the keyboard) to the computer (the program running). Output Stream.
Introduction to Programming (in C++) Loops Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
Chapter 5: Control Structures II (Repetition). Objectives In this chapter, you will: – Learn about repetition (looping) control structures – Learn how.
6/3/2016 CSI Chapter 02 1 Introduction of Flow of Control There are times when you need to vary the way your program executes based on given input.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X1 Chapter 3 Control Statements.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 5 Looping.
Loops cause a section of a program to be repeated a certain number of times. The repetition continues while a condition remains true. When a condition.
Before we get started…. First, a few things… Weighted Grading System Programming Style Submitting your assignments… The char and string variable types.
1 Standard Version of Starting Out with C++, 4th Brief Edition Chapter 5 Looping.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 5: Control Structures II (Repetition)
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 5 Looping.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 5: Control Structures II (Repetition)
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 5 Looping.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 5: Control Structures II (Repetition)
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 23, 2005 Lecture Number: 11.
Introduction to Loop. Introduction to Loops: The while Loop Loop: part of program that may execute > 1 time (i.e., it repeats) while loop format: while.
Chapter 4 Repetition Structures
Basic concepts of C++ Presented by Prof. Satyajit De
Introduction to Computer Programming
Topic 4: Looping Statements
Chapter 3 Control Statements
REPETITION CONTROL STRUCTURE
CHAPTER 4 REPETITION CONTROL STRUCTURE / LOOPING
The setw Manipulator The setw manipulator causes the number (or string) that follows it in the stream to be printed within a field n characters wide, where.
Chapter 5: Control Structures II (Repetition)
Programming Fundamental
Engineering Problem Solving with C++, Etter/Ingber
Control Structures II (Repetition)
Introduction to C++ October 2, 2017.
Chapter 5: Loops and Files.
Chapter 5: Looping Starting Out with C++ Early Objects Seventh Edition
Chapter 3 Control Statements Lecturer: Mrs Rohani Hassan
Repetition-Sentinel,Flag Loop/Do_While
Control Structures Combine individual statements into a single logical unit with one entry point and one exit point. Used to regulate the flow of execution.
Programming Fundamentals
Chapter 5: Looping Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Lecture 4B More Repetition Richard Gesick
Control Structures Combine individual statements into a single logical unit with one entry point and one exit point. Used to regulate the flow of execution.
TOPIC 4: REPETITION CONTROL STRUCTURE
Chapter 5: Looping Starting Out with C++ Early Objects Seventh Edition
Today in CS161 Week #3 Learn about… Writing our First Program
Alternate Version of STARTING OUT WITH C++ 4th Edition
Chapter 6: Repetition Statements
Computing Fundamentals
Chapter 4: Control Structures I (Selection)
© Copyright 2016 by Pearson Education, Inc. All Rights Reserved.
Control Structures Part 1
Chapter 5: Control Structures II (Repetition)
Chapter 4 Repetition Structures
Programming Fundamental
Presentation transcript:

Programming Fundamental Instructor Name: Muhammad Safyan Lecture-8

Lecture outline While loop cases Do while loop

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.  

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

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

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;

#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();

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.

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

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;

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

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. 8713105 - 7 digits 156 - 3 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;

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

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(); }

Do While

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.

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