If Statements.

Slides:



Advertisements
Similar presentations
If Statements & Relational Operators Programming.
Advertisements

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.
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
1 9/15/06CS150 Introduction to Computer Science 1 Combined Assignments, Relational Operators, and the If Statement.
1 9/24/07CS150 Introduction to Computer Science 1 Relational Operators and the If Statement.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
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
How Create a C++ Program. #include using namespace std; void main() { cout
1 CS150 Introduction to Computer Science 1 Relational Operators and the If Statement 9/22/08.
Thursday, December 14, 2006 “Would you tell me, please, which way I ought to go from here?” “That depends a good deal on where you want to get to,” said.
The If/Else Statement, Boolean Flags, and Menus Page 180
What is the out put #include using namespace std; void main() { int i; for(i=1;i
Loops Programming. COMP104 Lecture 9 / Slide 2 Shortcut Assignment l C++ has a set of operators for applying an operation to a variable and then storing.
Administrative MUST GO TO CORRECT LAB SECTION! Homework due 11:59pm on Tuesday. 25 points off if late (up to 24 hours) Cannot submit after 11:59pm on Wednesday.
For Loops Programming. COMP102 Prog Fundamentals I: for Loops/Slide 2 The for Statement condition action true false initialization update.
VARIABLES, TYPES, INPUT/OUTPUT, ASSIGNMENT OPERATION Shieu-Hong Lin MATH/CS Department Chapel.
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.
1 Conditions Logical Expressions Selection Control Structures Chapter 5.
CS31: Introduction to Computer Science I Discussion 1A 4/9/2010 Sungwon Yang
Quiz Answers 1. Show the output from the following code fragment: int a = 5, b = 2, c = 3; cout
Current Assignments Homework 3 is due tonight. Iteration and basic functions. Exam 1 on Monday.
CSE1222: Lecture 6The Ohio State University1. Common Mistakes with Conditions (1)  Consider the following code: int age(26); if (age = 18) { cout
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.
Conditional Structures UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) ADNAN BABAR MT14028 CR
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Lecture #6 OPERATORS AND ITS TYPES By Shahid Naseem (Lecturer)
1 Chapter 4: Basic Control Flow ► Chapter Goals  To be able to implement decisions using if statements  To understand statement blocks  To learn how.
CSC1201: Programming Language 2 Lecture 1 Level 2 Course Nouf Aljaffan (C) CSC 1201 Course at KSU1.
C++ Basics. Compilation What does compilation do? g++ hello.cpp g++ -o hello.cpp hello.
Lecture 7: Making Decisions Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
Input a number #include using namespace std; int main() { int num; cout num; return 0; }
If Statements Programming. COMP104 Lecture 7 / Slide 2 Review: Rules for Division l C++ treats integers different than doubles. 100 is an int. l 100.0,
Fundamental Programming Fundamental Programming More Expressions and Data Types.
Algorithms JPC and JWD © 2002 McGraw-Hill, Inc. 2 Algorithms 2 An Algorithm is a finite set of precise instructions for performing a computation or for.
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.
The Ohio State University
Branching statements.
Basic concepts of C++ Presented by Prof. Satyajit De
Introduction to Computer Programming
Chapter 3 Selection Statements
CHAPTER 4 REPETITION CONTROL STRUCTURE / LOOPING
Bill Tucker Austin Community College COSC 1315
LESSON 4 Decision Control Structure
A mechanism for deciding whether an action should be taken
Chapter 2 Assignment and Interactive Input
Engineering Problem Solving with C++, Etter/Ingber
Introduction to C++ October 2, 2017.
Programming Fundamentals
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.
לולאות קרן כליף.
Bools & Ifs.
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.
Starting Out with C++: From Control Structures through Objects
Bools & Ifs.
Repetition Control Structure
Flow Control Statements
Selection Control Structure
Summary Two basic concepts: variables and assignments Basic types:
By Hector M Lugo-Cordero September 3, 2008
Life is Full of Alternatives
Understanding Conditions
Fundamental Programming
Life is Full of Alternatives
(Dreaded) Quiz 2 Next Monday.
Branching statements Kingdom of Saudi Arabia
Selection Control Structure
Programming Fundamental
Presentation transcript:

If Statements

Boolean Expressions Bools are a data type which take on two values: 1. true (1) 2. false (0)

Boolean Expressions Bools can be used in a variety of expressions to express different logical statements. a && b – true if both a and b are true a || b – true if at least one of a and b are true !a - true if a is false Other expressions produce bools: a < b – true if a is less than b a > b – true if a is greater than b a <= b – true if a is less than or equal to b a >= b – true if a is greater than or equal to b a == b – true if a equals b a != b – true if a does not equal b

Boolean Expressions Try evaluating these expressions! In the following expressions a is true, b is false, c is 0, d is 1. 1. a && b 2. (b || b) && a 3. b || (b && a) 4. !a == b 5. !(b || (!a)) 6. (a && b) || (c < d) 7. a + b 8. -2 < -1 < 1

Conditionals Sometimes we want to make choices. If statements enable that! int money = 10; if (money < 5) {cout << “lentils for dinner” << endl;} else if (money < 10) {cout << “burgers for dinner” << endl;} else {cout << “lobsters for dinner” << endl;}

Basic Syntax if (condition) { ... } else if (condition) } else }

Sample Program #include <iostream> using namespace st int main() { double x = 0; cout << “Please input a number: \n”; cin >> x; if (x < 0) {cout << “x is negative”;} else if (x < 1) {cout << “x >= 0 and x <= 1”;} else {cout << “x >= 1”;} }

Problems using if statements 1. Write an if statement that will print out “Yes” if at least one of the numbers a, b, and c are positive. Base Code: If (_____) cout << “Yes”; 2. You are given an integer a. Write an if statement which will print out “fizz” if a is divisible by 3, “buzz” if a is divisible by 5 and “fizzbuzz” if a is divisible by both 3 and 5. (Hint: use % to check divisibility)

Gotchas Assigning instead of testing for equality. What happens in the following statement? int a = 0; if (a = 1) cout << “a equals 1”; ”a = 1” sets a to 1, and then returns the value of a. So it prints when it should not

Challenge Program What does the following program output? What are the values of a and b after if statement? int a = 0; bool a = 0, b = 1; if (a = b) cout << “1: true\n”; else cout << “1: false\n”; if (a = 0) cout << “2: true\n”; cout << “2: false\n”; if (b = a) cout << “3: true\n”; cout << “3: false\n”;