3 Decision Making: Equality and Relational Operators A condition is an expression that can be either true or false. Conditions can be formed using the.

Slides:



Advertisements
Similar presentations
Control Structures Selections Repetitions/iterations
Advertisements

More on Algorithms and Problem Solving
 Control structures  Algorithm & flowchart  If statements  While statements.
Introduction to C Programming
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 4 – Introducing Algorithms, Pseudocode and.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 3 - Structured Program Development Outline.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
Introduction to C Programming
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Structured Program Development in C
Chapter 4 Control Statements: Part I
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
The University of Texas – Pan American
Flow of Control Java Programming Mrs. C. Furman January 5, 2009.
Spring 2005, Gülcihan Özdemir Dağ Lecture 3, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 3 Outline 3.1 Introduction.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 4 – Wage Calculator Application: Introducing.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Structural Program Development: If, If-Else Outline.
Structured Program Development Outline 2.1Introduction 2.2Algorithms 2.3Pseudo code 2.4Control Structures 2.5The If Selection Structure 2.6The If/Else.
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
Lecture 2: Logical Problems with Choices. Problem Solving Before writing a program Have a thorough understanding of the problem Carefully plan an approach.
Pseudocode When designing an ALGORITHM to solve a problem, Pseudocode, can be used. –Artificial, informal language used to develop algorithms –Similar.
Dale Roberts Program Control using Java - Selection Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer.
C Lecture Notes 1 Structured Program Development.
1 C++ How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved. 4.
Chapter 04 Control Statements: Part II. OBJECTIVES In this part you will learn: if…else Double-Selection Statement. while Repetition Statement.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 21, 2005 Lecture Number: 10.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
1 Control Statements: Part I Chapter 4 Control Statements: Part I Chapter 4.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Selection Statements Selection Switch Conditional.
Dale Roberts 1 Program Control - Algorithms Department of Computer and Information Science, School of Science, IUPUI CSCI N305.
Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection Structure 3.6The.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements I.
 Pearson Education, Inc. All rights reserved Control Statements: Part 1.
Visual C# 2012 How to Program © by Pearson Education, Inc. All Rights Reserved. 1.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.1.
Sections © Copyright by Pearson Education, Inc. All Rights Reserved.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.1.
Java Programming Fifth Edition Chapter 5 Making Decisions.
 2002 Prentice Hall. All rights reserved. 1 Chapter 4 – Control Structures Part 1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
1 Lecture 2 Control Structures: Part 1 Selection: else / if and switch.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Programming Language C++ Lecture 3. Control Structures  C++ provides control structures that serve to specify what has to be done to perform our program.
1 JavaScript/Jscript 2 Control Structures I. 2 Introduction Before programming a script have a –Thorough understanding of problem –Carefully planned approach.
 By the end of this section you should be able to: ◦ Differentiate between sequence, selection, and repetition structure. ◦ Differentiae between single,
Chapter 3 Structured Program Development in C C How to Program, 8/e, GE © 2016 Pearson Education, Ltd. All rights reserved.1.
Chapter 7 JavaScript: Control Statements, Part 1
Branching statements.
Introduction to C++ Programming
Control Statements: Part 1
Introduction to C# Applications
Topic 3, Selection Statements
Java™ How to Program, 10/e Late Objects Version
JavaScript: Control Statements I
Chapter 2.1 Control Structures (Selection)
CSC113: Computer Programming (Theory = 03, Lab = 01)
Programming Fundamentals
Chapter 4 – Control Structures Part 1
JavaScript: Control Statements.
Chapter 4 Control Statements: Part I
Lecture 2: Logical Problems with Choices
Introduction to C++ Programming
Chapter 3: Introduction to Problem Solving and Control Statements
Structured Program
1) C program development 2) Selection structure
3 Control Statements:.
Chapter 6 Control Statements: Part 2
Structural Program Development: If, If-Else
Presentation transcript:

3 Decision Making: Equality and Relational Operators A condition is an expression that can be either true or false. Conditions can be formed using the equality operators ( == and != ) and relational operators ( >, = and <= ) summarized in Fig Fig | Equality and relational operators. (Part 1 of 2.)

3.9 Decision Making: Equality and Relational Operators (Cont.) 2 Fig | Equality and relational operators. (Part 2 of 2.)

3.6 Decision Making: Equality and Relational Operators The if structure – Used to make a decision based on the truth of the condition True: a statement is performed False: the statement is skipped over – The start of an if statement should not end in a semicolon (;) – Fig lists the equality and rational operators There should be no spaces separating the operators 3

3.6 Decision Making: Equality and Relational Operators 4

5 1 // Fig. 3.19: Comparison.cs 2 // Using if statements, relational operators and equality 3 // operators. 4 5 using System; 6 7 class Comparison 8 { 9 static void Main( string[] args ) 10 { 11 int number1, // first number to compare 12 number2; // second number to compare // read in first number from user 15 Console.Write( "Please enter first integer: " ); 16 number1 = Int32.Parse( Console.ReadLine() ); // read in second number from user 19 Console.Write( "\nPlease enter second integer: " ); 20 number2 = Int32.Parse( Console.ReadLine() ); if ( number1 == number2 ) 23 Console.WriteLine( number1 + " == " + number2 ); if ( number1 != number2 ) 26 Console.WriteLine( number1 + " != " + number2 ); if ( number1 < number2 ) 29 Console.WriteLine( number1 + " < " + number2 ); if ( number1 > number2 ) 32 Console.WriteLine( number1 + " > " + number2 ); 33

34 if ( number1 <= number2 ) 35 Console.WriteLine( number1 + " <= " + number2 ); if ( number1 >= number2 ) 38 Console.WriteLine( number1 + " >= " + number2 ); } // end method Main } // end class Comparison Please enter first integer: 2000 Please enter second integer: != > >= 1000 Please enter first integer: 1000 Please enter second integer: != < <= 2000 Please enter first integer: 1000 Please enter second integer: == <= >= 1000

7 3.6 Decision Making: Equality and Relational Operators

8 Figure 3.26 uses six if statements to compare two integers entered by the user. Outline Comparison.cs (1 of 3 ) Fig | Comparing integers using if statements, equality operators and relational operators. (Part 1 of 3).

9 Compare number1 and number2 for equality. Outline Comparison.cs (2 of 3 ) Fig | Comparing integers using if statements, equality operators and relational operators. (Part 2 of 3).

10 Outline Comparison.cs (3 of 3 ) Fig | Comparing integers using if statements, equality operators and relational operators. (Part 3 of 3).

Decision Making: Equality and Relational Operators (Cont.) If the condition in an if statement is true, the statement associated with that if statement executes. An if statement always begins with keyword if, followed by a condition in parentheses. An if statement expects one statement in its body. Common Programming Error 3.7 Forgetting the left and/or right parentheses for the condition in an if statement is a syntax error — the parentheses are required.

Decision Making: Equality and Relational Operators (Cont.) Common Programming Error 3.8 Reversing the operators !=, >= and and =<, can result in syntax or logic errors. Common Programming Error 3.9 It is a syntax error if the operators ==, !=, >= and = and < =, respectively. Good Programming Practice 3.12 Indent an if statement ’ s body to make it stand out and to enhance application readability.

Decision Making: Equality and Relational Operators (Cont.) Common Programming Error 3.10 Placing a semicolon immediately after the right parenthesis of the condition in an if statement is normally a logic error. Good Programming Practice 3.13 Place no more than one statement per line in an application. This format enhances readability. Good Programming Practice 3.14 A lengthy statement can be spread over several lines. If a single statement must be split across lines, choose breaking points that make sense, such as after a comma in a comma- ­ separated list, or after an operator in a lengthy expression. If a statement is split across two or more lines, indent all subsequent lines until the end of the statement.

Pseudocode Pseudocode is similar to every­day English, but it is not an actual computer programming language. It helps you “think out” an application before attempting to write it. A carefully prepared pseudocode application can easily be converted to a corresponding C# application.

Control Structures Normally, statements are executed one after the other in sequential execution. Various C# statements enable you to specify the next statement to execute. This is called transfer of control. Structured applications are clearer, easier to debug and modify, and more likely to be bug free.

Control Structures (Cont) An activity diagram models the workflow of a software system (Fig. 5.1). Activity diagrams are composed of symbols such as action-state symbols, diamonds, small circles and notes. Transition arrows represent the flow of the activity. The solid circle represents the activity’s initial state. The solid circle surrounded by a hollow circle represents the final state.

Control Structures (Cont) Fig. 5.1 | if else double-selection statement UML activity diagram.

Control Structures (Cont) Single-entry/single-exit control statements make it easy to build applications. Control statements are “attached” to one another by connecting the exit point of one to the entry point of the next. This procedure is called control-statement stacking. Control-statement nesting allows a control statement to appear inside another control statement.

if Single-Selection Statement if grade is greater than or equal to 60 display “Passed” The preceding pseudocode if statement may be written in C# as: if ( grade >= 60 ) Console.WriteLine( "Passed" ); Note that the C# code corresponds closely to the pseudocode.

if Single-Selection Statement (Cont.) Figure 5.2 illustrates the single-selection if statement. The diamond, or decision symbol indicates that a decision is to be made. The workflow will continue along a path determined by the symbol’s associated guard conditions. Fig. 5.2 | if single-selection statement UML activity diagram.

if … else Double-Selection Statement The if … else double-selection statement allows you to specify an action to perform when the condition is true and a different action when the condition is false: if grade is greater than or equal to 60 display “Passed” else display “Failed” The preceding if…else pseudocode statement can be written in C# as if ( grade >= 60 ) Console.WriteLine( "Passed" ); else Console.WriteLine( "Failed" );

if … else Double-Selection Statement (Cont.) Good Programming Practice 5.1 Indent both body statements of an if … else statement. Good Programming Practice 5.2 If there are several levels of indentation, each level should be indented the same addi­tional amount of space.

if … else Double-Selection Statement (Cont.) Figure 5.3 illustrates the flow of control in the if … else statement. The symbols in the UML activity diagram represent action states and a decision. Fig. 5.3 | if … else double-selection statement UML activity diagram.

24 The conditional operator ( ?: ) can be used in place of an if … else statement. Console.WriteLine( grade >= 60 ? "Passed" : "Failed" ); – The first operand is a boolean expression that evaluates to true or false. – The second operand is the value if the expression is true – The third operand is the value if the expression is false. ` 5.6 if … else Double-Selection Statement (Cont.)

25 Good Programming Practice 5.3 Conditional expressions are more difficult to read than if … else statements and should be used to replace only simple if else statements that choose between two values. Good Programming Practice 5.4 When a conditional expression is inside a larger expression, it’s good practice to parenthesize the conditional expression for clarity. Adding parentheses may also prevent operator precedence problems that could cause syntax errors. 5.6 if … else Double-Selection Statement (Cont.)

26 An application can test multiple cases with nested if … else statements: if grade is greater than or equal to 90 display “A” else if grade is greater than or equal to 80 display “B” else if grade is greater than or equal to 70 display “C” else if grade is greater than or equal to 60 display “D” else display “F” 5.6 if … else Double-Selection Statement (Cont.)

27 This pseudocode may be written in C# as if ( grade >= 90 ) Console.WriteLine( "A" ); else if ( grade >= 80 ) Console.WriteLine( "B" ); else if ( grade >= 70 ) Console.WriteLine( "C" ); else if ( grade >= 60 ) Console.WriteLine( "D" ); else Console.WriteLine( "F" ); 5.6 if … else Double-Selection Statement (Cont.)

28 Most C# programmers prefer to use else if : if ( grade >= 90 ) Console.WriteLine( "A" ); else if ( grade >= 80 ) Console.WriteLine( "B" ); else if ( grade >= 70 ) Console.WriteLine( "C" ); else if ( grade >= 60 ) Console.WriteLine( "D" ); else Console.WriteLine( "F" ); 5.6 if … else Double-Selection Statement (Cont.)

29 The C# compiler always associates an else with the immediately preceding if unless told to do otherwise by the placement of braces ( { and } ). This behavior can lead to what is referred to as the dangling- else problem. 5.6 if … else Double-Selection Statement (Cont.)

30 if ( x > 5 ) if ( y > 5 ) Console.WriteLine( "x and y are > 5" ); else Console.WriteLine( "x is <= 5" ); The compiler actually interprets the statement as: if ( x > 5 ) if ( y > 5 ) Console.WriteLine( "x and y are > 5" ); else Console.WriteLine( "x is <= 5" ); 5.6 if … else Double-Selection Statement (Cont.)

31 To have the nested if else statement execute as it was intended, write it as follows: if ( x > 5 ) { if ( y > 5 ) Console.WriteLine( "x and y are > 5" ); } else Console.WriteLine( "x is <= 5" ); 5.6 if … else Double-Selection Statement (Cont.)

32 A set of statements contained within a pair of braces ( { and } ) is called a block: if ( grade >= 60 ) Console.WriteLine( "Passed" ); else { Console.WriteLine( "Failed" ); Console.WriteLine( "You must take this course again." ); } Without the braces, the last statement would execute regardless of whether the grade was less than if … else Double-Selection Statement (Cont.)

33 A logic error has its effect at execution time. – A fatal logic error causes an application to fail and terminate prematurely. – A nonfatal logic error allows an application to continue executing. 5.6 if … else Double-Selection Statement (Cont.)

if … else Double-Selection Statement (Cont.) Common Programming Error 5.1 Forgetting braces that delimit a block can lead to syntax errors or logic errors in an application. Good Programming Practice 5.5 Always using braces in an if … else (or other) statement helps prevent their accidental omission. Some programmers type the beginning and ending braces of blocks before typing the individual statements within them.

if … else Double-Selection Statement (Cont.) Common Programming Error 5.2 Placing a semicolon after the condition in an if or if…else statement leads to a logic error in single-selection if statements and a syntax error in double-selection if…else statements (when the if -part contains an actual body statement).