Lesson #4 Logical Operators and Selection Statements.

Slides:



Advertisements
Similar presentations
Control Statements. Define the way of flow in which the program statements should take place. Control Statements Implement decisions and repetitions.
Advertisements

Programming In C++ Spring Semester 2013 Lecture 4 Programming In C++, Lecture 3 By Umer Rana.
1 Conditional Statement. 2 Conditional Statements Allow different sets of instructions to be executed depending on truth or falsity of a logical condition.
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
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.
Control Structures 4 Control structures control the flow of execution of a program 4 The categories of control structures are: –Sequence –Selection –Repetition.
1 Objectives You should be able to describe: Relational Expressions The if-else Statement Nested if Statements The switch Statement Common Programming.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
true (any other value but zero) false (zero) expression Statement 2
1 CS 201 Selection Structures (1) Debzani Deb. 2 Error in slide: scanf Function scanf(“%lf”, &miles); function name function arguments format string variable.
CS 117 Spring 2002 Review for Exam 2 March 6, 2002 open book, 1 page of notes.
Control Structures Control structures control the flow of program execution. 3 types of control structures: sequence, selection.
C++ for Engineers and Scientists Third Edition
Decision Structures and Boolean Logic
CPS 125: Digital Computation and Programming Selection Structures: if and switch Statements.
1 Chapter 4: Selection Structures. In this chapter, you will learn about: – Selection criteria – The if-else statement – Nested if statements – The switch.
Control Structures – Selection Chapter 4 2 Chapter Topics  Control Structures  Relational Operators  Logical (Boolean) Operators  Logical Expressions.
1 4.8The do/while Repetition Structure The do/while repetition structure –Similar to the while structure –Condition for repetition tested after the body.
1 CSC 1401 S1 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University
A. Abhari CPS1251 Topic 4: Control structures Control Structures Overview Conditions if Statement Nested if Statements switch Statement Common Programming.
2 Objectives You should be able to describe: Relational Expressions Relational Expressions The if-else Statement The if-else Statement Nested if Statements.
1 Lecture 5: Selection Structures. Outline 2  Control Structures  Conditions  Relational Operators  Logical Operators  if statements  Two-Alternatives.
Flow of Control Part 1: Selection
ㅎㅎ logical operator if if else switch while do while for Third step for Learning C++ Programming Repetition Control Structures.
Control Statements in C 1.Decision making statements 2.Looping statements 3.Branching statements
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
CCSA 221 Programming in C CHAPTER 6 MAKING DECISIONS 1.
Control statements Mostafa Abdallah
Department of Electronic & Electrical Engineering Expressions operators operands precedence associativity types.
Chapter 5 – Decision Making. Structured Programming Sequence Selection Repetition yesno yes no.
Control Statements: Part1  if, if…else, switch 1.
Chad’s C++ Tutorial Demo Outline. 1. What is C++? C++ is an object-oriented programming (OOP) language that is viewed by many as the best language for.
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
Chapter 4 : Selection Structures: if and switch statement By Suraya Alias.
Lesson #4 Logical Operators and Selection Statements.
CNG 140 C Programming (Lecture set 3)
‘C’ Programming Khalid Jamal.
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.
Decisions Chapter 4.
Chapter 4: Making Decisions.
EGR 2261 Unit 4 Control Structures I: Selection
Topics The if Statement The if-else Statement Comparing Strings
Chapter 4: Making Decisions.
JavaScript: Control Statements I
Control Structures – Selection
Topics The if Statement The if-else Statement Comparing Strings
Relational and Logical Operators
Control Structures: Selection Statement
IPC144 Introduction to Programming Using C Week 3 – Lesson 1
3 Control Statements:.
CSC215 Lecture Flow Control.
CSC215 Lecture Control Flow.
Summary Two basic concepts: variables and assignments Basic types:
Chapter 4: Control Structures I (Selection)
Control Structure Chapter 3.
CPS125 Week 5.
2. Second Step for Learning C++ Programming • Data Type • Char • Float
Relational and Logical Operators
Control Structures: Selection Statement
Relational and Logical Operators
Just Enough Java 17-May-19.
Chap 7. Advanced Control Statements in Java
Flow of Control.
Relational and Logical Operators
CSC215 Lecture Control Flow.
Control Structure.
ICS103: Programming in C 4: Selection Structures
Presentation transcript:

Lesson #4 Logical Operators and Selection Statements

Control Structures Control structures combine individual instructions into a single logical unit with one entry point at the top and one exit point at the bottom. 3 kinds of control structures: Sequence (default control structure)‏ Selection (branches)‏ Repetition (loops)‏

Conditions A condition is an expression that is either true or false. Ex: temperature=28; temperature < 0 will be false temperature >20 will be true Comparison (relational) operators are: < > <= >= == !=

Logical Operators A logical expression contains one or more comparison and/or logical operators. The logical operators are: &&: the and operator, true only when all operands are true. ||: the or operator, false only when all operands are false. !: the not operator, false when the operand is true, true when the operand is false.

Updated Operator Precedence Rule 2.1 Function calls 2.2 Unary operators (-, +, !, (int), (double))‏ 2.3 *, /, % 2.4 +, - 2.5 <, <=, >=, > 2.6 ==, != 2.7 && 2.8 || 2.9 = (assignment operator)‏

Fast Evaluation of Logical Expressions int x=3, y=4, z=10, w; w = x == 3 || y < 10; is w true or false? w = x%3-z<x&&z>=x/y || x<y; The trick is to divide the expression in sub-expressions between the || and evaluate the easiest first. As soon as you find a true sub-expression, the whole expression is true.

Fast Evaluation of Logical Expressions When there is no || operator. Divide the expression in parts between the && operators. If one part is false, the expression is false. int a=1, b=2, c=3; Is this expression true or false? a < 5 - 3 && b == 5 - c && b > 4

Building Logical Expressions Are x and y both greater than 10? x > 10 && y > 10 Is either x equal to 1 or 3? x == 1 || x == 3 Is x between y and z? x >= y && x <= z (never y <= x <= z) for example if a=7; 3 <= a <= 5 is true!! Is x an even number? x % 2 == 0

Comparing Characters '9' >= '0' ? 'a' < 'e' ? 'B' <= 'A' ? 'Z' == 'z' ? Is a letter lowercase? letter >= 'a' && letter <= 'z' Does the variable ch contain a letter? (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')‏

Logical Assignment Logical values can be assigned to variables. Logical variables are int in C. int age, senior; scanf ("%d", &age); senior = age >= 65; senior will contain 1 if true and 0 if false.

Integer and Logical Values In C, logical values (true or false) are represented by integer constant and variables. False is always 0. True is all the non-zero values. 1 always means true of course, but 2, 4.5, and –10000 are also values interpreted as true.

Condition Complements The opposite (or complement) of (x == 0) is !(x==0) or (x != 0). The opposite of (x > 3) is !(x > 3) or (x <=3). The opposite of (a == 10 && b > 5) is !(a == 10 && b > 5) or (a != 10 || b <=5)‏ De Morgan's Theorem: The complement of exp1&&exp2 is comp_exp1||comp_exp2. The complement of exp1||exp2 is comp_exp1&&comp_exp2.

The if Statement Syntax of a simple if statement: if (condition)‏ statement if condition is true; else statement if condition is false; Note: Never put a ; after the condition.

A Simple if Statement int temp; printf ("What is the temperature?"); scanf ("%d", &temp); if (temp >= 20)‏ printf ("The temperature is warm.\n"); else printf ("The temperature is cool.\n");

if Statement with Only One Alternative int temp; printf ("What is the temperature?"); scanf ("%d", &temp); if (temp >= 100)‏ printf ("WARNING! Boiling water\n");

if with Compound Statements if statements expect only one statement per branch (true/false). To have more, we use compound statements (enclosed in { }). int temp; printf ("What is the temperature?"); scanf ("%d", &temp); if (temp >= 100)‏ { printf ("WARNING! Boiling water.\n"); printf ("Turn off the heat!\n"); }

Nested if Statements if (temp >= 100)‏ printf ("Boiling water!\n"); Nested ifs are ifs inside ifs. It is good practice to only expand the false branch. The true branch is always terminal. If you need to expand the true branch, reverse the condition and expand the false branch. if (temp >= 100)‏ printf ("Boiling water!\n"); else if (temp <=0)‏ printf ("Freezing water!\n");

Nested if Statements Order is very important with nested ifs: if (noise <= 50)‏ printf ("Quiet\n"); else if (noise <= 70)‏ printf ("Intrusive\n"); else if (noise <=90)‏ printf ("Deafening\n"); else printf ("Dangerous\n");

The switch Statement The switch statement is a useful alternative for multiple branches (not just true and false). It works not with conditions but with control values. The control values must be int or char only, never double. The control values must be discrete, never ranges.

The switch Statement Syntax: switch (control value)‏ { case value1: statement(s) if value1 or value2 matches the control value; break; case value3: statement(s) if value3 matches the control value; /* other possible values here */ default: statement(s) if no case value matches the control value; }

A switch Statement Example switch (color)‏ { case 'R': case 'r': printf ("STOP!"); break; case 'Y': case 'y': printf ("CAUTION!"); case 'g': case 'G': printf ("GO!"); default: printf ("Invalid color"); }

End of lesson