© Janice Regan, CMPT 102, Sept. 2006 0 CMPT 102 Introduction to Scientific Computer Programming Logical and Relational Expressions Nested if statements.

Slides:



Advertisements
Similar presentations
Chapter 2 Flow of Control. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 2-2 Learning Objectives Boolean Expressions Building, Evaluating.
Advertisements

© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students Control Structures Nested ifs, switch statements.
Control Structures Control structures are used to manage the order in which statements in computer programs will be executed Three different approaches.
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
IntroductionIntroduction  Computer program: an ordered sequence of statements whose objective is to accomplish a task.  Programming: process of planning.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Expressions and Operators Program Style.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Conditional Statements Control Structures.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
CS 117 Spring 2002 Review for Exam 2 March 6, 2002 open book, 1 page of notes.
Tutorial 4 Decision Making with Control Structures and Statements Section A - Decision Making JavaScript Tutorial 4 -Decision Making with Control.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 4 Control Structures I: Selection.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Control Statements.
The switch Statement, DecimalFormat, and Introduction to Looping
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 4 Decision Structures and Boolean Logic.
Decision Structures and Boolean Logic
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
Chapter 4: Control Structures I J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
CPS120: Introduction to Computer Science Decision Making in Programs.
CSCI 1100/1202 January 28, The switch Statement The switch statement provides another means to decide which statement to execute next The switch.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Flow Control (Switch, do-while, break) Outline 4.7The.
Pseudocode When designing an ALGORITHM to solve a problem, Pseudocode, can be used. –Artificial, informal language used to develop algorithms –Similar.
 Learn about control structures  Examine relational and logical operators  Explore how to form and evaluate logical (Boolean) expressions  Learn how.
Control Structures 1. Control Structures Java Programming: From Problem Analysis to Program Design, D.S. Malik 2.
Decision making statements. Decision making statements are used to skip or to execute a group of statements based on the result of some condition. The.
Control Statements in C 1.Decision making statements 2.Looping statements 3.Branching statements
4.1 Object Operations operators Dot (. ) and new operate on objects The assignment operator ( = ) Arithmetic operators + - * / % Unary operators.
Programming 1 DCT 1033 Control Structures I (Selection) if selection statement If..else double selection statement Switch multiple selection statement.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students Introduction to branching.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 4 Control Structures I: Selection.
Chapter 4 Control Structures I. Chapter Objectives Learn about control structures Examine relational and logical operators Explore how to form and evaluate.
Week 4 Program Control Structure
Control statements Mostafa Abdallah
CPS120: Introduction to Computer Science Decision Making in Programs.
CPS120: Introduction to Computer Science Decision Making in Programs.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Control Structures (Selection & Repetition)
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Chapter 4: Control Structures I
Chapter 4 – C Program Control
Chapter 4: Control Structures I
Sequence, Selection, Iteration The IF Statement
Selections Java.
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.
Control Statements: Part 2
EGR 2261 Unit 4 Control Structures I: Selection
Chapter 3 Control Statements Lecturer: Mrs Rohani Hassan
Topics The if Statement The if-else Statement Comparing Strings
Expressions and Control Flow in JavaScript
JavaScript: Control Statements I
Chapter 4: Control Structures I
Topics The if Statement The if-else Statement Comparing Strings
الكلية الجامعية للعلوم التطبيقية
IF if (condition) { Process… }
Chapter 8 JavaScript: Control Statements, Part 2
Chapter 4 - Program Control
Control Structures: Selection Statement
Chapter 4: Control Structures I (Selection)
Control Structure Chapter 3.
Week 3 – Program Control Structure
Control Structures: Selection Statement
Relational and Logical Operators
Chap 7. Advanced Control Statements in Java
Relational and Logical Operators
Control Structure.
Presentation transcript:

© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Logical and Relational Expressions Nested if statements Case statements

© Janice Regan, CMPT 102, Sept Multiple Selections (if-else if-else) if (condition) {/* Series of actions to be taken when the condition is TRUE */ action 1; action n; } else if (condition 2) { /* actions to be taken when condition is FALSE and condition2 is TRUE */ action 1; action n; } else { /*Series of actions to be taken when condition and condition 2 are FALSE */ action 1; action n; }

© Janice Regan, CMPT 102, Sept Defining the condition  Each decision statement is based upon a condition  The condition is an expression with a logical value (true or false)  The condition may be  A relational expression (a type of logical expression) Two numerical values combined using a binary relational operator (a simple relational expression) A more complex relational expression  Another type of logical expression Two logical values combined with a binary logical operator One logical value One logical value operated on by a unary logical operator A more complex logical expression

© Janice Regan, CMPT 102, Sept Precedence of operators in C  ( ) []. innermost first  (pre) + - ! ~(unary) (right to left)  * / %  + -  >=  == !=  &&  ||  = += -= *= /= %=(right to left)

© Janice Regan, CMPT 102, Sept Expressions with relational operators  Value of a relational expression (expression including a relational or binary equality operator) is true or false.  Arguments of a relational operator are numerical (or character)  A < C  (A + B) >= CLet A=9, B=5, C=2  A * B <= C  A % C == A % B  A != -C

© Janice Regan, CMPT 102, Sept Expressions: relational operators  A < C  Let A=9, B=5, C=2 A C < 9 2 F A C < Value of expression is Boolean: In C Boolean (T or F) is represented by an integer. T=0, F any non zero integer

© Janice Regan, CMPT 102, Sept Expressions: relational operators  (A + B) >= C X >= C  Let A=9, B=5, C=2 + A B X C >= T A B C + >= Value of expression is Boolean:

© Janice Regan, CMPT 102, Sept Expressions: relational operators  A * B <= C X <= C  Let A=9, B=5, C=2 * A B X C <= F A B C * <= Value of expression is Boolean:

© Janice Regan, CMPT 102, Sept Expressions: relational operators  Let A=9, B=5, C=2  A % C == A % B X == A % B X == Y % A C X Y A 9 5 % B == F A C % % B 4 Value of expression

© Janice Regan, CMPT 102, Sept Expressions: relational operators  A != -C A != X  Let A=9, B=5, C=2 != A X C T A C - != Value of expression

© Janice Regan, CMPT 102, Sept Importance of order of operations  Order of operations is determined by operator precedence rules () before /  (A + B) / C X / C  Let A=10, B=5, C=2 2 A B C + / + A B X C Value of expression

© Janice Regan, CMPT 102, Sept Expressions: logical operators  Value of a logical expression (expression including a logical operator) is true or false.  Arguments of the logical operator are also true or false Let A=9, B=5, C=2 Then  (C = C  (A C  !(A C  A < B && B < C++

© Janice Regan, CMPT 102, Sept Expressions: logical operators  Let A=9, B=5, C=2  (C = C) X && (A >= C) X && Y < C B X 2 5 T Y A 9 2 >= C && T A C < >= B && T Value of expression

© Janice Regan, CMPT 102, Sept Expressions: logical operators  Let A=9, B=5, C=2  (A C) X || (A > C) X || Y < A B X 9 5 F || T A C < > B Value of expression Y A 9 2 > C T T

© Janice Regan, CMPT 102, Sept Expressions: logical operators  Let A=9, B=5, C=2  !(A C) !X || (A > C) Y || (A > C) Short Circuit < A B X 9 5 F Y T || F A B < ! C Value of expression !

© Janice Regan, CMPT 102, Sept Expressions: logical operators  Let A=9, B=5, C=2  A < B && B < C++ X && B < C++ Short Circuit (increment not evaluated!) < A B X 9 5 F && F A B < C Value of expression

© Janice Regan, CMPT 102, Sept Flowchart for multiple selection condition2 Statement 1; Statement n; T F Statement 1; Statement n; condition F Statement 1; Statement n; T

© Janice Regan, CMPT 102, Sept Multiple Selections (if-else if-else) if (condition) {/* Series of actions to be taken when the condition is TRUE */ action 1; action n; } else if (condition 2) { /* actions to be taken when condition is FALSE and condition2 is TRUE */ action 1; action n; } else { /*Series of actions to be taken when condition and condition 2 are FALSE */ action 1; action n; } NEXT STATEMENT

© Janice Regan, CMPT 102, Sept Multiple Selections (Nested if) if (condition) { /* Series of actions to be taken when the condition is TRUE */ action 1; action n; } else { if (condition 2) { /* Series of actions to be taken when condition is FALSE and condition2 TRUE */ action 1; action n; } else { /* Series of actions to be taken when condition and condition 2 are FALSE */ action 1; action n; }

© Janice Regan, CMPT 102, Sept Flowchart for multiple selection condition2 Statement 1; Statement n; T F Statement 1; Statement n; condition F Statement 1; Statement n; T

© Janice Regan, CMPT 102, Sept Flowchart for multiple selection Statement 1; Statement n; T F condition

© Janice Regan, CMPT 102, Sept Multiple Selections (Nested if) if (condition) { if (condition 2) { /* Series of actions to be taken when condition is FALSE and condition2 TRUE */ action 1; action n; } else { /* Series of actions to be taken when condition and condition 2 are FALSE */ action 1; action n; } else { /* Series of actions to be taken when the condition is TRUE */ action 1; action n; }

© Janice Regan, CMPT 102, Sept Flowchart for multiple selection condition2 Statement 1; Statement n; F T Statement 1; Statement n; condition F Statement 1; Statement n; T

© Janice Regan, CMPT 102, Sept Flowchart for multiple selection Statement 1; Statement n; F T condition

© Janice Regan, CMPT 102, Sept The switch statement  An alternative method to if statements with multiple else clauses  The controlling expression (selector) must have an integral type  Characters also work because they can be converted to integers  Case labels are particular values of the controlling expression  Break statements exit from the switch structure

© Janice Regan, CMPT 102, Sept switch Structures switch(controling expression) { case value1: statements1; break; case value2: statements2; break;... case valuen: statementsn; break; default: statements; }

© Janice Regan, CMPT 102, Sept switch Statement expression = value1 expression = value2 expression = valuen

© Janice Regan, CMPT 102, Sept Sample case statement /* This is in a loop that reads */ /* nextChar each time */ while ( endInput != nextChar ) { switch (nextChar) { case 0: count1++; break; case 1: count2++; break; case 2: count3++; break; case 4: count4++; break; case 5: count5++; break; case 6: count6++; break; case 7: count7++; break; default: countOther++; }