111/18/2015CS150 Introduction to Computer Science 1 Announcements  I have not graded your exam yet.

Slides:



Advertisements
Similar presentations
Switch code for Lab 4.2 switch (input) { /* input is a variable that we will test. */ case 'M': printf("The prefix is equal to 1E6.\n"); break; case 'k':
Advertisements

Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students Control Structures Nested ifs, switch statements.
Basic Control Structures Control order of execution of statements sequential selection iteration - Repeat some action while a certain condition is true.
True or false A variable of type char can hold the value 301. ( F )
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
1 10/16/06CS150 Introduction to Computer Science 1 switch Selection Structure.
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
1 9/24/07CS150 Introduction to Computer Science 1 Relational Operators and the If Statement.
1 9/26/08CS150 Introduction to Computer Science 1 Logical Operators and if/else statement.
1 9/28/07CS150 Introduction to Computer Science 1 Loops section 5.2, 5.4, 5.7.
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
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
1 Lab Session-7 CSIT-121 Fall Revising Structured Choice 4 The While Loop variations 4 Lab Exercises.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Logical and Relational Expressions Nested if statements.
1 10/9/06CS150 Introduction to Computer Science 1 for Loops.
1 CS150 Introduction to Computer Science 1 Relational Operators and the If Statement 9/22/08.
Control Structures Control structures control the flow of program execution. 3 types of control structures: sequence, selection.
1 9/28/07CS150 Introduction to Computer Science 1 Logical Operators and if/else statement.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
Presented by Joaquin Vila Prepared by Sally Scott ACS 168 Problem Solving Using the Computer Week 12 Boolean Expressions, Switches, For-Loops Chapter 7.
Control Structures I (Selection)
Selection Statements in C++ If Statement in C++ Semantics: These statements have the same meaning as in the algorithmic language. 2- Two way selection:
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
Switch Statement in C++. Syntax switch (selector) { case L1: statements1; break; case L2: statements2; break; … default: statements_n; } Semantics: This.
Decision II. CSCE 1062 Outline  Boolean expressions  switch statement (section 4.8)
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 9 – Income Tax Calculator Application: Introducing.
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.
Multi Way Selection You can choose statement(s) to run from many sets of choices. There are two cases for this: (a)Multi way selection by nested IF structure.
PROBLEM SOLVING & ALGORITHMS CHAPTER 5: CONTROL STRUCTURES - SELECTION.
Previously Repetition Structures While, Do-While, For.
1 09/20/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
Chapter 05 (Part III) Control Statements: Part II.
Selection Relational Expressions A condition or logical expression is an expression that can only take the values true or false. A.
Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Choices and Decisions if statement if-else statement Relational.
1 09/15/04CS150 Introduction to Computer Science 1 Life is Full of Alternatives Part 2.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X1 Chapter 3 Control Statements.
1 CS161 Introduction to Computer Science Topic #8.
1 09/27/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
1 10/3/05CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Lecture 7 Computer Programming -1-. Conditional Statements 1- if Statement. 2- if ….. else Statement. 3- switch.
1 9/26/05CS150 Introduction to Computer Science 1 Life is Full of Alternatives.
1 Advanced Programming IF. 2 Control Structures Program of control –Program performs one statement then goes to next line Sequential execution –Different.
Why Repetition? Read 8 real numbers and compute their average REAL X1, X2, X3, X4, X5, X6, X7, X8 REAL SUM, AVG READ *, X1, X2, X3, X4, X5, X6, X7, X8.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
C++ Programming Control Structures I (Selection).
Introduction to Computer Programming
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.
CS161 Introduction to Computer Science
for Repetition Structures
Chapter 2.1 Control Structures (Selection)
- Additional C Statements
Compound Assignment Operators in C++
Unary Operators ++ and --
SELECTION STATEMENTS (2)
Program Control Topics While loop For loop Switch statement
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Let’s all Repeat Together
switch Selection Structure
2.6 The if/else Selection Structure
Understanding Conditions
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Life is Full of Alternatives Part 3
Programming Fundamental
Presentation transcript:

111/18/2015CS150 Introduction to Computer Science 1 Announcements  I have not graded your exam yet

211/18/2015CS150 Introduction to Computer Science 1 Multiple Alternative Ifs if (condition1) statement1; else if (condition2) statement2; … else defaultstatement;

311/18/2015CS150 Introduction to Computer Science 1 Program  Write a program that displays a letter grade corresponding to an exam score A B C D 0-59 F

411/18/2015CS150 Introduction to Computer Science 1 Example if (salary < 0.00) tax = -1; else if (salary < ) tax = 0.15 * salary; else if (salary < ) tax = (salary )* ; else tax = salary * 0.26;

511/18/2015CS150 Introduction to Computer Science 1 What’s the difference? if (x >= 0) x = x + 1; else if (x >= 1) x = x + 2; if (x >= 0) x = x + 1; if (x >= 1) x = x + 2;

611/18/2015CS150 Introduction to Computer Science 1 Selection Structure Review  Write a program to solve the following problem: A police department needs to calculate fees for speeding tickets. The fees are as follows: SpeedFee 75 or greater$ $ $20.00

711/18/2015CS150 Introduction to Computer Science 1 Switch Statements  Another form of selection statement  Similar to if’s  Useful for lots of alternatives

811/18/2015CS150 Introduction to Computer Science 1 Example switch (watts) { case 25: life = 2500; break; case 40: case 60: life = 1000; break; case 75: case 100: life = 750; break; default: life = 0; }

911/18/2015CS150 Introduction to Computer Science 1 Form switch (selector) { case label1: statements1; break; case label2: statements2; break; … case labeln: statementsn; break; default: statements; }

1011/18/2015CS150 Introduction to Computer Science 1 Example switch (musical_note) { case ‘c’: cout << “do” << endl; break; case ‘d’: cout << “re” << endl; break; case ‘e’: cout << “mi” << endl; break; case ‘f’: cout << “fa” << endl; break; case ‘g’: cout << “sol” << endl; break; case ‘a’: cout << “la” << endl; break; case ‘b’: cout << “ti” << endl; break; default: cout << “An invalid note was read.”; }

1111/18/2015CS150 Introduction to Computer Science 1 Important!  Selector must be ordinal type  Each possible value is a separate case  break stops statements for case, otherwise continue with statements for next case

1211/18/2015CS150 Introduction to Computer Science 1 Example switch (color) { case ‘R’: case ‘r’: cout << “red” << endl; case ‘B’: case ‘b’: cout << “blue” << endl; case ‘Y’: case ‘y’: cout << “yellow” << endl; } What happens when color is ‘r’? ‘B’? ‘Y’?

1311/18/2015CS150 Introduction to Computer Science 1 Example switch (x > y) { case 1: cout << “x greater” << endl; break; case 0: cout << “y greater or equal” << endl; break; } Write as if statement

1411/18/2015CS150 Introduction to Computer Science 1 Questions  Can you write any switch statement as an if?  Can you write any if statement as a switch?

1511/18/2015CS150 Introduction to Computer Science 1 Problem  Write a switch statement to convert a character digit to an integer

1611/18/2015CS150 Introduction to Computer Science 1 Change to switch if (speed > 35) fee = 20.00; else if (speed > 50) fee = 40.00; else if (speed > 75) fee = 60.00;

1711/18/2015CS150 Introduction to Computer Science 1 Examples  Write an if statement that prints out the level of schooling. (0, none; 1 through 6, elementary; 7 through 8, middle school; 9 through 12, high school; > 12, college)  Write a switch statement to do the same

1811/18/2015CS150 Introduction to Computer Science 1 Write a Program Input an integer If the integer is positive, increment a variable poscount by 1. If the integer is negative, increment a variable negcount by 1. If neither, increment zerocount by 1.

1911/18/2015CS150 Introduction to Computer Science 1 Unary Operators ++ and --  ++ is increment operator x++; is the same as x = x + 1;  -- is decrement operator x--; is the same as x = x - 1;

2011/18/2015CS150 Introduction to Computer Science 1 Prefix and Postfix Unary ++ and -- PrefixPostfix k = --x;k =x--; k = ++x;k = x++; Increment/Assign value of x to decrement xk, then increment then assignor decrement x value of x to k

2111/18/2015CS150 Introduction to Computer Science 1 Example cout << “Value of i is” << i; cout << “Value of i++ is” << i++; cout << “Value of ++i is” << ++i; cout << “Value of --i is” << --i; cout << “Value of i-- is” << i--;

2211/18/2015CS150 Introduction to Computer Science 1 Program  Write a program that outputs the following: *****

2311/18/2015CS150 Introduction to Computer Science 1 count = 0; while (count < 5) { cout << “ ***** ” << endl; count++; } How many times (iterations) does loop run? Loops Loop Control Variable Initialize LCV Change the value of count

2411/18/2015CS150 Introduction to Computer Science 1 While loops while (logical expression is true) statement; while (logical expression is true) { statement1; statement2; … }

2511/18/2015CS150 Introduction to Computer Science 1 Key ingredients  Initialize MUST initialize loop control variable  Test The value of loop control variable is tested during each iteration of loop  Update Loop control variable is changed during each loop iteration If any one of these is missing or incorrect, your loop won’t run properly--not at all, too many/few times or infinitely.

2611/18/2015CS150 Introduction to Computer Science 1 Examples  Write a while loop that outputs each integer from 1 to 5  Write a program that inputs the following data for 5 students: name, id# and grade  Write a program that inputs the following data for a user specified number of students: name, id# and grade