Intro to Programming Week # 5 Repetition Structure Lecture # 9

Slides:



Advertisements
Similar presentations
Control Statements. Define the way of flow in which the program statements should take place. Control Statements Implement decisions and repetitions.
Advertisements

Do-while Loops Programming. COMP102 Prog Fundamentals I: do-while Loops /Slide 2 The do-while Statement l Syntax do action while (condition) l How it.
CSC 221: Computer Programming I Fall 2001  conditional repetition  repetition for: simulations, input verification, input sequences, …  while loops.
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 )
1 Lecture 11:Control Structures II (Repetition) (cont.) Introduction to Computer Science Spring 2006.
Programming Switch command. COMP102 Prog. Fundamentals: Switch command / Slide 2 Multiple Selection: The switch Statement value1 action 1 value2 action.
1 10/20/08CS150 Introduction to Computer Science 1 do/while and Nested Loops Section 5.5 & 5.11.
Iteration This week we will learn how to use iteration in C++ Iteration is the repetition of a statement or block of statements in a program. C++ has three.
Chapter 6 - Repetition. Introduction u Many applications require certain operations to be carried out more than once. Such situations require repetition.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
1 CS 192 Lecture 9 Winter 2003 December 19, 2003 Dr. Shafay Shamail.
Loops Programming. COMP104 Lecture 9 / Slide 2 Shortcut Assignment l C++ has a set of operators for applying an operation to a variable and then storing.
Outlines Chapter 3 –Chapter 3 – Loops & Revision –Loops while do … while – revision 1.
Chapter 4 Program Control Statements
Lecture 8: Choosing the Correct Loop. do … while Repetition Statement Similar to the while statement Condition for repetition only tested after the body.
Chapter 4 Selection Structures: Making Decisions.
Outlines Chapter 3 –Chapter 3 – Loops & Revision –Loops while do … while – revision 1.
CONTROLLING PROGRAM FLOW
CPS120: Introduction to Computer Science Decision Making in Programs.
Chapter 7 Additional Control Structures. 2 2 void GetYesOrNo (/* out */ char& response) // Inputs a character from the user // Postcondition: response.
C++ for Engineers and Scientists, Third Edition1 Objectives In this chapter, you will learn about: Basic loop structures while loops Interactive while.
OBJECTIVES  Illustration of the Concept of Control Flow  Types of Control Flow  Knowledge about conditional and repetitive statements.  Make more.
Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using.
ㅎㅎ logical operator if if else switch while do while for Third step for Learning C++ Programming Repetition Control Structures.
Chapter 8 Repetition Statements. Introduction Iteration - process of looping or the repetition of one or more statements Loop body - the statement, or.
COMPUTER PROGRAMMING. Iteration structures (loops) There may be a situation when you need to execute a block of code several number of times. In general,
Program Flow Control - Looping Addis Ababa Institute of Technology Yared Semu April 2012.
Chapter 3. Outline Relational Operators Loops Decisions Logical Operators Precedence Summary.
LOOP & Type Conversion. do – while Loop In the while loop, the test expression is evaluated at the beginning of the loop. If the test condition is false.
CSC1201: Programming Language 2 Lecture 1 Level 2 Course Nouf Aljaffan (C) CSC 1201 Course at KSU1.
Lecture 7: Making Decisions Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
GE 211 Dr. Ahmed Telba. // compound assignment operators #include using namespace std; int main () { a =5 int a, b=3; a = b; a+=2; // equivalent to a=a+2.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
1 Programming in C++ Dale/Weems/Headington Chapter 9 Additional Control Structures (Switch, Do..While, For statements)
COMP Loop Statements Yi Hong May 21, 2015.
FLOW OF CONTROL In C++ the program execution starts with the first statement in the main() and ends with the last statement of main(). This is called.
LESSON 5 Loop Control Structure. Loop Control Structure  Operation made over and over again.  Iterate statement.
Computer Programming -1-
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 23, 2005 Lecture Number: 11.
Looping I (while statement). CSCE 1062 Outline  Looping/repetition construct  while statement (section 5.1)
Control Structures Repetition or Iteration or Looping Part II.
Chapter 4 Repetition Structures
REPETITION CONTROL STRUCTURE
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.
CS161 Introduction to Computer Science
Intro to Programming Week # 6 Repetition Structure Lecture # 10
Programming Fundamentals
Chapter 2.2 Control Structures (Iteration)
Programming Fundamentals
Introduction to Functions
Chapter 8 Repetition Statements
Intro to Programming Week # 3 If-else Statement Lecture # 6
COMS 261 Computer Science I
CSC215 Lecture Flow Control.
CSC215 Lecture Control Flow.
Intro to Programming Week # 2 Variables/Data type Lecture # 4 & 5
Chapter 2.2 Control Structures (Iteration)
Computing Fundamentals
Chapter 4: Control Structures I (Selection)
Control Structures Part 3
By Hector M Lugo-Cordero September 3, 2008
CprE 185: Intro to Problem Solving (using C)
Reading from and Writing to Files
Repetition Statements (Loops) - 2
CSC215 Lecture Control Flow.
Chapter 4 Repetition Structures
CprE 185: Intro to Problem Solving (using C)
Reading from and Writing to Files
Programming Fundamental
Presentation transcript:

Intro to Programming Week # 5 Repetition Structure Lecture # 9 Department of Computer Science & Engineering Air University Intro to Programming Week # 5 Repetition Structure Lecture # 9 By: Saqib Rasheed

do-while

while loop while (condition) { statements; : } statements;

While loop executes zero or more times. What if we want the loop to execute at least one time?

Do while loop execute one or more times

Syntax of do-while loop { statements ; } while ( condition ) ;

The Do-While Statement Syntax do Action while (Expression) Semantics Execute Action If Expression is true then execute Action again Repeat this process until Expression evaluates to false Action is either a single statement or a group of statements within braces Action true Expression false

Example Develop a program in C++ that calculate the factorial of a given number. Use for loop to calculate the factorial, & do – while loop to perform the operation as many times as user want. Air University

#include<iostream> using namespace std; int main() { char ch; int value , factorial ; do cout<< "\n\n\t\tEnter an integer value: "; cin >> value; Air University

factorial = 1; for(int i = 2; i <= value; i++) factorial factorial = 1; for(int i = 2; i <= value; i++) factorial *= i; cout << "\nFactorial " << value << " is " << factorial; cout<< "\nDo you want to enter another value (y or n) "; cin >> ch; cout<<endl; } while((ch == 'y') || (ch == 'Y')); return 0; Air University

Example Create the equivalent of a four-function calculator. The program should Ask the user to enter a number, an Operator, and another number (10 + 20), using floating point. It should then carry out the specified arithmetical operation: adding, subtracting, multiplying, or dividing the two numbers. Use switch statement to select the operation. Finally, display the Result. Air University

When it finishes the calculation, the program should ask wheather the user wants to do another calculation. The response can be 'y' or 'n'. Some sample interaction with the program might look like this: Enter Num1, Oper, Num2 : 100 / 10 Answer = 10 Do you wnat to Continue ('y' or 'n')? y Again Enter Num1, Oper, Num2 : 20 * 3 Answer = 60 Do you wnat to Continue ('y' or 'n')? n Air University

#include<iostream #include<iostream.h> void main () { int num1,num2,ans; char oper,ch; do cout<<" Enter Num1, Oper, Num2 : "; cin>>num1>>oper>>num2; Air University

switch(oper) { case '+': ans=num1+num2; break; case '-': ans=num1-num2; break; case '*': ans=num1*num2; break; case '/': ans=num1/num2; break; default: ans=0; } cout<<"Answer= "<<ans; cout<<"\n\nDo you wnat to Continue ('y' or 'n')"; cin>>ch; } while (ch!='n'); Air University

The break Statement The break statement causes an exit from a loop Just as it does from a switch statement. The next statement after the break is executed is the statement following the loop

The break Statement

Break Statement for(int i=0; i<10; i++) { cout<<"\nLine from loop = "<<i; if (i>5){ break; } cout<<“Next Line after loop”; Air University

Continue statement The break statement takes you out of the bottom of a loop. Sometimes, however, you want to go back to the top of the loop when something unexpected happens. Executing continue has this effect. (Strictly speaking, the continue takes you to the closing brace of the loop body, from which you may jump back to the top.)

Continue Statement int main() { long dividend, divisor; char ch; do cout << "Enter dividend: "; cin >> dividend; cout << "Enter divisor: "; cin >> divisor; if( divisor == 0 ) cout << "Illegal divisor\n"; continue; //go to top of loop }

cout << "Quotient is " << dividend / divisor; cout << ", remainder is " << dividend % divisor; cout << "\nDo another? (y/n): "; cin >> ch; } while( ch != 'n' ); return 0; }

goto Statement Label for goto int x = 0; thisline: cout<<"Line 1\n"; x++; cout<<"Line 2\n"; Sleep(20); cout<<"Line 3\n"; goto thisline; Air University