Summary Two basic concepts: variables and assignments Basic types:

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.
1 Engineering Problem Solving With C++ An Object Based Approach Chapter 3 Control Structures.
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
Week 4 Selections This week shows how to use selection statements for more flexible programs. It also describes the various integral types that are available.
1 9/24/07CS150 Introduction to Computer Science 1 Relational Operators and the If Statement.
Programming Switch command. COMP102 Prog. Fundamentals: Switch command / Slide 2 Multiple Selection: The switch Statement value1 action 1 value2 action.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
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.
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.
1 9/25/06CS150 Introduction to Computer Science 1 Nested Ifs, Logical Operators, exit() Page 194.
1 CS150 Introduction to Computer Science 1 Relational Operators and the If Statement 9/22/08.
CS 1400 Jan 2007 Chapter 4, sections Relational operators Less than< Greater than> Less than or equal= Equal== Not equal!=
1 Midterm Review COMP 102. Tips l Eat a light meal before the exam l NO electronic devices (including calculators, dictionaries, phones, pagers, etc.)
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.
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.
C++ Programming Language Day 1. What this course covers Day 1 – Structure of C++ program – Basic data types – Standard input, output streams – Selection.
C++ Operators CS242 COMPUTER PROGRAMMING T.Banan Al-Hadlaq.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational operators – Discover.
Control Structures – Selection Chapter 4 2 Chapter Topics  Control Structures  Relational Operators  Logical (Boolean) Operators  Logical Expressions.
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.
Selection Structures (if & switch statements) (CS1123)
1 Conditions Logical Expressions Selection Control Structures Chapter 5.
CS31: Introduction to Computer Science I Discussion 1A 4/9/2010 Sungwon Yang
Basic Of Computer Science
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.
Computer Science 1620 boolean. Types so far: Integer char, short, int, long Floating Point float, double, long double String sequence of chars.
1 Compound Assignment C++ has a large set of operators for applying an operation to an object and then storing the result back into the object Examples.
1 09/15/04CS150 Introduction to Computer Science 1 Life is Full of Alternatives Part 2.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Programming 1 DCT 1033 Control Structures I (Selection) if selection statement If..else double selection statement Switch multiple selection statement.
CSC1201: Programming Language 2 Lecture 1 Level 2 Course Nouf Aljaffan (C) CSC 1201 Course at KSU1.
1 Chapter 4, Part 1 If Control Construct A mechanism for deciding whether an action should be taken JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S.
Lecture 7: Making Decisions Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
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,
CSE202: Lecture 5The Ohio State University1 Selection Structures.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 15, 2004 Lecture Number: 11.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Control Structures (Selection & Repetition)
1 09/10/04CS150 Introduction to Computer Science 1 What Actions Do We Have Part 2.
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational operators – Discover.
The Ohio State University
Branching statements.
Introduction to Computer Programming
Chapter 3 Selection Statements
Chapter 3 Control Statements
Bill Tucker Austin Community College COSC 1315
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.
CMPT 201 if-else statement
A mechanism for deciding whether an action should be taken
Engineering Problem Solving with C++, Etter/Ingber
CSC113: Computer Programming (Theory = 03, Lab = 01)
Control Structures – Selection
Miscellaneous Flow Control
3 Control Statements:.
Selection Control Structure
Chapter 4 Selection.
If Statements.
Chapter 4: Control Structures I (Selection)
SE1H421 Procedural Programming LECTURE 4 Operators & Conditionals (1)
Engineering Problem Solving with C++ An Object Based Approach
Arithmetic Operations
Life is Full of Alternatives
Branching statements Kingdom of Saudi Arabia
COMS 261 Computer Science I
Module 3 Selection Structures 6/25/2019 CSE 1321 Module 3.
Presentation transcript:

Summary Two basic concepts: variables and assignments Basic types: int, double, char (string), … Some C++ practical issues: division rule, operator precedence

Expression and statement An expression has a value which is the result of some operation(s) on the associated operands. 4, x-y, 2-a-(b*c) A statement is a sentence that acts as a command it does not have a value it always ends in a ‘;’ cin >> x; x = 5; int x;

Boolean type and expressions

Boolean type: bool C++ contains a type (new!) named bool which can have one of two values true (corresponding to non-zero value) false (corresponding to zero value) Boolean operators Logical and: && Logical or: || Logical not: ! Examples bool P = true; bool Q = false; bool R = true; bool S = P && Q; bool T = (!Q) || R; bool U = !(R && !Q);

Boolean expressions Arithmetic expression: use arithmetic operators +,-,*,/, to produce a number as the final result A Boolean expression has one of the two values: true or false use relational operators <,>, ==, … and boolean operators AND (&&), OR (||), NOT (!)

Using Relational Operators Relational operators are used to compare two values Math C++ Plain English = == equals [example: if(a==b) ] [ (a=b) means put the value of b into a ] < < less than  <= less than or equal to > > greater than  >= greater than or equal to  != not equal to

Examples: numberOfStudents < 200 10 > 20 20 * j == 10 + i

Using Boolean (logical) operators Boolean operators can be used to form more complex conditional expressions Logical AND operator  && Logical OR operator  || Logical NOT operator  ! Examples: (x>5) && (x<10) (x>10) || (x<5) !(x>5) Warning! & and | are also operators

Operator Precedence Answer: Which comes first? * / % + - * / % + - < <= >= > == != = Answer:

Summary of Operator Precedence Precedence of operators (from highest to lowest) Parentheses ( … ) Unary operators ! Multiplicative operators * / % Additive operators + - Relational ordering < <= >= > Relational equality == != Logical and && Logical or || Assignment = arithmetic relational logical

Example: 5 != 6 || 7 <= 3 (5 !=6) || (7 <= 3) 5 * 15 + 4 == 13 && 12 < 19 || !false == 5 < 24

Flow control

Introduction Three program structures or constructs: Sequence Statements in the given order Conditional statement (branching) Chooses between two (or more) sequences depending on some condition if <condition exists> { <do P> } else { <do Q> Iteration statement (looping) repetitively execute a given sequence while <condition exists> {

Structured programming Any program can be written as a sequence of three basic program structures!!! 1. sequences, 2. conditionals, 3. and iterations Program Structure 1 Program Structure 3 Program Structure 1 Program Structure 2

The fundamental conditional if-else Statement Choose between two alternative actions depending on a test (on the values of variables). Expression Action1 Action2 true false Syntax if (Expression) Action1 else Action2 If Expression is true then execute Action1 otherwise execute Action2 Example if(v == 0) cout << "v is 0"; else cout << "v is not 0";

It’s common in everyday life … ? if <it's sunny>{ <go to beach with sun block> } else{ <go to beach with umbrella>

Example: Absolute Value (1st ) // program to read number & print its absolute value #include <iostream> using namespace std; int main(){ int value; int absvalue; cout << "Enter integer: "; cin >> value; if (value < 0) absvalue = -value; else absvalue = value; cout << "The absolute value is " << absvalue << endl; return 0; }

When the action is more than one statement … Put multiple action statements within braces if <it's raining> { <take umbrella> <wear raincoat> } else { <take sunbathing stuff>

Example: Absolute Value (2nd) // program to read number & print its absolute value #include <iostream> using namespace std; int main(){ int value; int absvalue; // absolute value cout << "Enter integer: "; cin >> value; if (value < 0) { absvalue = -value; cout << "The input value is negative and its absolute value is " << absvalue << endl; } else { absvalue = value; cout << "The input value is positive and its absolute value is " << absvalue << endl; return 0;

Summary of the conditional if-else Statement if (cond1) B else C D A A B or C D D =

Nested if-else Statements Nested means that one complete statement is inside another if cond1 { A; if cond2 { B; } else { C D

Example double score; cin >> score; if(score >= 90.0) cout << "Grade = A" << endl; else if(score >= 80.0) cout << "Grade = B" << endl; else if(score >= 70.0) cout << "Grade = C" << endl; else if(score >= 60.0) cout << "Grade = D" << endl; else cout << "Grade = F" << endl;