CS114-009 Class 05 Topics  Selection: switch statement Announcements  Read pages 74-83, 110-116, 118-138.

Slides:



Advertisements
Similar presentations
COMPUTER PROGRAMMING I Essential Standard 5.02 Understand Breakpoint, Watch Window, and Try And Catch to Find Errors.
Advertisements

True or false A variable of type char can hold the value 301. ( F )
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 4 – Introducing Algorithms, Pseudocode and.
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
1 Lab Session-II CSIT 121 Fall 2000 Visual Studio Introduction Lab-1 Explanation and Demos Debugging Tips How to add new files to your projects How many.
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
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.
The IDE (Integrated Development Environment) provides a DEBUGGER for locating and correcting errors in program logic (logic errors not syntax errors) The.
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
1 Midterm Review COMP 102. Tips l Eat a light meal before the exam l NO electronic devices (including calculators, dictionaries, phones, pagers, etc.)
The If/Else Statement, Boolean Flags, and Menus Page 180
C++ for Engineers and Scientists Third Edition
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
Debugging Logic Errors CPS120 Introduction to Computer Science Lecture 6.
Computer Programming and Basic Software Engineering 4. Basic Software Engineering 1 Writing a Good Program 4. Basic Software Engineering.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
Chapter 5: Control Structures II (Repetition)
CHAPTER 5: CONTROL STRUCTURES II INSTRUCTOR: MOHAMMAD MOJADDAM.
Debugging Projects Using C++.NET Click with the mouse button to control the flow of the presentation.
A1 Visual C++.NET Intro Programming in C++ Computer Science Dept Va Tech August, 2002 © Barnette ND & McQuain WD 1 Quick Introduction The following.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 4 – Wage Calculator Application: Introducing.
CS 114 – Class 02 Topics  Computer programs  Using the compiler Assignments  Read pages for Thursday.  We will go to the lab on Thursday.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
CSE 332: C++ debugging in Eclipse C++ Debugging in Eclipse We’ve now covered several key program features –Variable declarations, expressions and statements.
Chapter 02 (Part III) Introduction to C++ Programming.
Testing and Debugging Version 1.0. All kinds of things can go wrong when you are developing a program. The compiler discovers syntax errors in your code.
Flow of Control Part 1: Selection
1 Κατανεμημένες Διαδικτυακές Εφαρμογές Πολυμέσων Γιάννης Πετράκης.
CSE1222: Lecture 6The Ohio State University1. Common Mistakes with Conditions (1)  Consider the following code: int age(26); if (age = 18) { cout
Data Structures and Debugging Dr. Nancy Warter-Perez June 18, 2003.
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.
CSE 332: C++ execution control statements Overview of C++ Execution Control Expressions vs. statements Arithmetic operators and expressions * / % + - Relational.
Chapter 8 Repetition Statements. Introduction Iteration - process of looping or the repetition of one or more statements Loop body - the statement, or.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 21, 2005 Lecture Number: 10.
CSE 232: C++ debugging in Visual Studio and emacs C++ Debugging (in Visual Studio and emacs) We’ve looked at programs from a text-based mode –Shell commands.
Logic Our programs will have to make decisions in terms of what to do next –we refer to the decision making aspect as logic Logic goes beyond simple if.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X1 Chapter 3 Control Statements.
1 EMT 101 – Engineering Programming Dr. Farzad Ismail School of Aerospace Engineering Universiti Sains Malaysia Nibong Tebal Pulau Pinang Week 2.
CS Class 08 Today  Exercises  Nested loops  for statement  Built-in functions Announcements  Homework #3, group solution to in-class.
Fundamental Programming Fundamental Programming for Loops.
CSC1201: Programming Language 2 Lecture 1 Level 2 Course Nouf Aljaffan (C) CSC 1201 Course at KSU1.
CS Class 03 Topics  Sequence statements Input Output Assignment  Expressions Read pages Read pages 40 – 49 for next time.
PEG200/Saidatul Rahah 1.  Selection Criteria › if..else statement › relational operators › logical operators  The if-then-else Statement  Nested if.
Control Structures RepetitionorIterationorLooping Part I.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 3 – Inventory Application: Introducing Variables,
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 3 – Inventory Application: Introducing Variables,
1 CS161 Introduction to Computer Science Topic #8.
Debugging tools in Flash CIS 126. Debugging Flash provides several tools for testing ActionScript in your SWF files. –The Debugger, lets you find errors.
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Conditions - compare the values of variables, constants and literals using one or more relational.
Variables  A piece of memory set aside to store data  When declared, the memory is given a name  by using the name, we can access the data that sits.
 2003 Prentice Hall, Inc. All rights reserved. 1 Basic C++ Programming.
CSE202: Lecture 5The Ohio State University1 Selection Structures.
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 5: Control Structures II (Repetition)
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Condition – any expression that evaluates to true/false value Relational operators are BINARY.
The Ohio State University
Basic concepts of C++ Presented by Prof. Satyajit De
Chapter 3 Control Statements
CMPT 201.
Chapter 2 Assignment and Interactive Input
Computer Programming I
Variables A piece of memory set aside to store data
Chapter 2.1 Control Structures (Selection)
CSC113: Computer Programming (Theory = 03, Lab = 01)
Chapter 3 Control Statements Lecturer: Mrs Rohani Hassan
Chapter 7 Conditional Statements
Presentation transcript:

CS Class 05 Topics  Selection: switch statement Announcements  Read pages 74-83, ,

Quick note: Comments In C++ to add comments to your program for readability:  If comment the rest of a line use // int a; //this variable hold the age  Or use /* and */ to surround comment (this is good if comment > 1 line long) /* This program was written by me and it is due on Monday */

Selection statements Two alternatives decision True clause False clause

Nested IF’s decision True clause False clause decision True clause False clause decision True clause False clause decision True clause False clause And so on… Multiple alternatives

Example int num; cin>>num; if (num<10){ cout<<"SMALL"<<endl; if (num%2==0){ cout<<"YEA"<<endl; if (num>5){ cout<<“W00T!"<<endl; if (num==8) cout<<num/3<<endl; } else cout<<"DUH HUH"<<endl; } else cout<<"BOO"<<endl; } else cout<<num<<endl; Note that not all decisions have an else. Trace this code with an input of 6. What is the output? No else

Class Exercise Copy and paste the following C++ file into a new project:  On our class web page under “Sample Programs” get “Sept. 4.cpp” Complete the program so that it will handle the numbers 1 thru 4 the same way that it handles 5 thru 10.  Delete the line cout<<"finish the code for num = 1 to 4\n"; and insert the appropriate code in its place.

Quick Note: The else if statement Rather than using a series of nested if-else clauses you can use an else if statement. if (x == 0) … else if (x == 1) … else if (x == 2) … else … //use as a catch-all Example: printing out the month corresponding to a number  User enters a 1, January is printed out.

Example of else if int month; cin>>month; if(month==1) cout<<"Jan."<<endl; else if (month == 2) cout<<"Feb."<<endl;. else if (month == 12) cout<<"Dec."<<endl; else cout<<"What month?"<<endl;

Switch decision Case 1 Case 2 Case 3 Case 4 Case 5 Case 6 Multiple alternatives

Switch statement Format: switch (variable) { case 1: statement(s) break; case 2: statement(s) break; … default: statement(s) break; // default case is optional, // but it’s smart! } Variable must be int or char break: jumps to end of the switch statement Example: int month; cin >> month; switch (month) { case 1: cout << "Jan" << endl; break; case 2: cout << "Feb” << endl; break; case 3: cout << “Mar" << endl; break; … }

Selection - FYI These are equivalent: If (x==5) cout<<"yes\n"; else cout<<"no\n"; switch (x){ case 5: cout<<"yes\n"; break; default: cout<<"no\n"; }

Break What happens if you don’t have the break statement? int month; cin >> month; switch (month) { case 1: cout << "Jan" << endl; break; case 2: cout << "Feb” << endl; case 3: cout << “Mar" << endl; break; … }

Class Exercise Write a C++ program that reads in a single integer telling your final position in a contest. Use a switch statement to generate the following messages:  if you finished first: “Gold medal”  if you finished second: “Silver medal”  if you finished third: “Bronze medal”  if you finished below third (the default case): “Nice try”

Increment( ++ ) and Decrement( -- ) Four ways to add one to a variable: a = a + 1; a++; ++a; a += 1; Other useful operators: += *= -= /= %= Important: a++ and ++a differ: int y, z=5; y = z++; // inc after use cout << y << endl: cout << z << endl; // prints 5 and 6 y = ++z; // inc before use cout << y << endl; cout << z << endl; // prints 7 and 7

Using a debugger Things go wrong  Not everyone will get their program to run perfectly the first time.  The compiler will catch “syntax” errors, but it will not catch “logic” errors. Syntax error  double int time; Logic error  time = min * 37 + sec; Need a process to resolve logic errors  Study the code  Extra print statements  Debugger  Change random lines

Using the debugger Three handy tools  Breakpoints Stop at a certain point Look around Do things make sense?  Step through code Walk through it and watch what happens Note: “Step over” I/O calls!  Watch on variable Track changes to a variable Breakpoint:  Right click on the line: time = min * 60 + sec;  Select “Breakpoint” -> “Insert Breakpoint” Red dot appears by line  Select “Debug/Start Debugging” or hit the F5 key Program should run and prompt you for two inputs Halts (yellow arrow by line where breakpoint was inserted) Examine the bottom left window (make sure “locals” is selected) F11 (or “Step Into”) to execute the line of code  Re-examine values in “locals” for updates to variables F5 (or “Continue”) continues execution

Use the debugger to answer: Questions:  What is the value of an integer variable right after it is declared?  What is the value of a real (double) variable right after it is declared?  What is the result of the following code? int a, b, c; b = 5; c = a + b; cout << c << endl; More questions:  What if you assign a real value to an integer? int a; a = 3.25; cout << a << endl;  What if you assign an integer variable to a real value? double a; a = 312; cout << a << endl;

End of Class 05  Read pages 74-83, ,