Selection in C.

Slides:



Advertisements
Similar presentations
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Advertisements

Subject: Information Technology Grade: 10
1 CSC103: Introduction to Computer and Programming Lecture No 8.
Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman Chapter 4 (Conditional Statements) © CPCS
Chapter 6 Horstmann Programs that make decisions: the IF command.
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.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
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.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 4- 1.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
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;
C++ Programming, Namiq Sultan1 Chapter 4 Making Decisions Namiq Sultan University of Duhok Department of Electrical and Computer Engineerin Reference:
1 Selection Structures. 2 Making Decisions Sample assignment statements to figure worker pay with possible overtime PayAmount = Hours * Rate PayAmount.
Chapter 4 Making Decisions
1 9/28/07CS150 Introduction to Computer Science 1 Logical Operators and if/else statement.
Programming Control Flow. Sequential Program S1 S2 S5 S4 S3 int main() { Statement1; Statement2; … StatementN; } Start End.
1  Ex: Declare a variable to store user’s age: int age; Prompt the user to enter his/her age: printf (“ How old are you? “); Read / scan the entered value.
C++ Operators CS242 COMPUTER PROGRAMMING T.Banan Al-Hadlaq.
Chapter 3 Decision Structures 1. Contents 1.The if Statement 2.The if-else Statement 3.The if-else-if Statement 4.Nested if Statement 5.Logical Operators.
Computer Science Selection Structures.
Lecture 5 Selection Control Structures Selection Control Structures Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor
Chapter 4 Selection Structures: Making Decisions.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Flow of Control Part 1: Selection
1 CSC103: Introduction to Computer and Programming Lecture No 7.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
A First C Program /* Print a Message */ #include main() { printf("This is a test!\n"); } The program is compiled and run as cc pg32.c  a.out  This is.
Making Decisions. 4.1 Relational Operators Used to compare numbers to determine relative order Operators: > Greater than < Less than >= Greater than.
Chapter 4 Making Decision Csc 125 C++ programming language Fall 2005.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4: Making Decisions.
1 09/15/04CS150 Introduction to Computer Science 1 Life is Full of Alternatives Part 2.
Copyright 2003 Scott/Jones Publishing Making Decisions.
Agenda Basic Logic Purpose if statement if / else statement
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 4: Making Decisions.
Selection-making Decisions Selection allows you to choose between two or more possible program flow --- it lets you make decisions in your program. Examples.
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
Chapter Making Decisions 4. Relational Operators 4.1.
CHAPTER 5 MAKING DECISION Hidayah Elias BFC2042 – Computer Programming.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 4 Making Decisions.
1 CS161 Introduction to Computer Science Topic #8.
©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. CSC INTRO TO COMPUTING - PROGRAMMING If Statement.
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Conditions - compare the values of variables, constants and literals using one or more relational.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 4: Making Decisions 1.
Lecture 6 – Selection FTMK, UTeM – Sem /2014.
Control Statements: Part1  if, if…else, switch 1.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
Introduction to Programming Python Lab 7: if Statement 19 February PythonLab7 lecture slides.ppt Ping Brennan
CSE202: Lecture 5The Ohio State University1 Selection Structures.
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Condition – any expression that evaluates to true/false value Relational operators are BINARY.
Slide 1 Chapter 4 The If…Then Statement  Conditional control structure, also called a decision structure  Executes a set of statements when a condition.
CS0007: Introduction to Computer Programming
Decision making If.. else statement.
Lecture 3 Selection Statements
Chapter 3 Selection Statements
Chapter 4: Making Decisions.
Chapter 4: Making Decisions.
Lecture 3- Decision Structures
Chapter 4: Making Decisions.
Lecture 3 MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz
Javascript Conditionals.
Control Statement Examples
If statement.
Decision making If statement.
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
Presentation transcript:

Selection in C

If Statement: if ( condition) statement(s); One way decision: Referred to as “Open Branch” in the book. Syntax: Flowchart: if ( condition) statement(s); Condition? true Statement(s) false Rest of the program

If / else statement: 2-way decision: Syntax: Flowchart: if (condition) statement(s); else Condition? false true Statement(s) Statement(s) Rest of the program

Exercise: Write a C program to.. Ask the user to enter his/her age. Check the age, if it is > 21, display “ You may drink.”, otherwise, display “Do not drink” regardless of the age, Display “Do not Drink and Drive”.

Explain the solution, using flowchart: Start Declare variable: age Get user’s age Age > 21? false true May not drink You may drink Don’t drink & drive end

Explain the solution using pseudo code: Declare variable to store age. Prompt the user to enter age, Read the entered value into variable If age is > 21 then Display you may drink Otherwise Display you may not drink 5. Display: Do not drink and drive

Translate the logic into C code:

Nested If Statement: An if statement can be nested in the if part or the else part of the if statement. if (condition) …. else … How can you tell where an if statement begins or ends?

Ex1: Assume that: int a = 5, b = 7, c = 10; Rule: else binds with the closest if Ex1: Assume that: int a = 5, b = 7, c = 10; if (a < b) if (c <= a + b) printf( " First place \n“); else printf( "2nd place \n“); printf (“3rd place \n”); Output:

Dangling else: Determine which if the else binds with? Ex2: Assume: a = 12, b = 2, c = 10 if (a < b) if (c <= a + b) printf( " First place \n“); else printf( "2nd place \n“); Output:

Ex: What is the outcome? float score = 75; float gpa = 2.5; if (score > 80) if (gpa > 2.0) printf (“xxx”); else printf(“ ###”);

If / else if statement: The else part of an if statement can be another if statement. if (condition) … else ….

Improved style  more clarity Such nested if / else can be written as: if (condition) … else if (condition) …. else

Example: Examine the student’s test score and assign a letter grade: int score = … char letter; if (score >= 90) { letter = ‘A’; printf(“ Great job! \n”); } else if (score >= 80) letter = ‘B’; else if (score >= 70) …

Rewrite the code with improved style: if (score >= 90) { letter = ‘A’; printf(“ Great job! \n”); } else if (score >= 80) letter = ‘B’; else if (score >= 70) letter = ‘C’; else if (score >= 60) letter = ‘D’; else letter = ‘F’; Printf(“ Your letter grade is: %c”, letter); …

More complex conditions: You may combine 2 or more conditions using logical operators: Logical Operators: Logical and: && C1 (condition1) C2 (condition2) C1 && C2 True False

Application: Check the range of the data: Ex: A healthy heart rate is between 60 and 85. Check the user’s heart rate, and inform the user whether s/he is healthy or not int rate; printf (“ Enter your heart rate: “); Scanf(“%d”, &rate); if ((rate >= 60) && (rate <= 85) ) … else

Logical or: || C1 (condition1) C2 (condition2) C1 || C2 True False

Applications: 1) Checking for invalid rage: Ex1: Check the range of the test score. If it is outside the range of 0 .. 100 end the program: int test; test = 77; if ( (test < 0) || (test > 100) ) { printf (“ Invalid test score..”); printf (“ Program will end now.”); return 1; }

Cont… Ex2: Assume that a company is considering a promotion for employees older than 60, or those who are paid under $20,000. Assume variables: salary and age are declared with some values stored in them. If ( (salary < 20000) || (age > 60) ) printf (“ Give promotion..”); else printf (“ Does not qualify!”);

Cont… Logical not ! Operates on one condition. It negates the value of the condition. Ex1: ! (120 < 100)  Ex2: int veteran = 1; !veteran  Notice: ! (a < b) is the same as (a >= b) ! (a == b) is the same as (a != b)

Precedence Rule: (..) Unary minus, ! * / % + - * / % + - <, <=, >, >= ==, != && || = assignment operator

Evaluate the following expressions: Assume int a = 5, b = 10, c = 15, flag = 1; a < b && b < c flag && b > 10 !(b <= 12) && (a %2 == 0) !flag || c > a int answer; answer = (a > b) || (c > b); Printf (“%d”,answer); C >= a + b