1 CS161 Introduction to Computer Science Topic #8.

Slides:



Advertisements
Similar presentations
True or false A variable of type char can hold the value 301. ( F )
Advertisements

1 CIS Jan Overview Selection Statements –If Statement –Else –Nested If-Else –Switch Repetition Statements –While statement –For Statement.
1 9/26/08CS150 Introduction to Computer Science 1 Logical Operators and if/else statement.
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.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
1 10/11/06CS150 Introduction to Computer Science 1 do/while and Nested Loops.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
1 9/25/06CS150 Introduction to Computer Science 1 Nested Ifs, Logical Operators, exit() Page 194.
Control Structures Control structures control the flow of program execution. 3 types of control structures: sequence, selection.
The If/Else Statement, Boolean Flags, and Menus Page 180
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.
High-Level Programming Languages: C++
Outlines Chapter 3 –Chapter 3 – Loops & Revision –Loops while do … while – revision 1.
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
Lecture 5 Selection Control Structures Selection Control Structures Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor
Lecture 4 Introduction to Programming. if ( grade ==‘A’ ) cout
1 Chapter 9 Additional Control Structures Dale/Weems/Headington.
More on Input Output Input Stream : A sequence of characters from an input device (like the keyboard) to the computer (the program running). Output Stream.
CPS120: Introduction to Computer Science Decision Making in Programs.
Decision II. CSCE 1062 Outline  Boolean expressions  switch statement (section 4.8)
CS102 Introduction to Computer Programming Chapter 4 Making Decisions.
Flow of Control Part 1: Selection
CONTROLLING PROGRAM FLOW
Additional Control Structures. Chapter 9 Topics Switch Statement for Multi-way Branching Do-While Statement for Looping For Statement for Looping Using.
Previously Repetition Structures While, Do-While, For.
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.
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.
1 09/20/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
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 05 (Part III) Control Statements: Part II.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Selection Statements Selection Switch Conditional.
PROGRAM FLOW CHAPTER3 PART1. Objectives By the end of this section you should be able to: Differentiate between sequence, selection, and repetition structure.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
CS161 Topic #16 1 Today in CS161 Lecture #16 Prepare for the Final Reviewing all Topics this term Variables If Statements Loops (do while, while, for)
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 4: Making Decisions.
C++ Programming Lecture 7 Control Structure I (Selection) – Part II The Hashemite University Computer Engineering Department.
Chapter 5: Control Structures I (Selection). Objectives In this chapter you will: Learn about control structures Examine relational and logical operators.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 4 Making Decisions.
COIT29222 Structured Programming 1 COIT29222-Structured Programming Lecture Week 02  Reading: Textbook(4 th Ed.), Chapter 2 Textbook (6 th Ed.), Chapters.
Today in CS161 Lecture #5 Learn about… Data types (char, int, float) Input and Output (cin, cout) Writing our First Program Write the Inches to MM Program.
1 Programming in C++ Dale/Weems/Headington Chapter 9 Additional Control Structures (Switch, Do..While, For statements)
A First Book of C++ Chapter 4 Selection. Objectives In this chapter, you will learn about: –Relational Expressions –The if-else Statement –Nested if Statements.
Switch Selection Structure (L14) * General Form of the switch Statement * Details of switch Statement * Flowchart of switch Statement * cin.get * Character.
STRUCTURED PROGRAMMING Selection Statements. Content 2  Control structures  Types of selection statements  if single-selection statement  if..else.
Lecture 6 – Selection FTMK, UTeM – Sem /2014.
 2003 Prentice Hall, Inc. All rights reserved. 1 Basic C++ Programming.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
 By the end of this section you should be able to: ◦ Differentiate between sequence, selection, and repetition structure. ◦ Differentiae between single,
LECTURE # 7 : STRUCTURED PROGRAMMING Selection Statements Tr.Hadeel.
A First Book of C++ Chapter 4 Selection.
Branching statements.
Chapter 3 Selection Statements
REPETITION CONTROL STRUCTURE
CS161 Introduction to Computer Science
Chapter 4: Making Decisions.
Variables A piece of memory set aside to store data
Chapter 2.1 Control Structures (Selection)
Chapter 4: Making Decisions.
Introduction to C++ Programming
Chapter 4: Control Structures I (Selection)
2.6 The if/else Selection Structure
The switch Statement When we want to compare a variable against several values to see which one it has, we can use the switch statement: switch (status)
C++ Programming Lecture 7 Control Structure I (Selection) – Part II
Presentation transcript:

1 CS161 Introduction to Computer Science Topic #8

CS161 Topic #82 Today in CS161 Review for Midterm –Variable Definitions –Conditional Expressions (if, else) –Loops (while, do while, for) –Sample Questions Switch Statements Questions?

CS161 Topic #83 Review for Midterm The midterm is a closed book and closed notes exam There will be questions asking you to determine the output of a program, indicate what is wrong with a program, evaluate conditional expressions, and write program fragments.

CS161 Topic #84 Understand these concepts: The difference between designing an algorithm and implementing C++ code. The basic data types: int, float, and char. The difference between dividing integers versus floating point numbers. The if/else control structures. The difference between relational operators, equality operators, and logical operators. The while, do-while, for loops

CS161 Topic #85 Understand these concepts: What does a loop allow us to do? Where do we put loops in our program? What is a C++ statement? How do you read information from the keyboard? How do you write information to the screen? Why would you use a while loop rather than a do- while loop?

CS161 Topic #86 Sample Questions: The following is supposed to output all positive odd numbers less than 10. It contains some errors. What are they and how can they be corrected? int x = 1; while (x != 10) { x += 2; cout << x << endl; }

CS161 Topic #87 Practice Questions.... Write a for loop to output all positive odd numbers less than 10, starting at 1 int i;//loop control variable for (i = 1; i < 10; i = i + 2) cout << i;

CS161 Topic #88 Sample Questions: What is the output of the following program fragment? for (i = -1; i <= 5; i = i + 1) cout << 2*i; cout << endl; How would you fix the appearance of the output? cout << 2*i << ‘\n’; or, cout << setw(5) << 2*i;

CS161 Topic #89 Sample Questions: Change the following while loop to a do- while loop:int i;cin >> i; while (i < 20) {if (i < 20) cout << i << ‘ ‘;do { i += 5; cout << i << ‘ ‘; } i += 5; } while (i < 20); answer:

CS161 Topic #810 Understand these concepts: What data type would you use to store –your age –your gpa –your first name’s initial –a test score (A, B, C) Write C++ code to display each upper case letter of the alphabet

CS161 Topic #811 Understand these concepts: What will be the output for the following: if (x >= 0.0) if (x < ) { y = 2*x; if (x <= 300) x = x/10; } else y = 3*x; else y = x; cout << x << “ and “ << y << endl;

CS161 Topic #812 Understand these concepts: What will be the output for the following: for (k = 2; k <= 4; k = k + 1) { for (j = 5; j <= 8; j = j + 1) cout << k+j; cout << endl; }

CS161 Topic #813 Understand these concepts: What is the escape sequence that is the same as a carriage return? Where should we include comments? Name some examples of whitespace Write a cout statement to display your name Which of the following are not legal integers: , Explain why it is important to prompt

CS161 Topic #814 Understand these concepts: Write a small program to read in two integer values and then display them in numerical order, regardless of the order in which they are entered: int main() { int first, second; cout << “Enter 2 whole numbers: “; cin >> first >> second; if (first <= second) cout << first << “ “ << second << endl; else cout << second << “ “ << first << endl; return 0; }

CS161 Topic #815 Switch Statements Another C++ control statement is called the switch statement It allows you to pick the statements you want to execute from a list of possible statements, instead of just two different alternatives (as is available with an if/else) or a set of nested if/elses! It allows for multi-way decisions.

CS161 Topic #816 Switch Statements char grade; cout << "Enter the grade..." << endl; cin >> grade; switch (grade) { case 'A': cout << "Excellent" << endl; cout << “Keep up the good work!”; break; case 'B': cout << "Very Good"; break; case 'C': cout << "Passing"; break; case 'D': case 'F': cout << "Too Bad"; break; default : cout << "No match was found...try again"; break; }

CS161 Topic #817 Switch Statements C++ provides a "default" clause so that if there isn't a match something is done. If the default is left off...and there is no match...no action takes place at all. When a case statement is executed, the value of Grade is checked and then depending on which of the cases it matches -- the statement following the colon for that case will be executed.

CS161 Topic #818 Switch Statements To exit from a switch statement...use break. Unlike Pascal, with C++ once you have a match... It will fall thru (ignoring any additional case or default labels that are encountered and continue executing code until a break is encountered. If you intend to use “fall thru”, then you should have a comment saying that the drop through is intentional

CS161 Topic #819 Switch Statements The rule of thumb is that you can use these to switch on integers and characters. It is not permitted to use the switch with floating point types or a string of characters. The type of the expression following a switch keyword must be the same as the expressions following each case keyword....and no two expressions following the case keywords can be the same.

CS161 Topic #820 What Fall Thru means... int count; cout << "Please enter the number of asterisks:"; cin >> count; switch (count) {//these { } are mandatory! case 1: cout << "*"; //intentional drop thru case 2: cout << "**"; case 3: cout << "***"; case 4: cout << "****"; default: cout << "!"; } cout << endl;

CS161 Topic #821 The CORRECT version.... int count; cout << "Please enter the number of asterisks:"; cin >> count; switch (count) {//these { } are mandatory! case 1: cout << "*";break; case 2: cout << "**";break; case 3: cout << "***";break; case 4: cout << "****";break; default: cout << "!";break; } cout << endl;