1 Lab Session-7 CSIT-121 Fall 2003 4 Revising Structured Choice 4 The While Loop variations 4 Lab Exercises.

Slides:



Advertisements
Similar presentations
Control Structures Corresponds with Chapters 3 and 4.
Advertisements

Basic Control Structures Control order of execution of statements sequential selection iteration - Repeat some action while a certain condition is true.
Computer Science 1620 Loops.
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
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.
Starting Out with C++, 3 rd Edition 1 Chapter 5. Looping.
1 CIS Jan Overview Selection Statements –If Statement –Else –Nested If-Else –Switch Repetition Statements –While statement –For Statement.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 5: Looping by Tony.
Chapter 4 - Control Structures: Part 1 Outline 4.4Control Structures 4.5The if Selection Structure 4.6The if/else Selection Structure 4.7The while Repetition.
1 9/28/07CS150 Introduction to Computer Science 1 Loops section 5.2, 5.4, 5.7.
Announcements The first graded lab will be posted Sunday 2/15 and due Friday 2/27 at midnight It is brand new, so please don’t hand in last semester’s.
1 10/20/08CS150 Introduction to Computer Science 1 do/while and Nested Loops Section 5.5 & 5.11.
CS150 Introduction to Computer Science 1
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.
Chapter 5: Loops and Files.
1 10/9/06CS150 Introduction to Computer Science 1 for Loops.
1 Lab Session-6 CSIT-121 Spring 2005 Structured Choice The do~While Loop Lab Exercises.
1 Lab Session-VII CSIT-121 Fall Revising Previous Lab and performing ASCII chart printing experiment (DEMO) 4 Visual Studio Features 4 The While.
1 Lab Session-V CSIT-120 Fall 2000 Menu Driven Software Lab Exercise While and do-while statements Lab-V Continues: The if statement Logical Operators.
1 Lab Session-VI CS121 Fall 2000 l HW#2 Assigned l Multiple Choice Selectors l The While Loop l Switch-Case-Break Exercise l The Counter Controlled Loops.
1 Lab Session-III CSIT-120 Spring 2001 Revising Previous session Data input and output While loop Exercise Limits and Bounds GOTO SLIDE 13 Lab session.
1 Lab Session-7 CSIT-121 Fall Introducing Structured Choice 4 The do~While Loop 4 Lab Exercises.
COMP 110 Introduction to Programming Mr. Joshua Stough September 24, 2007.
CIS3931 – Intro to JAVA Lecture Note Set 3 19-May-05.
Presented by Joaquin Vila Prepared by Sally Scott ACS 168 Problem Solving Using the Computer Week 12 Boolean Expressions, Switches, For-Loops Chapter 7.
1 Lab Session-III CSIT-120 Fall 2000 Revising Previous session Data input and output While loop Exercise Limits and Bounds Session III-B (starts on slide.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Looping Exercises Deciding Which Loop to Use At this.
Chapter 5: Control Structures II (Repetition)
EGR 2261 Unit 5 Control Structures II: Repetition  Read Malik, Chapter 5.  Homework #5 and Lab #5 due next week.  Quiz next week.
Chapter 4: Looping. Resource: Starting Out with C++, Third Edition, Tony Gaddis 5.1 The Increment and Decrement Operators ++ and -- are operators that.
CSE1222: Lecture 3The Ohio State University1. Assignment Operations  The C++ assignment operator is: =  Examples: x = 3 * 5; y = x – 7; y = y + 4; Do.
A First C Program /* Print a Message */ #include main() { printf("This is a test!\n"); } The program is compiled and run as cc pg32.c  a.out  This is.
Looping II (for statement). CSCE 1062 Outline  for statement  Nested loops  Compound assignment operators  Increment and decrement operators.
Previously Repetition Structures While, Do-While, For.
Chapter 5: Control Structures II (Repetition). Objectives In this chapter, you will: – Learn about repetition (looping) control structures – Learn how.
1 09/20/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
Repetition Structures Repetition Structures allow you to write programs that will repeat program steps multiple times. –Also called Loops –Counter controlled.
111/18/2015CS150 Introduction to Computer Science 1 Announcements  I have not graded your exam yet.
Counter-Controlled Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
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.
Computer Programming Control Structure
Introduction to Loops Iteration Repetition Counting Loops Also known as.
Loops and Files. 5.1 The Increment and Decrement Operators.
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.
Agenda Perform Quiz #1 (20 minutes) Loops –Introduction / Purpose –while loops Structure / Examples involving a while loop –do/while loops Structure /
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 Looping 5. The Increment and Decrement Operators 5.1.
COMP Loop Statements Yi Hong May 21, 2015.
PGT C Programming1 Week 4 – Repetition Structures / Loops.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
Chapter 4 Repetition Statements Program Development and Design Using C++, Third Edition.
1 ICS103 Programming in C Lecture 7: Repetition Structures.
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 5: Looping Starting Out with C++ Early Objects Seventh Edition
Chapter 4: Making Decisions.
Chapter 4: Making Decisions.
Unary Operators ++ and --
3 Control Statements:.
Looping III (do … while statement)
EECE.2160 ECE Application Programming
Chapter 4 Repetition Structures
Presentation transcript:

1 Lab Session-7 CSIT-121 Fall Revising Structured Choice 4 The While Loop variations 4 Lab Exercises

2 Revising Structured Choice 4 Structured choice can be implemented with switch~case construct. When should we prefer switch-case over if-else? 4 Why is a default case added at the end? 4 Is it ok to use floating point values in switch-case statements?

3 Counter Controlled Loops 4 The for loops are counter controlled loops.(Convert this loop to for) –int k; k=100; –while (k>0) {cout<<k<<endl; k--} 4 You can make the counter increment or decrement or “jump up” or “jump down” (i.e. inc/dec steps allowed) 4 You can increment or decrement multiple counters if you wish

4 The counter-controlled loop 4 The for loop is best for implementing a counter controlled loop because we can check all three conditions (I,T,U) at a glance. 4 For example, we can convert a given while loop into for loop to show how it works

5 The for loop 4 int count=0; //I 4 while (count<20) { //T 4 cout >thisID; count++;} //U 4 Changed to: 4 int count; 4 for (count=0;/*I*/ count<20; /*T*/count++/*U*/) 4 {cout >thisID; } 4 The for loop may have null expressions 4 e.g. for (; ;){cout<<“Will it end?\n”;}

6 The Upper and Lower Case Letters 4 We wish to print a list of uppercase letters with their ASCII codes followed by a list of lower case letters with their ASCII codes in the same row albeit with some tabbing distance

7 Printing List of Alphabets 4 Declare two char variables 4 char lower, upper; 4 assign these the starting values 4 (e.g. lower=‘a’) 4 cout <<“\t”<<lower 4 <<“\t”<<int(lower); 4 Increment these in for loop

8 While Loop Design 4 Design While loop with care else the program will be stuck in INFINITE LOOP. 4 Check for these three conditions 4 INITIALIZATION (The loop control variable should be initialized before starting the loop) 4 TESTING While(test) {…..} 4 UPDATE Loop control variable should be updated within the loop

9 Lab Exercise (Demo Required Sec01-Oct 21, Sec02-Oct 23) 4 Develop a program that accepts three integers and prints their bar chart, followed by their average. For example, input 5, 7 and 9 & get output as: 4 5: ***** 4 7: ******* 4 9: ********* 4 Average is 7.0

10 Using a do-while loop 4 You should use a do~while loop if you wish to run the loop body at least once before testing the condition 4 For example, a menu driven program should display all the choices and then accept user input. Next, the user input can be tested for validity. If invalid, run the loop body again

11 Practice Exercises 4 Design a program that: 4 prompts the user to input the salary for tax computation 4 Checks the user input to see if it is within the given salary range ( ) 4 If not, it gives a warning beep and re- displays the prompt on a clean screen. 4 Use do-while{} loop and see how the screen can be cleared between menu displays

12 More Exercises for Practice 4 Programming Problem 2 page 306 (old edition see page 322) 4 Exam Preparation Exercise 10 page 303 (old edition see page 320) 4 Exam Preparation Exercise 12 page 304 (old edition see page 321)