Agenda Exam #1 Review Modulus Conditionals Boolean Algebra Reading: Chapter 4.1-4.6 Homework #5.

Slides:



Advertisements
Similar presentations
Lecture 2 Introduction to C Programming
Advertisements

 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
Introduction to C Programming
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 4- 1.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
Flow control 1: if-statements (Liang 72-80) if(radius < 0) { System.out.println(“cannot get area: radius below zero”); } else { double area = radius *
The If/Else Statement, Boolean Flags, and Menus Page 180
Introduction to C Programming
Intro to Robots Conditionals and Recursion. Intro to Robots Modulus Two integer division operators - / and %. When dividing an integer by an integer we.
 Decision making statements Decision making statements if statement if...else statement Nested if...else statement (if...elseif....else Statement) 
What is RobotC?!?! Team 2425 Hydra. Overview What is RobotC What is RobotC used for What you need to program a robot How a robot program works Framework.
Programming Logic and Design Fourth Edition, Introductory
CPS120: Introduction to Computer Science Decision Making in Programs.
1 Conditions Logical Expressions Selection Control Structures Chapter 5.
Chapter 4 Selection Structures: Making Decisions.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Week 4 - Monday.  What did we talk about last time?  Wrapper classes  if statements.
CS102 Introduction to Computer Programming Chapter 4 Making Decisions.
Flow of Control Part 1: Selection
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
Making Decisions. 4.1 Relational Operators Used to compare numbers to determine relative order Operators: > Greater than < Less than >= Greater than.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 2: Variables & Data Types.
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
If Statements If statements: allow the computer to make a decision Syntax: if (some condition) { then do this; } else { then do that; } no semicolons on.
Chapter 4 Making Decision Csc 125 C++ programming language Fall 2005.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4: Making Decisions.
Conditional Structures UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) ADNAN BABAR MT14028 CR
Copyright 2003 Scott/Jones Publishing Making Decisions.
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 © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Python Conditionals chapter 5
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
Chapter Making Decisions 4. Relational Operators 4.1.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 4 Making Decisions.
Decision Structures, String Comparison, Nested Structures
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
Starting Out With Java 5 (Control Structures to Objects) Chapter 3 By Tony Gaddis Copyright © 2005 Pearson Addison-Wesley. All rights reserved.
CPS120: Introduction to Computer Science Decision Making in Programs.
Student Q and As from 5.1 – 5.7, 5.11 Students of CS104, Fall 2013 (and me)
CPS120: Introduction to Computer Science Decision Making in Programs.
Programming Logic and Design Fourth Edition, Comprehensive Chapter 5 Making Decisions.
Chapter 2: Fundamental Programming Structures in Java Adapted from MIT AITI Slides Control Structures.
Chapter 3. Control Structure C++ Programing. Conditional structure C++ programming language provides following types of decision making statements. Click.
 2003 Prentice Hall, Inc. All rights reserved Basics of a Typical C++ Environment C++ systems –Program-development environment –Language –C++
CSE202: Lecture 5The Ohio State University1 Selection Structures.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
IF STATEMENTS AND BOOLEAN EXPRESSIONS. BOOLEAN EXPRESSIONS Evaluate to a value of true or false Use relational or equivalence operators Boolean operators.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
 Type Called bool  Bool has only two possible values: True and False.
The if…else Selection Statement
Chapter 4: Making Decisions.
IF statements.
The Selection Structure
Selection CIS 40 – Introduction to Programming in Python
Conditions and Ifs BIS1523 – Lecture 8.
Conditions and Boolean Expressions
Java Programming Control Structures Part 1
Selection Statements.
Summary Two basic concepts: variables and assignments Basic types:
Chapter 3: Selection Structures: Making Decisions
Boolean Expressions to Make Comparisons
CS2011 Introduction to Programming I Selections (I)
Chapter 3: Selection Structures: Making Decisions
The Selection Structure
Agenda Warmup Review Finish 1.2 Assignments Lesson 1.3 (If Statements)
Presentation transcript:

Agenda Exam #1 Review Modulus Conditionals Boolean Algebra Reading: Chapter Homework #5

Modulus or Remainder: % You can think of this as the remainder of integer division 5 % 2 = 1 5 / 2 = 2 If the remainder of dividing two numbers is 0, the first number is divisible by the 2 nd. Negative numbers may not work the same in all languages Division by 0 is still an issue.

How is the remainder operator useful? 1) Maybe you are printing out tabular data and want to highlight every 2 nd row. If the current row's index is divisible by 2, the remainder will be 0. You can do the same for every 3 rd row, or every 10 th row...whatever 2) Extract the rightmost digit(s) from a number. 142 % 10 = % 100 = 42 3) Time. If you add 4 hours to 9:00, you get 1:00. (9 + 4) % 12 = 1

Conditional / If statement Often you need to make decisions in a program based on user input, calculated values, etc. To do this, we use an if statement. Conceptually, that is: If some condition is met Then do this Else do something else

Use Flow Charts to understand Program Flow DogFlowChart.jpg

If statement In C++, you write an if statement like this: if (condition) { // do something }

If-Else statement if (condition) { // do something } else { // do something else }

Chained conditionals if (condition) { // do something } else if (another condition) { // do something different } else if (another condition) { // do another something } else { // do something else }

Chained conditionals As soon as a condition is met, program flow moves into the corresponding { … }. Once the statements inside the curly brackets are complete, program flow moves to the line after the entire if-elseif-else block.

Boolean Conditions Remember, boolean is true or false. Is 1 equal to 1? True. Is 1+1 equal to 2? True. Is 2 greater than 3? False.

Boolean Conditions These are C++ comparison operators that give you boolean values x == y // Does x equal y? x != y // Does x not equal y? x > y // Is x greater than y? x < y // Is x less than y? x >= y // Is x greater than or equal to y? x <= y // Is x less than or equal to y?

Equals vs Assignment = is for assignment == is for testing if two expressions are equal These are easy to confuse or typo. It is also VERY difficult to debug. If your program compiles but is not working the way you expect, this is one of the first things you should check.

Expressions in conditionals You are not limited to variables in conditions. You can use expressions. if ( sin(x) > 1.0) {…} if (x + 5 > 10) {…} if (x + 5 > y + 2) {…}

Comparing data types You can only compare an expression to another expression with the same data type.

Nested if statements Inside an if statement, you can put more code. You can also put if statements inside if statements! if (userSelection == 1) { if (option == 1) { // do stuff }

User Input Validation If you have asked your user to enter a number, they might not follow your instructions. They might enter letters instead. To confirm that the user has entered a valid number, there is a function you can use. cin.good() will return true if the input is valid and false if the input is invalid.

User Input Validation int myNum; cin >> myNum; if (cin.good()) { //Input was valid } else { //Input was invalid }

Practice 1) Write a program that gives the user options to choose from 2) Edit a program you already wrote to validate user input 3) Make a text based adventure game 4) Decide what to do based on the weather outside 5) Make a flow chart for anything you like and then write the code to fit it

Homework #5 Posted online