If and If-Else Statements 1. Motivation 2 Need to build an intelligent program for an A/C to set the temperature between 20-30 deg C. How?? Too Cold Turn.

Slides:



Advertisements
Similar presentations
Nested if-else Statements.  Should be indented to make the logic clear.  Nested statement executed only when the branch it is in is executed. For example,
Advertisements

08/2012Tanya Mishra1 EASYC for VEX Cortex Llano Estacado RoboRaiders FRC Team 1817.
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman Chapter 4 (Conditional Statements) © CPCS
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
Control Flow C and Data Structures Baojian Hua
1 Objectives You should be able to describe: Relational Expressions The if-else Statement Nested if Statements The switch Statement Common Programming.
Conditions What if?. Flow of Control The order of statement execution is called the flow of control Unless specified otherwise, the order of statement.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 4- 1.
1 Chapter 3 Flow of Control. 2 Outline  How to specify conditions?  Relational, Equality and Logical Operators  Statements  Statements: compound statement.
Aalborg Media Lab 23-Jun-15 Software Design Lecture 6 “Conditionals and Loops”
Control Structures Control structures control the flow of program execution. 3 types of control structures: sequence, selection.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Control Statements.
Programming Control Flow. Sequential Program S1 S2 S5 S4 S3 int main() { Statement1; Statement2; … StatementN; } Start End.
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
True or False Unit 3 Lesson 7 Building Blocks of Decision Making With Additions & Modifications by Mr. Dave Clausen True or False.
Spring 2005, Gülcihan Özdemir Dağ Lecture 3, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 3 Outline 3.1 Introduction.
Computer Science Selection Structures.
CPS120: Introduction to Computer Science Decision Making in Programs.
CMSC 104, Version 8/061L11Relational&LogicalOps.ppt Relational and Logical Operators Topics Relational Operators and Expressions The if Statement The if-else.
Conditional Statements For computer to make decisions, must be able to test CONDITIONS IF it is raining THEN I will not go outside IF Count is not zero.
Programming in Java (COP 2250) Lecture 11 Chengyong Yang Fall, 2005.
2 Objectives You should be able to describe: Relational Expressions Relational Expressions The if-else Statement The if-else Statement Nested if Statements.
Repetitive Structures BBS514 Structured Programming (Yapısal Programlama)1.
Flow of Control Part 1: Selection
A Program is nothing but the execution of sequence of one or more Instructions. On the basis of application it is essential. To Alter the flow of a program.
Lecture 2: Logical Problems with Choices. Problem Solving Before writing a program Have a thorough understanding of the problem Carefully plan an approach.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 5.
 2007 Pearson Education, Inc. All rights reserved Structured Program Development in C.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 21, 2005 Lecture Number: 10.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4: Making Decisions.
Conditional Structures UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) ADNAN BABAR MT14028 CR
Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection Structure 3.6The.
Lesson - 5. Introduction While programming, we usually need to decide the path of the program flow according to the parameters and conditions. Actually.
CCSA 221 Programming in C CHAPTER 6 MAKING DECISIONS 1.
Chapter 4 Controlling Execution CSE Objectives Evaluate logical expressions –Boolean –Relational Change the flow of execution –Diagrams (e.g.,
Week 8: Decisions Bryan Burlingame 21 October 2015.
Chapter Making Decisions 4. Relational Operators 4.1.
Week 4 Program Control Structure
IT CS 200: C ONDITION Lect. Napat Amphaiphan. T HE ABILITY TO CONTROL THE FLOW OF YOUR PROGRAM, LETTING IT MAKE DECISIONS ON WHAT CODE TO EXECUTE 2.
CPS120: Introduction to Computer Science Decision Making in Programs.
Application development with Java Lecture 6 Rina Zviel-Girshin.
A First Book of C++ Chapter 4 Selection. Objectives In this chapter, you will learn about: –Relational Expressions –The if-else Statement –Nested if Statements.
Conditional Control Structures Chapter 5. Goals and Objectives Understand conditional control structures. Demonstrate the use of decision structures to.
CPS120: Introduction to Computer Science Decision Making in Programs.
Control structures in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
BIL 104E Introduction to Scientific and Engineering Computing Lecture 5.
Lecture #8 SWITCH STATEMENT By Shahid Naseem (Lecturer)
A First Book of C++ Chapter 4 Selection.
Lecture 3 Selection Statements
Chapter 3 Selection Statements
Chapter 5 The if Statement
Selections Java.
Decisions Chapter 4.
Chapter 4: Making Decisions.
EKT150 INTRODUCTION TO COMPUTER PROGRAMMING
Lecture 4 - Loops UniMAP EKT120 Sem 1 08/09.
Week 4 – Repetition Structures / Loops
Condition Statements.
Chapter 4: Making Decisions.
Programming Fundamentals
Looping.
Lecture 2: Logical Problems with Choices
Structured Program
Program Flow.
Selection Control Structure
Control Structures.
Presentation transcript:

If and If-Else Statements 1

Motivation 2 Need to build an intelligent program for an A/C to set the temperature between deg C. How?? Too Cold Turn off the compressor Too Hot Turn on the compressor

Outline Flow Chart Programming with Decisions (2 or more choices) If-Else Statements Switch Statement 3

Flowchart Flowchart: A Diagram representing sequential steps in the process or algorithm in order to achieve specific tasks Components in Flowchart: Sequences, Selections, Iterations symbols Creating a flowchart before the actual coding (in C) helps one to finish the task in a more systematic and timely manner 4

Symbols in Flowchart 5 Terminal symbol Process or statement symbol Input/output symbol Decision symbol, if the condition is true, perform task 1, if not, perform task 2 Start X=70; Get, print Condition

Flowchart (Continued) 6 connector symbol (Coming in from many directions) link symbol (Used in case you are out of space to write) function symbol (Indicating there is more details in the function) flow line ( Direction of the flow in commands/statements ) repeated line (Indicating repeated steps in flow charts)

1 Flowchart Showing a program that decides if a new car needs an oil change. 7 Start…..Once the car’s age and the mileage are entered into its database, the program will suggest an oil change if the age is more than 6 months or the mileage is more than If both conditions are false, then no oil change is needed. The END….

(Programming with selections) Instead of sequential flow of controls/statements, when you need to select a path to go (from 2 choices), the decision is based on the. Two types of checking conditions: 1. Relational expression 2. Logical expression which has value of 1 (or positive integer) when it is TRUE, and 0 when it is false 8

Relational Expression Relational operators: 9 Relational operators Meanings <Less than >Greater than <=Less than or equal >=More than or equal ==Equal !=Not equal Examples of correct expression:  ( num_student < 3 )  ( sum_money > 200 )  -1.3 >= (2.0*x + 3)  ( score != -999)

Relational Expression (Cont’) 10 Incorrect expression:  a =< b //out of order  a < = b //space is not allowed  a >> b //refers shift expression  a = b //assignment statement  a = = b-1 //space is not allowed Note: Machine has precision issues (Round off). Do not use Float in the relational expression for checking the equal or not equal unless you have to

Logical Expression Logical operators: 11 Logical operators Meanings !(NOT, negation) &&Logic AND ||Logic OR Example: a=5;b=10;c=1;d=0; (a<b)&&(c) True (1) c&&d False (0) (b>a) || (c==1) True (1)!(a>d)False (0)

Logical Expression Truth Table 12 expression exp1exp2exp1 && exp2exp1 || exp

Logical Expression (Continued) 13 Short Cut  For expr1 && expr2, If we know that expr1 is false (0). Does not matter what value expr2 is, the result is always false. -For expr1 || expr2, if expr1 is not zero, the result is true no matter what value expr2 is.

If Statement If (Condition) is true, the statement_1 is executed If (Condition) is false, the statement_1 is NOT executed 14 if (condition) statement_1; Flowchart If-single statement

Example: If-Statement (Truth Table) 15 #include void main() { int expr1,expr2; printf("Enter expr 1: "); scanf("%d",&expr1); printf("Enter expr 2: "); scanf("%d",&expr2); ////////////////////////////////////// if (expr1 && expr2)/* AND*/ printf("expr1 && expr2 is: True \n"); if (!(expr1 && expr2)) printf("expr1 && expr2 is: False \n"); if ((expr1 || expr2) == 1)/* OR */ printf("expr1 || expr2 is: True"); if ((expr1 || expr2) == 0) printf("expr1 || expr2 is: False"); /////////////////////////////////////// printf("\n"); }

If Compound Statements Two or more statements to be executed when if (condition) is true or false, they are grouped using {………} 16 if (condition) { statement1; statement2; … } Flowchart If-multiple statement

Example: If Compound Statement 17 #include void main() { int expr1,expr2; printf("Enter expr 1: "); scanf("%d",&expr1); printf("Enter expr 2: "); scanf("%d",&expr2); ////////////////////////////////////// if (expr1 && expr2)/* AND*/ { printf("Since expr1 && expr2 is: True \n"); printf("So, expr1 || expr2 is: True"); } if ((expr1 || expr2) == 0) { printf("Since, expr1 || expr2 is: False\n"); printf("So, expr1 || expr2 is: False"); } if (( (expr1 && expr2) == 0) && (expr1||expr2) ) { printf("expr1 && expr2 is: False\n"); printf("But, expr1 || expr2 is: True"); } /////////////////////////////////////// printf("\n"); }

If-Else (compound) Statement If-else statement, There are actions (statements) for both TRUE and FALSE conditions 18 if (condition) { statement1; statement2; … } else { statement3; statement4; … } Flowchart If-Else statement

If-else statement 19 #include void main() { int expr1,expr2; printf("Enter expr 1: "); scanf("%d",&expr1); printf("Enter expr 2: "); scanf("%d",&expr2); ////////////////////////////////////// if (expr1 && expr2)/* AND*/ printf("expr1 && expr2 is: True \n"); else printf("expr1 && expr2 is: False \n"); if (expr1||expr2) printf("expr1 || expr2 is: True \n"); else printf("expr1 || expr2 is: False"); /////////////////////////////////////// printf("\n"); }

If-Else Compound Statement: TEMP conversion 20 #include int main( ) {char type; float temp, fahren, celcius; printf("enter temperature: "); scanf("%f", &temp); printf("enter f(farenheit) or c(celcius) for temperature unit: "); scanf("\n%c", &type); if ( type == 'f') { celcius = (5.0/9.0) *(temp -32.0); printf("\nThe equivalent celcius temperature is %6.2f\n", celcius); } else { fahren = (9.0/5.0)*(temp) ; printf("\nThe equivalent farenheit temperature is %6.2f\n", fahren); } return 0; }

If-Else-If Statement If-else-if statement used when there is more than 2 choices of actions (See the Flow Chart) 21 if (condition1) statement1; else if (condition2) statement2; else if (condition3) statement3; else defaultstatement;

Example: If-Else-If Compound Statement Year in the study Program 1 st Year “Freshman” 2 nd Year “Sophomore” 3 rd Year “Junior” 4 th Year “Senior” Others “Super” 22 #include int main( ) { int year; printf("enter student year:"); scanf("%d",&year); if ( year == 1) printf("Freshman"); else if ( year == 2) printf("Sophomore"); else if ( year == 3) printf("Junior"); else if ( year == 4) printf("Senior"); else printf("Super"); return 0; }

Nested If Statement Nested if statement: 2(or more) condition needs to be checked for an action, and another action is taken when only one of the condition is true. 23 if (condition1) statement1; if (condition 2) statement2; else statement3; else statement4; Inner If

Example: Nested If Compound Statement Two numbers are entered. Check if the first number is between [0,9], and Check if the 2 nd number is between [10,99] 24 #include int main( ) { int num1, num2; printf("program to check num1 (1-9) and num2 (10- 99)\n"); printf("enter num1 num2:"); scanf("%d %d",&num1,&num2); if ( num1 >=0 && num1 <= 9) if (num2 >= 10 && num2 <= 99) printf("Both numbers are entered correctly"); else printf("only num1 is entered correctly"); else if (num2 >= 10 && num2 <= 99) printf("only num2 is entered correctly"); else printf("Both numbers are not entered correctly"); return 0; }

Switch Statement switch statement: for more than 1 course of action, similar to if- else-if statement: (). For discrete values only; does not deal with ranges of values 25 switch (expression) { case const1Expr: statement11; statement12; … break; case const2Expr: statement21; statement22; … break; … default: defaultstatement1; defaultstatement2; … } Break statement in every cases except the last (default) one

Example: Switch Statement 26 switch (i) { case 1: case 2: case 3: flag = 0; break; case 4: flag = 1; break; default: process(h); }

Example: Switch Statement 27 #include int main( ) { int year; printf("enter student year:"); scanf("%d",&year); switch (year) { case 1: printf("Freshman"); break; case 2: printf("Sophomore"); break; case 3: printf("Junior"); break;

Example Switch Statement (Continued) 28 case 4: printf("Senior"); break; default: printf("Super"); } return 0; }