Conditional Statement

Similar documents
Conditional Statement

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

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

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

Introduction to C Programming

Simple if Simple if-else Nested if - else Notes for if-else statement Logical operators

Unit-2 (Operators) ANAND KR.SRIVASTAVA

Conditional Expressions

PDS Lab Section 16 Autumn Tutorial 3. C Programming Constructs

C: How to Program. Week /Mar/05

Chapter 2, Part III Arithmetic Operators and Decision Making

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

Chapter 2 - Introduction to C Programming

Decision Making -Branching. Class Incharge: S. Sasirekha

Chapter 4 C Program Control

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

DECISION MAKING STATEMENTS

Operators And Expressions

Fundamentals of Programming Session 9

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

C Program Control. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan

Problem Solving through Programming In C Prof. Anupam Basu Department of Computer Science & Engineering Indian Institute of Technology, Kharagpur

Chapter 4 C Program Control

Chapter 4. Flow of Control

Decision Control Structure. Rab Nawaz Jadoon DCS. Assistant Professor. Department of Computer Science. COMSATS IIT, Abbottabad Pakistan

Operators and Expressions in C & C++ Mahesh Jangid Assistant Professor Manipal University, Jaipur

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

3. EXPRESSIONS. It is a sequence of operands and operators that reduce to a single value.

Introduction to C Programming. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan

Lecture 3 Tao Wang 1

COMP-202: Foundations of Programming. Lecture 3: Boolean, Mathematical Expressions, and Flow Control Sandeep Manjanna, Summer 2015

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

CSE101-lec#12. Designing Structured Programs Introduction to Functions. Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU

Overview: Programming Concepts. Programming Concepts. Names, Values, And Variables

Overview: Programming Concepts. Programming Concepts. Chapter 18: Get With the Program: Fundamental Concepts Expressed in JavaScript

COMP 208 Computers in Engineering

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

More Things We Can Do With It! Overview. Circle Calculations. πr 2. π = More operators and expression types More statements

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

Information Science 1

Chapter 4: Expressions. Chapter 4. Expressions. Copyright 2008 W. W. Norton & Company. All rights reserved.

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

LESSON 1. A C program is constructed as a sequence of characters. Among the characters that can be used in a program are:

Programming for Engineers Introduction to C

Chapter 1 & 2 Introduction to C Language

Programming for Engineers Iteration

Flow Chart. The diagrammatic representation shows a solution to a given problem.

Chapter 4: Basic C Operators

Department of Computer Science

Computer Programming CS F111

Decision Making and Loops

BRANCHING if-else statements

Note: unless otherwise stated, the questions are with reference to the C Programming Language. You may use extra sheets if need be.

Operators and Expressions:

COP 2000 Introduction to Computer Programming Mid-Term Exam Review

Expression and Operator

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal

Boolean Algebra Boolean Algebra

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

Syntax Analysis Check syntax and construct abstract syntax tree

Conditionals. CSE / ENGR 142 Programming I. Chapter 4. Conditional Execution. Conditional ("if ") Statement. Conditional Expressions

Computational Expression

Topics. Chapter 5. Equality Operators

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal

4.1. Structured program development Overview of C language

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

Operators in java Operator operands.

15 FUNCTIONS IN C 15.1 INTRODUCTION

بسم اهلل الرمحن الرحيم

MA 511: Computer Programming Lecture 3: Partha Sarathi Mandal

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

Lecture 5 Tao Wang 1

A3-R3: PROGRAMMING AND PROBLEM SOLVING THROUGH 'C' LANGUAGE

Information Science 1

Flow Control. So Far: Writing simple statements that get executed one after another.

Decision Making. if Statement. Decision Making

CpSc 111 Lab 5 Conditional Statements, Loops, the Math Library, and Redirecting Input

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

Operators & Expressions

Computer Science & Engineering 150A Problem Solving Using Computers

Data Types and Variables in C language

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

Lecture 5. Review from last week. Selection Statements. cin and cout directives escape sequences

7/8/10 KEY CONCEPTS. Problem COMP 10 EXPLORING COMPUTER SCIENCE. Algorithm. Lecture 2 Variables, Types, and Programs. Program PROBLEM SOLVING

Lesson 7 Part 2 Flags

Basics of Programming

By the end of this section you should: Understand what the variables are and why they are used. Use C++ built in data types to create program

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

CSE 1001 Fundamentals of Software Development 1. Identifiers, Variables, and Data Types Dr. H. Crawford Fall 2018

CS 112 Introduction to Programming

CS 112 Introduction to Programming

In Java, data type boolean is used to represent Boolean data. Each boolean constant or variable can contain one of two values: true or false.

Contents. Jairo Pava COMS W4115 June 28, 2013 LEARN: Language Reference Manual

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

Operators and Expression. Dr Muhamad Zaini Yunos JKBR, FKMP

MIDTERM EXAMINATION - CS130 - Spring 2003

Constants and Variables

Statements. Control Flow Statements. Relational Operators. Logical Expressions. Relational Operators. Relational Operators 1/30/14

Relational and Logical Statements

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

Transcription:

Conditional Statement 1

Logical Expressions n Uses relational and logical operators n Informally, specifies a condition which can be true or false n Evaluates to value 0 or 1 0 implies the condition is false 1 implies the condition is true 2

Logical Expressions (count <= 100) ((math+phys+chem)/3 >= 60) ((sex == M ) && (age >= 21)) ((marks >= 80) && (marks < 90)) ((balance > 5000) (no_of_trans > 25)) (! (grade == A )) 3

Relational Operators n Used to compare two quantities. < is less than > is greater than <= is less than or equal to >= is greater than or equal to == is equal to!= is not equal to 4

Examples 10 > 20 is false, so value is 0 25 < 35.5 is true, so value is 1 12 > (7 + 5) is false, so value is 0 32!= 21 is true, so value is 1 n When arithmetic expressions are used on either side of a relational operator, the arithmetic expressions will be evaluated first and then the results compared a + b > c d is the same as (a+b) > (c+d) 5

Logical Operators Logical AND (&&) n Evalutes to 1 if both the operands are non-zero Logical OR ( ) n Result is true if at least one of the operands is non-zero X Y X && Y X Y 0 0 0 0 0 non-0 0 non-0 non-0 0 0 non-0 non-0 non-0 non-0 non-0 6

Contd n Unary negation operator (!) Single operand Value is 0 if operand is non-zero Value is 1 if operand is 0 7

Example n (4 > 3) && (100!= 200) 4 > 3 is true, so value 1 100!= 200 is true so value 1 Both operands 1 for &&, so final value 1 n (!10) && (10 + 20!= 200) 10 is non-0, so value!10 is 0 10 + 20!= 200 is true so value 1 Both operands NOT 1 for &&, so final value 0 n (!10) (10 + 20!= 200) Same as above, but at least one value non-0, so final value 1 8

n a = 3 && b = 4 No parenthesis, so need to look at precedence and associativity = has higher precedence than && b=4 is an assignment expression, evaluates to 4 a = 3 is an assignment expression, evaluates to 3 Both operands of && are non-0, so final value of the logical expression is 1 n Note that changing to b = 0 would have made the final value 0 9

Example: Use of Logical Expressions void main () { int i, j; scanf( %d%d,&i,&j); printf ( %d AND %d = %d, %d OR %d=%d\n, i,j,i&&j, i,j, i j) ; If 3 and 0 are entered from keyboard, output will be 3 AND 0 = 0, 3 OR 0 = 1 10

Conditional Statements n Allow different sets of instructions to be executed depending on truth or falsity of a logical condition n Also called Branching n How do we specify conditions? Using expressions n non-zero value means condition is true n value 0 means condition is false Usually logical expressions, but can be any expression n The value of the expression will be used 11

Branching: if Statement if (expression) statement; if (expression) { Block of statements; 12

Branching: if Statement if (expression) statement; if (expression) { Block of statements; The condition to be tested is any expression enclosed in parentheses. The expression is evaluated, and if its value is non-zero, the statement is executed. 13

marks >= 40 true print Passed print Good luck false 14

marks >= 40 true print Passed print Good luck A decision can be made on any expression. zero - false nonzero - true false 15

marks >= 40 true print Passed print Good luck A decision can be made on any expression. zero - false nonzero - true false if (marks >= 40) { printf( Passed \n ); printf( Good luck\n ); printf ( End\n ) ; 16

Branching: if-else Statement if (expression) { Block of statements; else { Block of statements; if (expression) { Block of statements; else if (expression) { Block of statements; else { Block of statements; 17

Grade Computation void main() { int marks; scanf( %d, &marks); if (marks >= 80) printf ( A ) ; else if (marks >= 70) printf ( B ) ; else if (marks >= 60) printf ( C ) ; else printf ( Failed ) ; 18

Find the larger of two numbers START READ X, Y YES IS X>Y? NO OUTPUT X OUTPUT Y STOP STOP 19

Find the larger of two numbers YES OUTPUT X START READ X, Y IS X>Y? NO OUTPUT Y void main () { int x, y; scanf ( %d%d, &x, &y) ; if (x > y) printf ( %d\n, x); else printf ( %d\n, y); STOP STOP 20

Largest of three numbers START READ X, Y, Z YES IS X > Y? NO Max = X Max = Y YES OUTPUT Max IS Max > Z? NO OUTPUT Z STOP STOP 21

START READ X, Y, Z YES IS X > Y? NO Max = X Max = Y YES OUTPUT Max IS Max > Z? NO OUTPUT Z STOP STOP 22

Max = X YES YES START READ X, Y, Z IS X > Y? IS Max > Z? NO Max = Y NO void main () { int x, y, z, max; scanf ( %d%d%d,&x,&y,&z); if (x > y) max = x; else max = y; if (max > z) printf ( %d, max) ; else printf ( %d,z); OUTPUT Max OUTPUT Z STOP STOP 23

Another version void main() { int a,b,c; scanf ( %d%d%d, &a, &b, &c); if ((a >= b) && (a >= c)) printf ( \n The largest number is: %d, a); if ((b >= a) && (b >= c)) printf ( \n The largest number is: %d, b); if ((c >= a) && (c >= b)) printf ( \n The largest number is: %d, c); 24

Confusing Equality (==) and Assignment (=) Operators n Dangerous error Does not ordinarily cause syntax errors Any expression that produces a value can be used in control structures Nonzero values are true, zero values are false n Example: if ( paycode = 4 WRONG! ) Will always print the line printf( "You get a bonus!\n" ); 25

Nesting of if-else Structures n It is possible to nest if-else statements, one within another n All if statements may not be having the else part Confusion?? n Rule to be remembered: An else clause is associated with the closest preceding unmatched if 26

Dangling else problem if (exp1) if (exp2) stmta else stmtb if (exp1) { if (exp2) stmta else stmtb OR if (exp1) { if (exp2) stmta else stmtb? Which one is the correct interpretation? Give braces explicitly in your programs to match the else with the correct if to remove any ambiguity 27

More Examples if e1 s1 else if e2 s2 if e1 s1 else if e2 s2 else s3? if e1 if e2 s1 else s2 else s3 28

Answers if e1 s1 if e1 s1 else if e2 s2 else { if e2 s2 if e1 s1 if e1 s1 else if e2 s2 else { if e2 s2 else s3 else s3 if e1 if e2 s1 if e1 { if e2 s1 else s2 else s2 else s3 else s3 29