Conditional Expressions

Slides:



Advertisements
Similar presentations
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Advertisements

If Statements & Relational Operators Programming.
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
true (any other value but zero) false (zero) expression Statement 2
1 Selection in C. 2 If / else if statement:  The else part of an if statement can be another if statement. if (condition) … else if (condition) … else.
1 Lecture 7:Control Structures I (Selection) Introduction to Computer Science Spring 2006.
CS 3850 Lecture 5 Operators. 5.1 Binary Arithmetic Operators Binary arithmetic operators operate on two operands. Register and net (wire) operands are.
1 CS150 Introduction to Computer Science 1 Relational Operators and the If Statement 9/22/08.
1 Arithmetic in C. 2 Type Casting: STOPPED You can change the data type of the variable in an expression by: (data_Type) Variable_Name Ex: int a = 15;
Chapter 4 Making Decisions
1 Revision of IO Streams printf scanf. 2 CSE1301 Computer Programming Lecture 8 Booleans.
Ryan Chu. Arithmetic Expressions Arithmetic expressions consist of operators, operands, parentheses, and function calls. The purpose is to specify an.
Computer Science 101 The Boolean System. George Boole British mathematician ( ) Boolean algebra –Logic –Set theory –Circuits –Conditions in if.
CHAPTER:8 OPERATORS AND EXPRESSION IN C++ Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
Chapter 2 part #4 Operator
 Input and Output Functions Input and Output Functions  OperatorsOperators Arithmetic Operators Assignment Operators Relational Operators Logical Operators.
Chapter 3: Data Types and Operators JavaScript - Introductory.
CHAPTER 2 PART #4 OPERATOR 2 nd semester King Saud University College of Applied studies and Community Service Csc 1101 By: Asma Alosaimi Edited.
1 Conditions Logical Expressions Selection Control Structures Chapter 5.
Computer Science 210 Computer Organization Introduction to Boolean Algebra.
1 If statement and relational operators –, >=, ==, != Finding min/max of 2 numbers Finding the min of 3 numbers Forming Complex relational expressions.
CPS120: Introduction to Computer Science Operations Lecture 9.
Programming Fundamental Slides1 Data Types, Identifiers, and Expressions Topics to cover here: Data types Variables and Identifiers Arithmetic and Logical.
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.
CCSA 221 Programming in C CHAPTER 6 MAKING DECISIONS 1.
1 Operators and Expressions. Expressions Combination of Operators and Operands Example 2 * y + 5 Operands Operators.
VARIABLES, CONSTANTS, OPERATORS ANS EXPRESSION
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.
Boolean Data Lesson CS1313 Fall Boolean Data Outline 1.Boolean Data Outline 2.Data Types 3.C Boolean Data Type: char or int 4.C Built-In Boolean.
Booleans Lecture 26. Summary of previous lecture In the previous lecture, we have been learnt,  The if statement  The else statement  Cascaded if 
Operators & Expressions
An Object-Oriented Approach to Programming Logic and Design Chapter 5 Making Decisions.
Department of Electronic & Electrical Engineering Expressions operators operands precedence associativity types.
ECE 103 Engineering Programming Chapter 4 Operators Herbert G. Mayer, PSU CS Status 6/19/2015 Initial content copied verbatim from ECE 103 material developed.
Expression and Operator. Expressions and Operators u Examples: 3 + 5; x; x=0; x=x+1; printf("%d",x); u Two types: –Function calls –The expressions formed.
Operators A binary operator combines two values to get one result: x OP y where OP is any binary operators such as +, -, *, /, ==, !=, >, &&, or even =.
 Most C programs perform calculations using the C arithmetic operators (Fig. 2.9).  Note the use of various special symbols not used in algebra.  The.
CSE202: Lecture 5The Ohio State University1 Selection Structures.
OPERATORS IN C CHAPTER 3. Expressions can be built up from literals, variables and operators. The operators define how the variables and literals in the.
1 Chapter 3 – Operators and Expressions Outline 3.1Introduction 3.2Arithmetic operators 3.3Relational operators 3.4Logical operators 3.5Assignment operators.
Rational Expressions relational operators logical operators order of precedence.
7-1/27 Chapter 7 Expressions and Assignment Statements Introduction Arithmetic Expressions Overloaded Operators Type Conversions Relational and Boolean.
 Type Called bool  Bool has only two possible values: True and False.
ECE 103 Engineering Programming Chapter 4 Operators Herbert G. Mayer, PSU Status 6/10/2016 Initial content copied verbatim from ECE 103 material developed.
Relational Operator and Operations
Revision of IO Streams printf scanf.
Computer Science 210 Computer Organization
Chapter 7: Expressions and Assignment Statements
Operators And Expressions
Operators and Expressions
Rational Expressions. relational operators. logical operators
CMPT 201 if-else statement
A mechanism for deciding whether an action should be taken
Data Types, Identifiers, and Expressions
Chapter 7: Expressions and Assignment Statements
Intro to C Tutorial 4: Arithmetic and Logical expressions
Operators and Expressions
Introduction to Programming
Bools and simple if statements
Relational Operators Operator Meaning < Less than > Greater than
College of Computer Science and Engineering
Computer Science 210 Computer Organization
Lecture3.
Chapter 7 Expressions and Assignment Statements.
Herbert G. Mayer, PSU CS Status 7/19/2015
ENERGY 211 / CME 211 Lecture 5 October 1, 2008.
Operator King Saud University
Conditionals.
Presentation transcript:

Conditional Expressions

Control Statements A control statement will or will not be executed based on the value of the conditional expression scanf("%d", &pressureValue); while (pressureValue <= MAX_PRESSURE) { if (pressureValue == NOMINAL_VALUE) printf("Pressure is nominal\n"); else printf("Pressure is %d pounds\n", pressureValue); for (i = 5; i <= pressureValue; i = i + 10) printf("+"); printf("\n"); } // End while

Boolean Type A conditional expression evaluates to a value of true or false These are the two values for the Boolean type named after the mathematician George Boole When a conditional expression is evaluated, it results in a value of type Boolean In the C programming language there is no explicit Boolean type. Instead, false is 0 and true is any non-zero value. Many C programs use a #define macro to set a Boolean variable to true or false #define FALSE 0 #define TRUE 1 Conditional expressions are also called Boolean expressions A conditional expression is used whenever a true or false decision needs to be made about the value of certain variables or constants in a program

Expression Contents A conditional expression in C may contain any valid mathematical expression It may also contain relational operators and logical operators In addition, it may contain one or more function calls that return a value

Relational Operators ==,!= Equivalent, Not equivalent <, <= Less than, Less than or equals >, >= Greater than, Greater than or equals if (A == B) C = 5; if (A != B) C = 10; for (i = 0; i < MAX_INDEX; i++) printf("*"); if (A <= B) D = 20; while (A > B) B++; if (A >= B) D = 40; A = B <= C; D = A == B;

Logical Operators && Logical AND || Logical OR ! Logical NOT if ( (A == B) && (C <= D) ) E = 5; if ( (A != B) || (C >= D) ) F = 10; while ( !(A == B) ) B++; if ( !((A >= B) && (C <= D) || (E != F))) G = 100; A = B || C; D = A && B;

Boolean Algebra NOT false = true NOT true = false true AND true = true true AND false = false true OR true = true true OR false = true false AND false = false false OR false = false if (0 || 0) printf("Does false OR false = true?"); if (0 && 0) printf("Does false AND false = true?");

More Examples ch1 = 'a'; ch2 = 'a'; printf("ch1 OR ch2 = %d\n", ch1 || ch2); // 1 printf("ch1 AND ch2 = %d\n", ch1 && ch2); // 1 ch1 = 'd'; ch2 = 'f'; ch2 = '\0'; printf("ch1 AND ch2 = %d\n", ch1 && ch2); // 0

Example with Logical and Relational Operators while ( (w <= x) && ( y >= z) ) { if ( (w < MAX_HEIGHT) || (y > MAX_DISTANCE) ) w = w + newHeight; y = y - newDistance; newValuesUsed = TRUE; } else w = w + oldHeight; y = y - oldDistance; newValuesUsed = FALSE; } // End if if ( !silentMode ) // Use of negative logic printf("Height: %d Distance: &d\n", w, y); } // End while

Operator Precedence Rule of thumb: Avoid complicated Precedence refers to the order in which operations are evaluated in an expression a = w * x + !y || -u – v && b % c / d++; There is a hierarchy of rules that tells which operators are evaluated before other operators (i.e., unary before binary operators, multiplication before addition, relational operators before logical operators) The highest precedence goes to parentheses Rule of thumb: Avoid complicated expressions and use parentheses

Special Notes Avoid complicated conditional expressions, especially When they are not a part of the original procedural design When they are created just to get the program to finally run Use meaningful names for Boolean variables and declare them of type int Ex. valueFound, errorStatus, moreData Do not compare Boolean variables to TRUE or FALSE if (valueFound == TRUE) X = 5; // Bad practice if (valueFound) X = 5; // Good practice if (valueFound == FALSE) X = 5; // Bad practice if (!valueFound) X = 5; // Good practice

Special Notes (continued) Beware of the bitwise operators; they are not the same as the logical operators & bitwise AND | bitwise OR if (4 & 2) printf("Is this true?"); // Bitwise if (4 && 2) printf("Or is this true?"); // Logical Do not user == or != operator with values of type float double v = 0.999999999999999999999999999; . . . if (v == ZERO) X = 10; // Bad if ((v >= -0.001) && (v <= 0.001)) x = 10; // Good 