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.

Slides:



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

Chapter 4 Selection Structures: if and switch Statements Part I J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering.
BBS514 Structured Programming (Yapısal Programlama)1 Selective Structures.
1 CSC103: Introduction to Computer and Programming Lecture No 8.
July 13 th.  If/ Else if / Else  Variable Scope  Nested if/else's  Switch statements  Conditional Operator.
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
Control Structures 4 Control structures control the flow of execution of a program 4 The categories of control structures are: –Sequence –Selection –Repetition.
1 9/26/08CS150 Introduction to Computer Science 1 Logical Operators and if/else statement.
Boolean Types & Compound Conditionals CSC 1401: Introduction to Programming with Java Lecture 4 – Part 3 Wanda M. Kunkle.
Conditions What if?. Flow of Control The order of statement execution is called the flow of control Unless specified otherwise, the order of statement.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
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;
1 Selection Structures. 2 Making Decisions Sample assignment statements to figure worker pay with possible overtime PayAmount = Hours * Rate PayAmount.
Chapter 4 Making Decisions
Fall 2006AE6382 Design Computing1 Relational and Logical Operators Use relational operators to test two values Work with values of true and false Compare.
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.
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.
Selection in C.
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.
Conditional Statement
© Copyright 2013 by Pearson Education, Inc. All Rights Reserved.1 Chapter 3 Selections.
IF-ELSE IF-ELSE STATEMENT SWITCH-CASE STATEMENT Computer Programming Asst. Prof. Dr. Choopan Rattanapoka and Asst. Prof. Dr. Suphot Chunwiphat.
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.
Selection Structure If... Then.. Else Case. Selection Structure Use to make a decision or comparison and then, based on the result of that decision or.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Computer Science 111 Fundamentals of Programming I Making Choices with if Statements.
Chapter 3:Decision Structures.  3.1 The if Statement  3.2 The if-else Statement  3.3 The if-else-if Statement  3.4 Nested if Statements  3.5 Logical.
Programming Fundamental Slides1 Data Types, Identifiers, and Expressions Topics to cover here: Data types Variables and Identifiers Arithmetic and Logical.
1 09/15/04CS150 Introduction to Computer Science 1 Life is Full of Alternatives Part 2.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X1 Chapter 3 Control Statements.
Agenda Basic Logic Purpose if statement if / else statement
Selection-making Decisions Selection allows you to choose between two or more possible program flow --- it lets you make decisions in your program. Examples.
Computer Programming Control Structure
Arithmetic Expressions in C++. CSCE 1062 Outline Data declaration {section 2.3} Arithmetic operators in C++ {section 2.6} Mixed data type arithmetic in.
Structuring Data: Arrays ANSI-C. Representing multiple homogenous data Problem: Input: Desired output:
Control statements Mostafa Abdallah
©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. CSC INTRO TO COMPUTING - PROGRAMMING If Statement.
CONTROL STRUCTURE. 2 CHAPTER OBJECTIVES  Learn about control structures.  Examine relational and logical operators.  Explore how to form and evaluate.
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Conditions - compare the values of variables, constants and literals using one or more relational.
5.02B Decision Making Structure (part 2). Compound Boolean Expressions.
Control Statements: Part1  if, if…else, switch 1.
Operators A binary operator combines two values to get one result: x OP y where OP is any binary operators such as +, -, *, /, ==, !=, >, &&, or even =.
1 CSC103: Introduction to Computer and Programming Lecture No 9.
Slide 1 Chapter 4 The If…Then Statement  Conditional control structure, also called a decision structure  Executes a set of statements when a condition.
Chapter 4: Control Structures I J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition Second.
I F S TATEMENTS Tarik Booker CS 290 California State University, Los Angeles.
CS0007: Introduction to Computer Programming
Lesson #4 Logical Operators and Selection Statements.
Lesson #4 Logical Operators and Selection Statements.
Decision making If.. else statement.
Lecture 3 Selection Statements
Chapter 3 Selection Statements
Data Types, Identifiers, and Expressions
Expressions An expression is a portion of a C++ statement that performs an evaluation of some kind Generally requires that a computation or data manipulation.
Chapter 3 Control Statements Lecturer: Mrs Rohani Hassan
Lecture 3 MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz
Javascript Conditionals.
Chapter 3 Selections Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1.
If statement.
Computers & Programming Languages
Decision making If statement.
Comparing Strings Strings can be compared using the == and != operators String comparisons are case sensitive Strings can be compared using >, =, and.
CS2011 Introduction to Programming I Selections (I)
Life is Full of Alternatives
Life is Full of Alternatives
CprE 185: Intro to Problem Solving (using C)
Control Structure.
Lecture 9: Implementing Complex Logic
Presentation transcript:

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 if (condition) …. else …

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

4 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) …

5 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); …

6 True / false in C:  How does the computer store true or false internally? True is stored as 1 False is stored as 0 Ex: what is the outcome? printf (“%d”, 5 < 100);

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

8 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

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

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

11 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 60) ) printf (“ Give promotion..”); else printf (“ Does not qualify!”);

12 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) ! (a == b) is the same as (a != b)

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

14 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