Lesson #4. Logical Operators and Selection Statements. 4. Logical Operators and Selection Statements - Copyright Denis Hamelin - Ryerson University

Similar documents
V2 2/4/ Ch Programming in C. Flow of Control. Flow of Control. Flow of control The order in which statements are executed

Flow of Control. Flow of control The order in which statements are executed. Transfer of control

Conditions, logical expressions, and selection. Introduction to control structures

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

Conditional Statement

Conditional Statement

5. Selection: If and Switch Controls

Chapter 4 - Notes Control Structures I (Selection)

Chapter 4: Making Decisions

Chapter 4: Making Decisions

Maltepe University Computer Engineering Department. Algorithms and Programming. Chapter 4: Conditionals - If statement - Switch statement

COP 2000 Introduction to Computer Programming Mid-Term Exam Review

Introduction. Following are the types of operators: Unary requires a single operand Binary requires two operands Ternary requires three operands

Relational operators (1)

Flow Control. CSC215 Lecture

Chapter 4. Flow of Control

C++ Programming: From Problem Analysis to Program Design, Third Edition

1.4 Control Structures: Selection. Department of CSE

Chapter 4: Making Decisions. Copyright 2012 Pearson Education, Inc. Sunday, September 7, 14

Computer Science & Engineering 150A Problem Solving Using Computers

Lecture 5 Tao Wang 1

Expressions. Arithmetic expressions. Logical expressions. Assignment expression. n Variables and constants linked with operators

UNIT IV 2 MARKS. ( Word to PDF Converter - Unregistered ) FUNDAMENTALS OF COMPUTING & COMPUTER PROGRAMMING

C Course. IIT Kanpur. Lecture 3 Aug 31, Rishi Kumar <rishik>, Final year BT-MT, CSE

Conditions, logical expressions, and selection. Introduction to control structures

Decision Making -Branching. Class Incharge: S. Sasirekha

A Look Back at Arithmetic Operators: the Increment and Decrement

COMP 208 Computers in Engineering

Sudeshna Sarkar Dept. of Computer Science & Engineering. Indian Institute of Technology Kharagpur

There are algorithms, however, that need to execute statements in some other kind of ordering depending on certain conditions.

Expression and Operator

Control Structures. Lecture 4 COP 3014 Fall September 18, 2017

EDIABAS BEST/2 LANGUAGE DESCRIPTION. VERSION 6b. Electronic Diagnostic Basic System EDIABAS - BEST/2 LANGUAGE DESCRIPTION

Programming for Engineers Iteration

DECISION MAKING STATEMENTS

If Control Construct

Overview of C, Part 2. CSE 130: Introduction to Programming in C Stony Brook University

CS313D: ADVANCED PROGRAMMING LANGUAGE

(4-2) Selection Structures in C H&K Chapter 4. Instructor - Andrew S. O Fallon CptS 121 (September 12, 2018) Washington State University

Chapter 4 C Program Control

Topics. Chapter 5. Equality Operators

Operators And Expressions

Computers Programming Course 7. Iulian Năstac

Control Structure: Selection

Boolean Algebra Boolean Algebra

Lesson 04. Control Structures I : Decision Making. MIT 31043, VISUAL PROGRAMMING By: S. Sabraz Nawaz

LECTURE 04 MAKING DECISIONS

STUDENT OUTLINE. Lesson 8: Structured Programming, Control Structures, if-else Statements, Pseudocode

Programming: Java. Chapter Objectives. Control Structures. Chapter 4: Control Structures I. Program Design Including Data Structures

Problem Solving and 'C' Programming

Logical Operators and switch

Introduction to C Programming

DECISION CONTROL AND LOOPING STATEMENTS

Objectives. Chapter 4: Control Structures I (Selection) Objectives (cont d.) Control Structures. Control Structures (cont d.) Relational Operators

Day05 A. Young W. Lim Sat. Young W. Lim Day05 A Sat 1 / 14

LESSON 3 CONTROL STRUCTURES

Constants and Variables

Full file at C How to Program, 6/e Multiple Choice Test Bank

Guide for The C Programming Language Chapter 1. Q1. Explain the structure of a C program Answer: Structure of the C program is shown below:

Will introduce various operators supported by C language Identify supported operations Present some of terms characterizing operators

Chapter 4: Control Structures I (Selection) Objectives. Objectives (cont d.) Control Structures. Control Structures (cont d.

BRANCHING if-else statements

Object oriented programming. Instructor: Masoud Asghari Web page: Ch: 3

BBM 101 Introduc/on to Programming I Fall 2013, Lecture 4

IECD Institute for Entrepreneurship and Career Development Bharathidasan University, Tiruchirappalli 23.

Recognize the correct ordering of decisions in multiple branches Program simple and complex decision

5. Control Statements

PDS Lab Section 16 Autumn Tutorial 3. C Programming Constructs

Flow of Control. Selection. if statement. True and False in C False is represented by any zero value. switch

Control Structures: The IF statement!

A Fast Review of C Essentials Part I

Review for Test 1 (Chapter 1-5)

Logical and Bitwise Expressions

15 FUNCTIONS IN C 15.1 INTRODUCTION

n Group of statements that are executed repeatedly while some condition remains true

c) Comments do not cause any machine language object code to be generated. d) Lengthy comments can cause poor execution-time performance.

School of Computer Science CPS109 Course Notes 5 Alexander Ferworn Updated Fall 15

Week 4 Selection Structures. UniMAP Sem II-11/12 DKT121 Basic Computer Programming 1

C expressions. (Reek, Ch. 5) 1 CS 3090: Safety Critical Programming in C

Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade;

Module 3 SELECTION STRUCTURES 2/15/19 CSE 1321 MODULE 3 1

Java Notes. 10th ICSE. Saravanan Ganesh

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

Week 2. Relational Operators. Block or compound statement. if/else. Branching & Looping. Gaddis: Chapters 4 & 5. CS 5301 Spring 2018.

Chapter Overview. More Flow of Control. Flow Of Control. Using Boolean Expressions. Using Boolean Expressions. Evaluating Boolean Expressions

Kingdom of Saudi Arabia Princes Nora bint Abdul Rahman University College of Computer Since and Information System CS240 BRANCHING STATEMENTS

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath

Chapter 2, Part III Arithmetic Operators and Decision Making

MA 511: Computer Programming Lecture 3: Partha Sarathi Mandal

4.1. Chapter 4: Simple Program Scheme. Simple Program Scheme. Relational Operators. So far our programs follow a simple scheme

In this lab, you will learn more about selection statements. You will get familiar to

Selection Structures II

Computer Programming. Decision Making (2) Loops

Flow Charts. Visual Depiction of Logic Flow

Unit-2 (Operators) ANAND KR.SRIVASTAVA

QUIZ: What value is stored in a after this

C++ Programming: From Problem Analysis to Program Design, Fourth Edition. Chapter 4: Control Structures I (Selection)

Control Statements. If Statement if statement tests a particular condition

In this chapter, you will:

Repetition Structures II

Transcription:

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 subexpressions between the and evaluate the easiest first. As soon as you find a true subexpression, 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 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: case value2: statement(s) if value1 or value2 matches the control value; break; case value3: statement(s) if value3 matches the control value; break; /* 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!"); break; case 'g': case 'G': printf ("GO!"); break; default: printf ("Invalid color"); }

End of lesson