1 CIS 230 25-Jan-06. 2 Overview Selection Statements –If Statement –Else –Nested If-Else –Switch Repetition Statements –While statement –For Statement.

Slides:



Advertisements
Similar presentations
Flow Control if, while, do-while Juan Marquez (03_flow_control.ppt)
Advertisements

LOOP / REPETITION while loop. for loop do/while loop We assume that loops are not meant to be infinite. That is, there should always be a way out of the.
Computer Science 1620 Loops.
1 Engineering Problem Solving With C++ An Object Based Approach Chapter 3 Control Structures.
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.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 5: Looping by Tony.
1 Objectives You should be able to describe: Relational Expressions The if-else Statement Nested if Statements The switch Statement Common Programming.
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.
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
Control Structures in C++ while, do/while, for switch, break, continue.
Objectives You should be able to describe:
Chapter 5: Repetition Statements. In this chapter, you will learn about: Basic loop structures while loops Interactive while loops for loops Loop programming.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
Chapter 5: Control Structures II (Repetition)
CHAPTER 5: CONTROL STRUCTURES II INSTRUCTOR: MOHAMMAD MOJADDAM.
Chapter 4 Program Control Statements
Copyright © 2000, Department of Systems and Computer Engineering, Carleton University 1 While’s – The Basic Idea Set of operations is repeated as.
1 Chapter 9 Additional Control Structures Dale/Weems/Headington.
COIT29222 Structured Programming Slide 1 COIT29222-Structured Programming Lecture Week 06  Reading: Study Guide Book 2, Modules 9 & 10 Textbook (4 th.
CSCI 3328 Object Oriented Programming in C# Chapter 5: C# Control Statement – Part II UTPA – Fall
2 Objectives You should be able to describe: Relational Expressions Relational Expressions The if-else Statement The if-else Statement Nested if Statements.
CONTROLLING PROGRAM FLOW
Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP For loop is a counter controlled loop. For loop is a pretest loop. Used when number.
Additional Control Structures. Chapter 9 Topics Switch Statement for Multi-way Branching Do-While Statement for Looping For Statement for Looping Using.
1 Chapter 9 Additional Control Structures Dale/Weems.
1 Additional Control Structures. 2 Chapter 9 Topics  Switch Statement for Multi-way Branching  Do-While Statement for Looping  For Statement for Looping.
Chapter 5 Control Structure (Repetition). Objectives In this chapter, you will: Learn about repetition (looping) control structures Explore how to construct.
1 Do-While Statement Is a looping control structure in which the loop condition is tested after each iteration of the loop. SYNTAX do { Statement } while.
Chapter 4 Loops Write code that prints out the numbers Very often, we want to repeat a (group of) statement(s). In C++, we have 3 major ways of.
Chapter 5: Control Structures II (Repetition). Objectives In this chapter, you will: – Learn about repetition (looping) control structures – Learn how.
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.
Chapter 8 Repetition Statements. Introduction Iteration - process of looping or the repetition of one or more statements Loop body - the statement, or.
Program Flow Control - Looping Addis Ababa Institute of Technology Yared Semu April 2012.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 21, 2005 Lecture Number: 10.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 5 Looping.
+ Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 5: Looping.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Control Structures RepetitionorIterationorLooping Part I.
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.
1 CS161 Introduction to Computer Science Topic #8.
Repetition Statements (Loops) The do while Loop The last iteration structure in C++ is the do while loop. A do while loop repeats a statement or.
1 do-while Statement 2 Do-While Statement Is a looping control structure in which the loop condition is tested after each iteration of the loop. SYNTAX.
A loop is a repetition control structure. body - statements to be repeated control statement - decides whether another repetition needs to be made leading.
1 Standard Version of Starting Out with C++, 4th Brief Edition Chapter 5 Looping.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: The while Statement cin within a while Loop The for.
A FIRST BOOK OF C++ CHAPTER 5 REPETITION. OBJECTIVES In this chapter, you will learn about: The while Statement Interactive while Loops The for Statement.
A First Book of C++ Chapter 5 Repetition.
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
Fundamental Programming Fundamental Programming More on Repetition.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 5 Looping.
Chapter 5 Repetition. 2 Objectives You should be able to describe: The while Statement cin within a while Loop The for Statement The do Statement Common.
1 Programming in C++ Dale/Weems/Headington Chapter 9 Additional Control Structures (Switch, Do..While, For statements)
Chapter 6 - Repetition. while Loop u Simplest loop u Two parts: test expression and loop body u Pre-tested loop –Execute loop body if test true –Bypass.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
Chapter 05 (Part II) Control Statements: Part II.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
CHAPTER 2.2 CONTROL STRUCTURES (ITERATION) Dr. Shady Yehia Elmashad.
REPETITION CONTROL STRUCTURE
Chapter 2.2 Control Structures (Iteration)
Conditional Construct
Chapter 2.2 Control Structures (Iteration)
Presentation transcript:

1 CIS Jan-06

2 Overview Selection Statements –If Statement –Else –Nested If-Else –Switch Repetition Statements –While statement –For Statement –Infinite loops –Do-While Statement –Break –Continue Formatted Output

3 Selection Statements if-else –Decision making statement switch –Allows choice: one out of several options

4 If Statement if ( test ) statement; if ( test ) { statements; } if true (i.e. != 0) do statement if false (i.e. == 0) skip it

5 If Statement if ( x == 7 ) cout << “you win\n”; if ( value != 0) { statements; } SAME AS: if (value) { statements; } Remember to use == to compare. Using = will assign the value, and will ALWAYS be true.

6 Else if ( test ) true statement; else false statement; if ( test ) { true statements; } else { false statements; }

7 Else cout << “Please enter a positive integer”; cin >> value; if (value > 0) cout << “Thank you!\n”; else { cout << “The value entered was not positive\n”; cout << “I will use the value 10 \n”; value = 10; }

8 Nested If-Else if (x > 0) cout << "x is positive"; else if (x < 0) cout << "x is negative"; else cout << "x is 0";

9 Nested If-Else cin >> command; if ( command == ‘u’ ) cout << “Move up” << endl; else if ( command == ‘d’ ) cout << “Move down” << endl; else if ( command == ‘l’ ) cout << “Move left” << endl; else if ( command == ‘r’ ) cout << “Move right” << endl; else cout << “Invalid command received << endl;

10 Nested If-Else cin >> command; if ( command == ‘u’ ) cout << “Move up” << endl; else if ( command == ‘d’ ) cout << “Move down” << endl; else if ( command == ‘l’ ) cout << “Move left” << endl; else if ( command == ‘r’ ) cout << “Move right” << endl; else cout << “Invalid command received << endl;

11 Switch switch (value) { case constant1: statement1; statement; break; case constant2: statement2; break; default: statement; break; } Cases may be in any order “case” is terminated with a : (colon) “constants” can be integers or characters Cases may have several statements break jumps out of the construct default is optional

12 Switch cin >> choice; switch (choice) { case 0: cout << “Just a beginner huh?\n”; moves = 15; break; case 1: case 2: case 3: cout << “OK, let’s begin \n”; moves = 10; break; case 4: cout << “You only have 7 moves \n”; moves = 7; break; default: cout << “Incorrect selection \n”; moves = 0; break; } Only way to have several choices do the same thing.

13 If-else-if vs Switch cin >> command; if ( command == ‘u’ ) cout << “Move up” << endl; else if ( command == ‘d’ ) cout << “Move down” << endl; else if ( command == ‘l’ ) cout << “Move left” << endl; else if ( command == ‘r’ ) cout << “Move right” << endl; else cout << “Invalid command received << endl; cin >> command; switch ( command ) { case ‘u’: cout << Move up” << endl; break; case ‘d’: cout << “Move down” << endl; break; case ‘l’: cout << “Move left” << endl; break; case ‘r’: cout << “Move right” << endl; break; default: cout << “Invalid command received” << endl; break; }

14 Repetition Statements while loops –pretest loop for loops –pretest loop do-while loops –posttest loop pretest loop: –Test at the beginning of the loop –Condition must be set prior to loop entry. posttest loop: –Test at the end of the loop –Always executes at least once.

15 While statement while ( expression ) statement; while ( expression ) { statements; } Evaluates the expression if true (i.e. != 0) executes statement(s) While test Body true false

16 While statement while ( x < 0 ) x = x – y + z; while ( a <= b) { a = a + 1; b = b / 2; total = total + b; }

17 For Statement for ( initialize; expression; alter ) statement; for ( initialize; expression; alter ) { statement(s); } Initialize for test Body Alter false true

18 For Statement int i; for ( I = 0; I <= 10; i++) { cout << i << endl; } int answer, a, c, b; a = 0; b = 100; answer = 0; for (c = a; c <= b; c++) answer = answer + c;

19 While vs. For int i = 0; while ( i <= 10) { cout << i << endl; i++; } for (int i = 0; i <= 10; i++) cout << i << endl;

20 Beware the infinite loop! for (c = a; c <= b; 1) answer = answer + c; for ( c = a; 10; c++) answer = answer + c; while (i < 10) { cout << i << endl; } c does not change, infinite loop Always true, infinite loop i is never incremented; infinite loop

21 Do-While Statement do { body; } while ( expression ) executes statement(s) evaluates the expression if true (i.e. != 0) do body while test true false

22 Do-While statement do { cout << “Enter a number between 1 & 10 \n”; cin >> value; } while ( value 10 )

23 Break break –Can also be used to exit (jump out) of other loops as well for (i = 1; a <= b; i++) { a += i;//a = a + i b -= i;//b = b – i if (b <=0) break; c += a + b//c = c + (a + b) } Can end in two ways

24 Continue continue –Goes to the test of a loop (Does the test again) –Must be a loop not a switch while ( a < b) { cin >> a; if (a 9) continue; //process rest of loop }

25 Formatted Output #include setw( ) fixed setprecision( ) setiosflags( )

26 Formatted Output cout << 6 << endl << 18 << endl << 124 << endl << "---\n" << ( ) << endl; Results:

27 setw() Used within cout Sets field width for next output Right justifies

28 setw() //Added setw(3) to each line cout << setw(3) << 6 << endl << setw(3) << 18 << endl << setw(3) << 124 << endl << "---\n" << ( ) << endl; Results:

29 setw int a = 24; int b = 573; cout << '|' << setw(4) << a << '|' << setw(6) << b << '|' << setw(2) << b << '|' << endl; Results: | 24| 573|573|

30 fixed Used with cout Forces the display of a decimal point

31 setprecision() Used with cout Sets maximum number of decimal places –Will show less if fewer decimal values Floating point numbers only

32 setprecision() float x = ; float y = 6.4; cout << '|' << setw(7) << setprecision(2) << y << '|' << endl; cout << '|' << setw(7) << fixed << setprecision(2) << x << '|' << endl; cout << '|' << setw(7) << fixed << setprecision(2) << y << '|' << endl; Results: | 6.4| | 19.17| | 6.40|

33 setiosflags() ios::fixed ios::scientific ios::showpoint ios::showpos ios::left ios::right