1 CS 1430: Programming in C++. 2 IF Statement if (cond) statement //Next statement if (cond) { statement1 statement2 … } //Next statement.

Slides:



Advertisements
Similar presentations
1 Demo Reading Assignments Important terms & concepts Fundamental Data Types Identifier Naming Arithmetic Operations Sample Programs CSE Lecture.
Advertisements

C++ Basics Prof. Shermane Austin. Learning Programming Language Basics Data Types – simple Expressions Relational and Logical Operators Conditional Statements.
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
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.
1 10/11/06CS150 Introduction to Computer Science 1 do/while and Nested Loops.
CS 1400 Chapter 4, sections Relational operators Less than< Greater than> Less than or equal= Equal== Not equal!=
Chapter 6 Control Structures.
1 CS 105 Lecture 4 Selection Statements Wed, Jan 26, 2011, 6:05 pm.
1 9/08/06CS150 Introduction to Computer Science 1 Arithmetic Operators.
1 9/25/06CS150 Introduction to Computer Science 1 Nested Ifs, Logical Operators, exit() Page 194.
CS 1400 Jan 2007 Chapter 4, sections Relational operators Less than< Greater than> Less than or equal= Equal== Not equal!=
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 CIS Jan Overview Evolution of C++ Programming Style Syntax & Semantics Comments & White Space Data Types Variables & Declarations cout.
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.
1 CS 105 Lecture 5 Logical Operators; Switch Statement Wed, Feb 16, 2011, 5:11 pm.
Admin Office hours 2:45-3:15 today due to department meeting if you change addresses during the semester, please unsubscribe the old one from the.
If Statements & Relational Operators, Part 2 Programming.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
Quiz 1 Exam 1 Next Week. Nested if Statements if (myGrade >= 80) if (myGrade >= 90) cout
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
© Copyright 2013 by Pearson Education, Inc. All Rights Reserved.1 Chapter 3 Selections.
Selection Statements in C++ If Statement in C++ Semantics: These statements have the same meaning as in the algorithmic language. 2- Two way selection:
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.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
1 CS 1430: Programming in C++. 2 Input: Input ends with -1 Sentinel-Controlled Loop Input: Input begins with.
Chapter 4 Selection Structures: Making Decisions.
1 CS 1430: Programming in C++. 2 Literal Values Literal values of int Literal values of float
CS31: Introduction to Computer Science I Discussion 1A 4/9/2010 Sungwon Yang
Basic Of Computer Science
CONTROLLING PROGRAM FLOW
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 21, 2005 Lecture Number: 10.
Making Decisions (True or False) Relational Operators >greater than =greater than or equal to
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X1 Chapter 3 Control Statements.
CSC1201: Programming Language 2 Lecture 1 Level 2 Course Nouf Aljaffan (C) CSC 1201 Course at KSU1.
C++ Programming, Namiq Sultan1 Chapter 3 Expressions and Interactivity Namiq Sultan University of Duhok Department of Electrical and Computer Engineerin.
Lecture 7: Making Decisions Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
Selection-making Decisions Selection allows you to choose between two or more possible program flow --- it lets you make decisions in your program. Examples.
Before we get started…. First, a few things… Weighted Grading System Programming Style Submitting your assignments… The char and string variable types.
1 CS 1430: Programming in C++. 2 Find Max, Min, Average of m Sections Max, Min and Average of each section Max, Min and Average of all sections together.
1 CS 1430: Programming in C++. Quiz Functions Function Prototype float sqrt(float x); Function name, type, parameter and parameter type Function.
1 CS 1430: Programming in C++. 2 Input: Input ends with -1 Sentinel-Controlled Loop Input: Input begins with.
LESSON 4 Decision Control Structure. Decision Control Structures 1. if statement 2. if-else statement 3. If-else-if statement 4. nested if statement 5.
1 CS1430: Programming in C++ Section 2 Instructor: Qi Yang 213 Ullrich
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Conditions - compare the values of variables, constants and literals using one or more relational.
2/19/2016IT 279, Chung-Chih Li1 Branching Condition Statement list 1 T F Statement list 2 Condition Statement list T F.
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.
Lecture 7 Computer Programming -1-. Conditional Statements 1- if Statement. 2- if ….. else Statement. 3- switch.
1 Functions Function Prototype float sqrt(float x); Function name, type, parameter and parameter type Function Definition float sqrt(float x) { // compute.
CS 1430: Programming in C++.
Chapter 4 Repetition Statements Program Development and Design Using C++, Third Edition.
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Condition – any expression that evaluates to true/false value Relational operators are BINARY.
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
Decision making If.. else statement.
Chapter 3 Selection Statements
Chapter 3 Control Statements
CS 1430: Programming in C++ No time to cover HiC.
Decisions Chapter 4.
CS 1430: Programming in C++ Turn in your Quiz1-2 No time to cover HiC.
CS 1430: Programming in C++ No time to cover HiC.
Decision making If statement.
CS 1430: Programming in C++.
Summary Two basic concepts: variables and assignments Basic types:
Chapter 4: Control Structures I (Selection)
DeMorgan's & related Laws
Fundamental Programming
COMS 261 Computer Science I
CS 1430: Programming in C++ No time to cover HiC.
Presentation transcript:

1 CS 1430: Programming in C++

2 IF Statement if (cond) statement //Next statement if (cond) { statement1 statement2 … } //Next statement

3 IF - Else if (cond) statement1 else statement2 //Next statement if (cond) { statement11 statement12... } else { statement21 statement22... } //Next statement

4 IF – Else if (cond1) statement block1 else if (cond2) statement block2 else statement block3 //Next statement

5 Example int score; char grade; cin >> score; if (score >= 90) grade = 'A'; else if (score >= 80) grade = 'B'; else if (score >= 70) grade = 'C'; else if (score >= 60) grade = 'D'; else // NO if here! grade = 'F'; cout << “Your Grade: ” << grade; // The order is crucial: semantics!

6 Different Order int score; char grade; cin >> score; if (score >= 80) grade = ‘B'; else if (score >= 90) grade = ‘A'; else if (score >= 70) grade = 'C'; else if (score >= 60) grade = 'D'; else // NO if here! grade = 'F'; cout << “Your Grade: ” << grade; // You never get ‘A’!

7 Different Way if (score >= 80 && score < 90) grade = ‘B’; if (score >= 90) grade = ‘A’; if (score >= 70 && score < 80) grade = ‘C’; if (score >= 60 && score < 70) grade = ‘D’; if (score < 60) grade = ‘F’; // Is it correct? // Can the statements be in different order? // Which is better? // if – else if – else if (score >= 90) grade = 'A'; else if (score >= 80) grade = 'B'; else if (score >= 70) grade = 'C'; else if (score >= 60) grade = 'D'; else // NO if here! grade = 'F'; // Do it this way! // Magic numbers!

8 // Declare constants to avoid magic numbers const float MAX_SCORE = 100.0; //change to 100 for final const float MIN_SCORE = 0.0; const float A_PERNT = 0.9; const float B_PERNT = 0.8; const float C_PERNT = 0.7; const float D_PERNT = 0.6; const char GRADE_A = ‘A’; const char GRADE_B = ‘B’; const char GRADE_C = ‘C’; const char GRADE_D = ‘D’; const char GRADE_F = ‘F’; if (score >= MAX_SCORE * A_PERNT) grade = GRADE_A; else if (score >= MAX_SCORE * B_PERNT) grade = GRADE_B; else if (score >= MAX_SCORE * C_PERNT) grade = GRADE_C; else if (score >= MAX_SCORE * D_PERNT) grade = GRADE_D; else grade = GRADE_F;

9 char ch1, ch2, ch3; cin >> ch1 >> ch2 >> ch3; if (ch1 == ch2) if (ch2 == ch3) cout << "All characters are the same."; else cout << "First two are the same."; if (ch1 == ch2) if (ch2 == ch3) cout << "All characters are the same."; else cout << "First two are NOT the same."; // Do we get “NOT the same” when ch1 != ch2? // NO! // else is paired with the last if // Indentation does not matter Nested IF

10 // Use braces to change the pairing if (ch1 == ch2) { if (ch2 == ch3) cout << "All initials are the same."; else cout << "First two are the same."; } if (ch1 == ch2) { if (ch2 == ch3) cout << "All initials are the same."; } else cout << "First two are NOT the same.";

11 if (ch1 == ch2) { if (ch2 == ch3) cout << "All initials are the same."; else cout << "First two are the same."; } else { if (ch2 == ch3) cout << "Last two are the same."; else if (ch1 == ch3) cout << "First and last are the same."; else cout << "All initials are different."; } // Next statement

12 char ch1, ch2, ch3; cin >> ch1 >> ch2 >> ch3; if (ch1 == ch2 && ch2 == ch3) cout << "All characters are the same."; if (ch1 == ch2 || ch2 == ch3 || ch1 == ch3) cout << “At least two characters are the same."; if (ch1 != ch2 && ch2 != ch3 && ch1 != ch3) cout << “The three characters are different."; if (ch1 != ch2 || ch2 != ch3 || ch1 != ch3) cout << “At least two characters are different."; if (ch1 && ch2 == ch3) // NO! if (ch1 == ch3) && (ch2 == ch3) // NO! if ((ch1 == ch3) && (ch2 == ch3)) // Yes! if (ch1 || ch2 == ch3) // NO! if (ch1 == ch3 || ch2 == ch3) // YES! Multiple Conditions

13 Arithmetic Expression and Logical (Boolean) Expression if (num2 == 0) cout << endl << "Cannot divide by zero!"; else { quotient = float(num1) / num2; cout << endl << "The quotient is " << quotient; }

14 Truth Table Cond1 Cond2 Cond1 && Cond2 Cond1 || Cond2 !Cond1 T F F F T T T F F T F F T T T F F F T T Value of Arithmetic Expression : a number Value of Logical Expression : true or false

15 Evaluate Logical Expression int num1 = 5, num2 = 7, num3 = 12; Logical Expression Value num1 + num2 > num3 ? (num1 + num2) > num3 (num3 > num2) && (num1 % num2 == 2) ? (num3 <= num2) || (num1 % num2 != 2) ? !((num3 > num2) && (num1 % num2 == 2)) ? (!(num3 > num2) || !(num1 % num2 == 2)) ?

16 Evaluate Logical Expression Logical Expression Same As This? !(cond1 && cond2) (!cond1 || !cond2) !(cond1 || cond2) (!cond1 && !cond2) DeMorgan’s Law

17 The Area of a Circle A = πr 2 const float PI = 3.14; float area, radius; area = PI * radius * radius; area = PI * pow(radius, 2);

18 The Area of a Circle const float PI = 3.14; float area, radius; cin >> radius; // Checking input if (radius > 0) { area = PI * radius * radius; cout << “The area is ” << area << ‘.’; } else cout << “Radius is zero or negative.”;

19 Example int inputValue; float result; char op; // ‘c’ for cube, ‘s’ for sqare, ‘q’ for sqare root, and any other chars are invalid cout << “Enter an integer and an operator: ”; cin >> inputValue >> operator; // Checking input if (op == ‘c’ || op == ‘C’) { } else if (op == ‘S’ || op == ‘s’) { } else if (op == ‘q’ || op == ‘Q’) { } else cout << “Invalid operator.”;

20... if (op == ‘c’ || op == ‘C’) { result = pow(inputValue, 3); cout << “The cube of ” << inputValue << “ is ” << result << ‘.’; } else if (op == ‘S’ || op == ‘s’) { result = pow(inputValue, 2); cout << “The sqare of ” << inputValue << “ is ” << result << ‘.’; } else if (op == ‘q’ || op == ‘Q’) { if (inputValue < 0) cout << “The input value is negative!”; else { result = sqrt(inputValue); cout << “The sqare root of ” << inputValue << “ is ” << result << ‘.’; } } else cout << “Invalid operator.”;

21 int main() { int inputValue; float result; char op; cout << “Enter an integer and an operator: ”; cin >> inputValue >> operator; if (op == ‘c’ || op == ‘C’) { result = pow(inputValue, 3); cout << “The cube of ” << inputValue << “ is ” << result << ‘.’; } else if (op == ‘S’ || op == ‘s’) { result = pow(inputValue, 2); cout << “The sqare of ” << inputValue << “ is ” << result << ‘.’; } else if (op == ‘q’ || op == ‘Q’) { if (inputValue < 0) cout << “The input value is negative!”; else { result = sqrt(inputValue); cout << “The sqare root of ” << inputValue << “ is ” << result << ‘.’; } } else cout << “Invalid operator.”; return 0; }

22 int main() { int inputValue; float result; string op; cout << “Enter an integer and an operator: ”; cin >> inputValue >> operator; if (op == “cube”) { result = pow(inputValue, 3); cout << “The cube of ” << inputValue << “ is ” << result << ‘.’; } else if (op == “square”) { result = pow(inputValue, 2); cout << “The sqare of ” << inputValue << “ is ” << result << ‘.’; } else if (op == “root”) { if (inputValue < 0) cout << “The input value is negative!”; else { result = sqrt(inputValue); cout << “The sqare root of ” << inputValue << “ is ” << result << ‘.’; } else cout << “Invalid operator.”; return 0; }

23 int main() { int inputValue; float result; string op; cout << “Enter an integer and an operator: ”; cin >> inputValue >> operator; if (op == “cube”) { result = pow(inputValue, CUBE); cout << “The cube of ” << inputValue << “ is ” << result << ‘.’; } else if (op == “square”) { result = pow(inputValue, SQUARE); cout << “The sqare of ” << inputValue << “ is ” << result << ‘.’; } else if (op == “root”) { if (inputValue < 0) cout << “The input value is negative!”; else { result = sqrt(inputValue); cout << “The sqare root of ” << inputValue << “ is ” << result << ‘.’; } else cout << “Invalid operator.”; return 0; }

24 #include using namespace std; int main() { int inputValue; float result; string op; cout << “Enter an integer and an operator: ”; cin >> inputValue >> operator; if (op == “cube”) { } else if (op == “square”) { } else if (op == “root”) { } else cout << “Invalid operator.”; return 0; }

Lab Assistants 25

26 Schedule Quiz2-1 (1 point) Due today by 10 pm Lab1 (3 points) Grace Time: Tuesday by 5 pm Quiz2-2 (2 Points) Wednesday in Class Prog 1 Due Next Monday

Grader You can submit to the grader multiple times. We grade your last submission. After you complete Labs before the due/grace time, DO NOT submit it again after the due/grace time. Keep the with 0 differences. me if you did it by accident. 27

Lab Access Room 009, 110 and 206 in Ullrich Weekdays: –7 am – 11 pm Weekends –9 am – 10 pm West side door Scan your ID card Report to me and ITS Helpdesk if not working 28

UWP Check UWP at least once a day me with any issues 29

Last Day to Drop without a ‘W’ 30 Drop fee begins and Withdrawn is noted on transcript Wednesday, September 16 Am I ready for CS143? Should I Go to CS113?