CS114-009 Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages 50-73 for next.

Slides:



Advertisements
Similar presentations
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Advertisements

If Statements & Relational Operators Programming.
While Loops Programming. COMP102 Prog Fundamentals I: while Loops/Slide 2 Shortcut Assignments l C++ has a set of shortcut operators for applying an operation.
True or false A variable of type char can hold the value 301. ( F )
Computer Science 1620 Loops.
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 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
1 9/26/08CS150 Introduction to Computer Science 1 Logical Operators and if/else statement.
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.
Computer Science 1620 Accumulators. Recall the solution to our financial program: #include using namespace std; int main() { double balance = ;
1 CS 105 Lecture 4 Selection Statements Wed, Jan 26, 2011, 6:05 pm.
If Statements. COMP104 If / Slide 2 Three Program Structures * Sequence - executable statements which the computer processes in the given order * Choice.
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
Computer Science 1620 Programming & Problem Solving.
CS 1400 Jan 2007 Chapter 4, sections Relational operators Less than< Greater than> Less than or equal= Equal== Not equal!=
The If/Else Statement, Boolean Flags, and Menus Page 180
CSE202: Lecture 14The Ohio State University1 Arrays.
1 9/28/07CS150 Introduction to Computer Science 1 Logical Operators and if/else statement.
PRINCIPLES OF PROGRAMMING Revision. A Computer  A useful tool for solving a great variety of problems.  To make a computer do anything (i.e. solve.
Chapter 5: Control Structures II (Repetition)
CHAPTER 5: CONTROL STRUCTURES II INSTRUCTOR: MOHAMMAD MOJADDAM.
EGR 2261 Unit 5 Control Structures II: Repetition  Read Malik, Chapter 5.  Homework #5 and Lab #5 due next week.  Quiz next week.
True or False: Boolean Expression Boolean expression are expressions which evaluate to "true" or "false“ Primary operators to combine expressions: && (and),
1 EMT 101 – Engineering Programming Dr. Farzad Ismail School of Aerospace Engineering Universiti Sains Malaysia Nibong Tebal Pulau Pinang Week 4.
CS 114 – Class 02 Topics  Computer programs  Using the compiler Assignments  Read pages for Thursday.  We will go to the lab on Thursday.
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.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT3: Conditional Statements CS2311 Computer Programming.
CS1201: Programming Language 2 Recursion By: Nouf Almunyif.
1 Conditions Logical Expressions Selection Control Structures Chapter 5.
Quiz Answers 1. Show the output from the following code fragment: int a = 5, b = 2, c = 3; cout
Chapter 5: Control Structures II (Repetition). Objectives In this chapter, you will: – Learn about repetition (looping) control structures – Learn how.
# ACS 168 Structured Programming Using the Computer Chapter 2 Spring 2002 Prepared by Shirley White.
Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using.
While Loops Programming. COMP102 Prog Fundamentals I: while Loops/Slide 2 Shortcut Assignments l C++ has a set of shortcut operators for applying an operation.
Program Flow Control - Looping Addis Ababa Institute of Technology Yared Semu April 2012.
CS Class 05 Topics  Selection: switch statement Announcements  Read pages 74-83, ,
1 ELEC 206 Chapter 3 Control Structures 5-Step Problem Solving Methodology 1. State the problem clearly. 2. Describe the input and output. 3. Work a.
Control Structures Repetition or Iteration or Looping Part II.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
CS Class 08 Today  Exercises  Nested loops  for statement  Built-in functions Announcements  Homework #3, group solution to in-class.
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.
Control Structures RepetitionorIterationorLooping Part I.
Input a number #include using namespace std; int main() { int num; cout num; return 0; }
Overview Go over parts of quiz? Another iteration structure for loop.
Introduction to Loops Iteration Repetition Counting Loops Also known as.
Control Structures RepetitionorIterationorLooping Part I.
1 10/3/05CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
Think First, Code Second Understand the problem Work out step by step procedure for solving the problem (algorithm) top down design and stepwise refinement.
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.
CSE202: Lecture 5The Ohio State University1 Selection Structures.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 5: Control Structures II (Repetition)
C++ Programming: CS102 LOOP. Not everything that can be counted counts, and not every thing that counts can be counted. −Albert Einstein Who can control.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 23, 2005 Lecture Number: 11.
The Ohio State University
Basic concepts of C++ Presented by Prof. Satyajit De
Introduction to Computer Programming
Bill Tucker Austin Community College COSC 1315
Chapter 2 Assignment and Interactive Input
Engineering Problem Solving with C++, Etter/Ingber
Compound Assignment Operators in C++
Programming Funamental slides
CS1100 Computational Engineering
Programming Funamental slides
If Statements.
Control Structures Part 1
Life is Full of Alternatives
REPETITION Why Repetition?
Presentation transcript:

CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next class.

Class Exercise from last Thurs. What are the final values for e, f, g, and y? #include using namespace std; int main( ) { int a=2, b=4, c=8, d=5, e, f, g; double x = 2.5, y; e = (a + b * c) / (d + 12); f = x / 2; g = c % d; y = c / d + x; }

Questions to consider What if we used double instead of int for our variables in the microwave program?  Try it  Does the output change? What if min and sec are both a double, but time is still an int?  Does it still run?  Does it always work?

Comments on writing programs Everything is a combination of Sequence, Selection, and Iteration statements Program from last time, sequence only  Read / Read / Compute / Print Other than S/S/I, need to know  Variables – storage locations – today  Functions – next month  Arrays – March

Expressions statement; Sequence statements statement; No decisions Just perform one statement after another Selection and Iteration statements Make a decision to determine what to do (Which path? Another loop?) The decision is an “expression” which evaluates to either true or false statement; truefalse statement; truefalse

CS 114 Content (details) Algorithms Definition Tracing Writing Analyzing Efficiency Recursion Searching Sorting Language Implementation (using Visual Studio.NET 2005) Environment / compilation / debugging / resources / tools Sequence input output assign File I/O formatting Selection if switch Iteration for while Basic Elements variables operators expression functions arrays objects

If statement Syntax if ( expression ) statement ; // then clause else statement ; // else clause Expression determines path to take  If TRUE, does “then” clause  If FALSE, does the “else” clause (when it exists) Example if ( a < b ) cout << a << endl; else cout << b << endl; statement; truefalse

Need to understand expressions True or False? int a, b, c; a=5; b=3; c=10; ( b == c ) ( (a+b) < c) (b == (c / 3)) ( (b > c) || (c != 10) ) ( (a <= c) && (a == b) ) == equals != not equal <less than <= less than or equal to >greater than >= greater than or equal to &&logical and (both must be true) ||logical or (one must be true) !logical not (opposite of the expr)

Modifying Our First Program #include using namespace std; int main ( ) { int min, sec; cout > min; cout > sec; int time; if (min == 0) time = sec; else time = min * 60 + sec; cout << "total time is " <<time << endl; return 0; }

Class Exercises int main ( ) { int a=5, b=10, c=15; if ( a > (c%b) ) cout << “yes” << endl; else cout << “no” << endl; if ( (a+b) == c ) cout << “UA” << endl; if ( (b/a) < (c/a) ) cout << “AU” << endl; } int main ( ) { int a=2, b=4, c=8; if ( (b>1) && (c<10) ) if ( (a>c) || (a<b) ) cout << “UA” << endl; if ( (c%b) == a ) cout << “Roll” << endl; else cout << “Tide” << endl; if ( (a != 2) && (b != 4) ) cout << “Yes” << endl; else cout << “No” << endl; }

More on selection statements Selection Statements come in three flavors  if statements  switch statements  ?: operator (we will not use this in CS 114) We will focus on if statements for now  Can use { and } to group a block of statements together if (a == b) { c = a * a; b = b + 1; } else { a = a - 1; }

Watch out! int main( ) { int a = 5; if (a = 4) cout << “yes” << endl; else cout << “no” << endl; } Expression evaluates:  True is non-zero  False is zero C++ does the following  The expression “a = 4” is an assignment stmt  assigns a the value of 4  expression itself evaluates to value assigned (4)  4 is non-zero (TRUE)   this expression is true! Output: “yes” Syntax is correct  no compile errors Logic is wrong  a semantic error

Class Exercises What does the program at the right do? int main( ) { int a, b, c; cin >> a; cin >> b; cin >> c; int num; if (a >= b) num = a; else num = b; if (num < c) num = c; cout << num << endl; }

Exercise - Put sequence & selection together Write a C++ program that:  Reads in four integers from the user  Calculates an average for these four numbers  Prints out the average and the number of inputted values that were above the average (greater than or equal to)  Example #1 Input = 1, 2, 3, 4 Output = 2.5 and 2  Example #2 Input = 1, 1, 1, 9 Output = 3 and 1  Hint: use a “counter” to keep track of how many are above average

Quick check of the solution #include using namespace std; int main( ) { int a, b, c, d; cin >> a;// read in the four values cin >> b; cin >> c; cin >> d; double avg = (a + b + c + d) / 4.0;// average should be a real number int count = 0;// this is our counter if (a >= avg) count = count + 1;// increment for each above avg if (b >= avg) count = count + 1; if (c >= avg) count = count + 1; if (d >= avg) count = count + 1; cout << avg << " " << count << endl;// print results }

End of Class 04 Read pages 50 – 73 for next time. Make sure you can do today’s exercises.