Selection-making Decisions Selection allows you to choose between two or more possible program flow --- it lets you make decisions in your program. Examples.

Slides:



Advertisements
Similar presentations
Fundamental of C programming
Advertisements

Decisions If statements in C.
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Introduction to C Programming
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT3: Conditional Statements CS2311 Computer Programming.
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
Control Flow C and Data Structures Baojian Hua
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
true (any other value but zero) false (zero) expression Statement 2
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.
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
C++ for Engineers and Scientists Third Edition
Programming Control Flow. Sequential Program S1 S2 S5 S4 S3 int main() { Statement1; Statement2; … StatementN; } Start End.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 8P. 1Winter Quarter Control Statements Lecture.
COMP 110 Introduction to Programming Mr. Joshua Stough September 19, 2007.
COMP 110 Introduction to Programming Mr. Joshua Stough September 24, 2007.
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
An Introduction to C Programming Geb Thomas. Learning Objectives Learn how to write and compile a C program Learn what C libraries are Understand the.
Spring 2005, Gülcihan Özdemir Dağ Lecture 3, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 3 Outline 3.1 Introduction.
Lecture 10: Reviews. Control Structures All C programs written in term of 3 control structures Sequence structures Programs executed sequentially by default.
Computer programming Lecture 4. Lecture 4: Outline Making Decisions [chap 6 – Kochan] –The if Statement –The if-else Construct –Logical Operators –Boolean.
Computer Science Department Relational Operators And Decisions.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT3: Conditional Statements CS2311 Computer Programming.
Chapter 3 Control Flow Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University.
Selection Structures (if & switch statements) (CS1123)
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.
CS102 Introduction to Computer Programming Chapter 4 Making Decisions.
WEEK8WEEK8 WEEK8WEEK8 Selection Making Decisions 1 Kulliyyah of ICT INFO 2020 Structured Programming Language.
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.
Relational Operators Relational operators are used to compare two numeric values and create a boolean result. –The result is dependent upon the relationship.
1 Objectives ❏ To understand how decisions are made in a computer ❏ To understand the logical operators: and, or, and not ❏ To understand how a C program.
Control Statements in C 1.Decision making statements 2.Looping statements 3.Branching statements
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Computer Science: A Structured Programming Approach Using C1 5-2 Two-Way Selection The decision is described to the computer as a conditional statement.
Computer Science: A Structured Programming Approach Using C1 5-2 Two-Way Selection The decision is described to the computer as a conditional statement.
CCSA 221 Programming in C CHAPTER 6 MAKING DECISIONS 1.
Week 4 Program Control Structure
1 Agenda If Statement True/False Logical Operators Nested If / Switch Exercises & Misc.
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.
CMSC 104, Version 9/011 Relational and Logical Operators Topics Relational Operators and Expressions The if Statement The if-else Statement Nesting of.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Dr. Sajib Datta Jan 23,  A precedence for each operator ◦ Multiplication and division have a higher precedence than addition and subtraction.
CC213 Programming Applications Week #2 2 Control Structures Control structures –control the flow of execution in a program or function. Three basic control.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Lecture 3.1 Operators and Expressions Structured Programming Instructor: Prof. K. T. Tsang 1.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand how decisions are made in a computer ❏ To understand the logical.
THE DECISIONS CONTROL STRUCTURE! CHAPTER 2. Transfer of control statement: o The statement of computer program are executed one after the other in the.
Decision making If.. else statement.
- Standard C Statements
Introduction to C++ Programming Language
Decisions Chapter 4.
Selection—Making Decisions
Week 4 – Repetition Structures / Loops
Lecture 2: Logical Problems with Choices
Structured Program
Relational and Logical Operators
3 Control Statements:.
CSC215 Lecture Control Flow.
Decision making If statement.
Relational, Logical, and Equality Operators
Week 3 – Program Control Structure
Control Statements Paritosh Srivastava.
Computer Programming Basics
Dale Roberts, Lecturer IUPUI
DATA TYPES There are four basic data types associated with variables:
CSC215 Lecture Control Flow.
Presentation transcript:

Selection-making Decisions Selection allows you to choose between two or more possible program flow --- it lets you make decisions in your program. Examples of selection : 2-way Selection:if…else statement Multi-way Selection:switch statement else-if construct C++ Programming

Logical Data and Operators data valuelogical meaning zero (0)FALSE non-zero (>0, <0)TRUE e.g. x=0 x is false x=-2 x is true x=3 x is true

 Logical Operators 

/*Logical Operators. */ #include int main ( void ) {int a = 4, int b = -1, int c = 0 ; printf( " %2d && %2d is %2d\n", a, b, a && b ) ; printf( " %2d && %2d is %2d\n", a, c, a && c ) ; printf( " %2d && %2d is %2d\n", c, a, c && a ) ; printf( " %2d || %2d is %2d\n", a, c, a || c ) ; printf( " %2d || %2d is %2d\n", c, a, c || a ) ; printf( " %2d || %2d is %2d\n", c, c, c || c ) ; printf( "!%2d && !%2d is %2d\n", a, c, !a && !c ) ; printf( "!%2d && %2d is %2d\n", a, c, !a && c ) ; printf( " %2d && !%2d is %2d\n", a, c, a && !c ) ; return 0 ; }

complement Relational Operators e.g. (x =y) (x>y) is same as !(x<=y)

/*Relational Operators. */ #include int main ( void ) {int a = 1 ; int b = -2 ; printf( " %2d < %2d is %2d\n", a, b, a < b ) ; printf( " %2d == %2d is %2d\n", a, b, a == b ) ; printf( " %2d != %2d is %2d\n", a, b, a != b ) ; printf( " %2d > %2d is %2d\n", a, b, a > b ) ; printf( " %2d <= %2d is %2d\n", a, b, a <= b ) ; printf( " %2d >= %2d is %2d\n", a, b, a >= b ) ; return 0 ; }

false (0)true (!=0) Two-Way Selection C uses the if…else statement to make 2-way selection. statement1statement2 expression Syntax: if ( expression ) statement1 else statement2

Examples of if…else : (1) if ( i==6 ) ++a; else --a; (2)if ( ++num > 12 ) { printf( “\n” ); num = 0; } printf( …… ); The semicolons (;) are required by ++a, --a statements, not by if…else !! Side-effect of this if (…) : num is incremented before testing for the limit 12. [ Purpose: we want to go to a new line after writing every 12th items. ]

(3)if ( j != 4 && k == 3 ) { ++j; --a; printf( “%d%d”, j,a ); } else { --j; ++a; printf( “%d%d”, j,a ); } Use braces {…} to combine multiple statements into one compound statement. Both if and else can be followed by : 1. null (nothing) 2. one-line statement 3. compound statement

/*if…else ( two-way selection )*/ #include int main ( void ) {int b ; printf( "Please enter two integers: " ) ; scanf ( "%d%d", &a, &b ) ; if ( a <= b ) printf( "%d <= %d\n", a, b ) ; else printf( "%d > %d\n", a, b ) ; return ; }

Nested if : we can have if…else inside an if…else statement. true statement1statement2 statement3 false expression2 truefalse expression1 Syntax : if ( expression1 ) if ( expression2 ) statement1 else statement2 else statement3

/*Nested if in two-way selection. */ #include int main ( void ) {int a, b ; printf( "Please enter two integers: " ) ; scanf ( "%d%d", &a, &b ) ; if ( a <= b ) if ( a < b ) printf( "%d < %d\n", a, b ) ; else printf( "%d == %d\n", a, b ) ; else printf( "%d > %d\n", a, b ) ; return ; }

 Programming Tips:   C always pair an else with the most recent unpaired if. When an if has no else to pair with, enclose it in braces{…} if ( expression1 ) if ( expression2 ) statement1 else statement2  C pairs this if and else!! if ( expression1 ) { if ( expression2 ) statement1 } else statement2 {…} avoids problem for if without else

The else - if construct When the case values are not integral (e.g. a float), we use the else - if construct, a special style of coding (program writing): true false score>=90 true false score>=80 true false score>=70 true false score>=60 grade=‘A’ grade=‘B’ grade=‘C’ grade=‘D’grade=‘F’ if ( score >= 90) grade = 'A' ; else if ( score >= 80 ) grade = 'B' ; else if ( score >= 70 ) grade = 'C' ; else if ( score >= 60 ) grade = 'D' ; else grade = 'F' ; same variable is being tested in expression

/* This program reads a test score, calculates the letter (A-F) grade for the score, and prints the grade.(using else-if ) */ #include int main ( void ) {char scoreToGrade ( int score ) ; int score ; char grade ; printf( "\nEnter the test score (0-100): " ) ; scanf( "%d", &score ) ; grade = scoreToGrade ( score ) ; printf( "The grade is: %c\n", grade ) ; return 0 ; }/* end of main body*/ Prototype Declarations Local Declarations

/*scoreToGrade -- function to calculate letter grade for a score. using else - if construct */ char scoreToGrade ( int score ) {char grade ; if ( score >= 90 ) grade = 'A' ; else if ( score >= 80 ) grade = 'B' ; else if ( score >= 70 ) grade = 'C' ; else if ( score >= 60 ) grade = 'D' ; else grade = 'F' ; return grade ; }/* end of scoreToGrade function*/ Local Declaration