If statement.

Slides:



Advertisements
Similar presentations
Decision Structures - If / Else If / Else. Decisions Often we need to make decisions based on information that we receive. Often we need to make decisions.
Advertisements

July 13 th.  If/ Else if / Else  Variable Scope  Nested if/else's  Switch statements  Conditional Operator.
1 Engineering Problem Solving With C++ An Object Based Approach Chapter 3 Control Structures.
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.
Decisions (Conditional Programming) Chapter 5 (Sec. 5.1 & 5.2)
1 9/26/08CS150 Introduction to Computer Science 1 Logical Operators and if/else statement.
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.
School of Computing Science CMT1000 Ed Currie © Middlesex University 1 CMT1000: Introduction to Programming Ed Currie Lecture 5B: Branch Statements - Making.
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;
The If/Else Statement, Boolean Flags, and Menus Page 180
1 9/28/07CS150 Introduction to Computer Science 1 Logical Operators and if/else statement.
Programming with MATLAB. Relational Operators The arithmetic operators has precedence over relational operators.
Selection in C.
Boolean Algebra Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2009.
19/5/2015CS150 Introduction to Computer Science 1 Announcements  1st Assignment due next Monday, Sep 15, 2003  1st Exam next Friday, Sep 19, 2003  1st.
1 CSC 1401 S1 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design First Edition by Tony Gaddis.
Conditionals & boolean operators
PAGES:51-59 SECTION: CONTROL1 : DECISIONS Decisions.
Reviw: Control Structures
Control Structures 1. Control Structures Java Programming: From Problem Analysis to Program Design, D.S. Malik 2.
Review the following: if-else One branch if Conditional operators Logical operators Switch statement Conditional expression operator Nested ifs if –else.
1 09/15/04CS150 Introduction to Computer Science 1 Life is Full of Alternatives Part 2.
Control structures Algorithm Development Conditional Expressions Selection Statements 1.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
CHAPTER#3 PART1 STRUCTURED PROGRAM DEVELOPMENT IN C++ 2 nd semester King Saud University College of Applied studies and Community Service Csc.
Python Conditionals chapter 5
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
COMP 110: Spring Announcements Lab 1 was due at noon Lab 2 on Friday (Bring Laptops) Assignment 1 is online TA Office hours online 30-min quiz at.
Chapter 4 Control Structures: Selection We introduced the three fundamental control structures from which all programs are developed: 1. Sequence structures.
Decisions. Three Forms of Decision Making in Java if statements (test a boolean expression) switch statements (test an integer expression) conditional.
CONTROL STRUCTURE. 2 CHAPTER OBJECTIVES  Learn about control structures.  Examine relational and logical operators.  Explore how to form and evaluate.
8. DECISION STRUCTURES Rocky K. C. Chang October 18, 2015 (Adapted from John Zelle’s slides)
Expressions and Order of Operations Operators – There are the standard operators: add, subtract, divide, multiply – Note that * means multiply? (No times.
Copyright Curt Hill The Assignment Operator and Statement The Most Common Statement you will use.
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Decision making If.. else statement.
Lecture 3 Selection Statements
Relational Operator and Operations
Basic concepts of C++ Presented by Prof. Satyajit De
Chapter 3 Selection Statements
Engineering Problem Solving with C
Boolean Algebra.
Control Structures and Data Files
Chapter 4: Making Decisions.
CMPT 201 if-else statement
Introduction to programming in java
Engineering Problem Solving with C++, Etter/Ingber
Chapter 4: Decision Structures and Boolean Logic
Selection and Repetition (Loop) Chapter 4 and 5
SELECTION STATEMENTS (1)
And now for something completely different . . .
Chapter 4: Decision Structures and Boolean Logic
Decision making If statement.
Pages:51-59 Section: Control1 : decisions
Selection Control Structure
Summary Two basic concepts: variables and assignments Basic types:
Chapter 2 Programming Basics.
CS2011 Introduction to Programming I Selections (I)
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
Life is Full of Alternatives
Life is Full of Alternatives
CS 101 First Exam Review.
Pages:51-59 Section: Control1 : decisions
HNDIT11034 More Operators.
Chapter 4: Decision Structures and Boolean Logic
REPETITION Why Repetition?
Presentation transcript:

If statement

Conditional Expressions If statement uses conditions A condition is an expression that can be evaluated to be TRUE (any value > 0) or FALSE (value of 0) Conditional Expression is composed of expressions combined with relational and/or logical operators

Relational Operators == equality (x == 3) != non equality (y != 0) < less than (x<y) > greater than (y>10) <= less than equal to (x<=0) >= greater than equal to (x>=y) !!! a==b vs. a=b !!!

Examples a < b x+y >= k/m fabs(denum) < 0.0001

Logical Operators ! not !(x==0) && and (x>=0) && (x<=10) || or (x>0) || (x<0) A B A && B A || B !A !B False True

Examples a<b<c is not the same as (a<b) && (b<c) a+b * 2 < 5 && 4>=a/2 || t-2 < 10 a=3, b=5, t=4

Precedence for Arithmetic, Relational, and Logical Operators

Exercise Assume that following variables are declared a = 5.5 b = 1.5 k = -3 Are the following true or false a < 10.0 + k a + b >= 6.5 k != a-b !(a == 3*b) a<10 && a>5 fabs(k)>3 || k<b-a

If statement if(Boolean expression) statement; //single statement { //more than one statement statement1; . statement n; }

Examples if (x>0) { x=sqrt(x); } k++; 4 2 x k -2 2 x k 4 2 x k -2 2

if - else statement if (boolean expression) statement1; else { statement block1 } statement block2 x=y; x=y; k=3;

If-else statement What does the following program do? Assume that x, y, temp are declared. if (x>y) temp = x; else temp = y; 1 3 ? x y temp 3 1 ? x y temp

If-else statement Split the following statement into two separate if if (x>y) temp = x; else temp = y; if (x>y) temp = x; if (x<=y) temp = y;

Exercise Write an if-else statement to find both the maximum and minimum of two numbers. Assume that x,y, min, max are declared. if (x>y) Ex: x = 10, y = 5 { y = 3, y = 4 max = x; x = 6, y = 6 min = y; } else { max = y; min = x;

nested if-else if(x > y) if(y < z) k++; if (y<z) k++; else m++; j++; if (y<z) k++; else m++;

Exercise int x=9, y=7, z=2, k=0, m=0, j=0; if (x > y) if(y < z) else m++; j++; What are the values of j, k and m after execution of if statement? 9 7 x y 2 z k m j

Exercise int x=3, y=7, z=2, k=0, m=0, j=0; if (x > y) if(y < z) else m++; j++; What are the values of j, k and m after execution of if statement? 3 7 x y 2 z k m j

Exercise Given a score and the following grading scale write a program to find the corresponding grade. 80-100 A 60-79 B 40-59 C 0-39 D

Solution if ((score >= 80) && (score <=100)) grade = 'A'; grade = 'B'; if ((score >= 40) && (score <= 59)) grade = 'C'; if ((score >= 0) && (score <= 39)) grade = 'D';

Nested if - else statement if (boolean expression) { statement block1 } else if (boolean expression2 { statement block2 else if (Boolean expression3) { statement block3 else { statement block4

Solution if ((score >= 80) && (score <=100)) grade = 'A'; else if ((score >= 60) && (score <= 79)) grade = 'B'; else if ((score >= 40) && (score <= 59)) grade = 'C'; else if ((score >= 0) && (score <= 39)) grade = 'D'; else grade = ‘I’;

Alternate Solution grade = ‘I’; else if (score >= 80) grade = 'A'; grade = 'B'; else if (score >= 40) grade = 'C'; else if (score >= 0) grade = 'D'; else

Exercise Write a program that reads 3 numbers a, b and c from user and computes minimum, maximum and median of the numbers. Example: a = 2, b = 5, c = 3 minimum = 2, maximum = 5, median = 3 a = 2, b = 2, c = 3 minimum = 2, maximum = 3, median = 2

Exercise continued Find minimum of 3 numbers Write an if statement to check if minimum is a if (condition) what should min = a; be condition? Write an if statement to check if minimum is b Write an if statement to check if minimum is c Combine 1-3 into a single nested if-else statement Similarly find maximum of 3 numbers Find median of 3 numbers

Minimum if ((a<b) && (a<c)) min = a; else if ((b<a) && (b<c)) min = b; else if ((c<a) && (c<b)) min = c;

Maximum if ((a>b) && (a>c)) max = a; else if ((b>a) && (b>c)) max = b; else if ((c>a) && (c>b)) max = c;

Median median = b; else if ((c<b) && (b<a)) if ((a<b) && (b<c)) median = b; else if ((c<b) && (b<a)) else if ((b<a) && (a<c)) median = a; else if ((c<a) && (a<b)) else if ((b<c) && (c<a)) median = c; else if ((a<c) && (c<b))

Alternate Solution for Median if (((a<b) && (b<c)) || ((c<b) && (b<a))) median = b; else if (((b<a) && (a<c)) || ((c<a) && (a<b))) median = a; else if (((b<c) && (c<a)) || ((a<c) && (c<b))) median = c;

Exercise Write a program that reads a point (x, y) from user and prints the region it resides in. For example Enter x, y: 3 -1 Point in Region 4 Enter x, y: -1 -5 Point in Region 3 Region 2 Region 1 Region 3 Region 4

Solution if ((x>0) && (y>0)) printf("Region 1\n"); else if ((x<0) && (y>0)) printf("Region 2\n"); else if ((x<0) && (y<0)) printf("Region 3\n"); else if ((x>0) && (y<0)) printf("Region 4\n");

Alternate Solution if (x>0) if (y>0) printf("Region1\n"); else