CCSA 221 Programming in C CHAPTER 6 MAKING DECISIONS 1.

Slides:



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

1 Conditional Statement. 2 Conditional Statements Allow different sets of instructions to be executed depending on truth or falsity of a logical condition.
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
C++ Programming, Namiq Sultan1 Chapter 4 Making Decisions Namiq Sultan University of Duhok Department of Electrical and Computer Engineerin Reference:
Chapter 4 Making Decisions
C++ for Engineers and Scientists Third Edition
1 Lecture 5  More flow control structures  for  do  continue  break  switch  Structured programming  Common programming errors and tips  Readings:
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
EGR 2261 Unit 4 Control Structures I: Selection  Read Malik, Chapter 4.  Homework #4 and Lab #4 due next week.  Quiz next week.
Conditional Statement
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
1 Chapter 4: Selection Structures. In this chapter, you will learn about: – Selection criteria – The if-else statement – Nested if statements – The switch.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational operators – Discover.
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.
1 4.8The do/while Repetition Structure The do/while repetition structure –Similar to the while structure –Condition for repetition tested after the body.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
Program Looping Making Decisions Copyright © 2012 by Yong-Gu Lee
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.
1 Lecture 5: Selection Structures. Outline 2  Control Structures  Conditions  Relational Operators  Logical Operators  if statements  Two-Alternatives.
Decision II. CSCE 1062 Outline  Boolean expressions  switch statement (section 4.8)
Rational Expressions and selection structures Relational operators Logical operators Selection structures.
CS102 Introduction to Computer Programming Chapter 4 Making Decisions.
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
Chapter 3. Outline Relational Operators Loops Decisions Logical Operators Precedence Summary.
Lecture 3 – Selection. Outline Recall selection control structure Types of selection One-way selection Two-way selection Multi-selection Compound statement.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Selection Statements Selection Switch Conditional.
1 Compound Assignment C++ has a large set of operators for applying an operation to an object and then storing the result back into the object Examples.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X1 Chapter 3 Control Statements.
COS120 Software Development Using C++ AUBG Fall semester 2010 Ref book: Problem Solving, Abstraction and Design Using C++ Authors: Frank Friedman, Elliot.
Selection-making Decisions Selection allows you to choose between two or more possible program flow --- it lets you make decisions in your program. Examples.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
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.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 4: Making Decisions 1.
CMSC 104, Version 9/011 Relational and Logical Operators Topics Relational Operators and Expressions The if Statement The if-else Statement Nesting of.
Chapter 5 – Decision Making. Structured Programming Sequence Selection Repetition yesno yes no.
Control Statements: Part1  if, if…else, switch 1.
Making Decisions in c. 1.if statement Imagine that you could translate a statement such as “If it is not raining, then I will go swimming” into the C.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Control Structures (Selection & Repetition)
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Condition – any expression that evaluates to true/false value Relational operators are BINARY.
Program Control: Selection Subject: T0016 – ALGORITHM AND PROGRAMMING Year: 2013.
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
Lesson #4 Logical Operators and Selection Statements.
Lesson #4 Logical Operators and Selection Statements.
Branching statements.
Chapter 4 – C Program Control
The if…else Selection Statement
Chapter 3 Control Statements
Control Structures Combine individual statements into a single logical unit with one entry point and one exit point. Used to regulate the flow of execution.
Decisions Chapter 4.
Week 3 – Selection Structures
Chapter 3 Control Statements Lecturer: Mrs Rohani Hassan
Chapter 5 Decision Making and Branching
Relational and Logical Operators
Relational, Logical, and Equality Operators
Chapter 4: Control Structures I (Selection)
Week 3 – Program Control Structure
Control Structures Lecture 6.
Relational and Logical Operators
Relational and Logical Operators
Relational and Logical Operators
Presentation transcript:

CCSA 221 Programming in C CHAPTER 6 MAKING DECISIONS 1

Outline Making Decisions [chap 6 – Kochan] –The if Statement –The if-else Construct –Logical Operators –Boolean Variables –Nested if Statements –The else if Construct –The switch Statement –The Conditional Operator Character Input/Output

The if statement if ( expression ) program statement If expression is true (non-zero), executes statement. If gives you the choice of executing statement or skipping it. Program statement expression yes no

Example - if int main (void) { int deg; printf ("Type in your degree: "); scanf ("%i", &deg); if ( deg< 60 ) printf (“You are Failed “); return 0; }

The if-else statement if-else statement: enables you to choose between two statements Program statement 1 expression yes no Program statement 2 if ( expression ) program statement 1 else program statement 2

Example: if-else // Program to determine if a number is even or odd #include int main () { int number_to_test, remainder; printf ("Enter your number to be tested: "); scanf ("%i", &number_to_test); remainder = number_to_test % 2; if ( remainder == 0 ) printf ("The number is even.\n"); else printf ("The number is odd.\n"); return 0; }

Attention on if-else syntax ! if ( remainder == 0 ) printf ("The number is even.\n"); else printf ("The number is odd.\n"); if ( expression ) program statement 1 else program statement 2 In C, the ; is part (end) of a statement ! You have to put it also before an else ! if ( x == 0 ); printf ("The number is zero.\n"); Syntactically OK (void statement on if) but probably a semantic error !

Example: compound relational test // Program to determine if a year is a leap year #include int main (void) { int year, rem_4, rem_100, rem_400; printf ("Enter the year to be tested: "); scanf ("%i", &year); rem_4 = year % 4; rem_100 = year % 100; rem_400 = year % 400; if ( (rem_4 == 0 && rem_100 != 0) || rem_400 == 0 ) printf ("It's a leap year.\n"); else printf (“It's not a leap year.\n"); return 0; }

Logical operators OperatorSymbolMeaning AND && X && y is true if BOTH x and y are true OR || X || y is true if at least one of x and y is true NOT ! !x is true if x is false Logical values as operands or in tests: true = non-zero, false=zero Logical values returned as results of expressions: true = 1, false=zero Example: 5 || 0 is 1

Example Program to calculate the GRADING SCALE A =90%-100% B =80%-89% C =70%-79% D =60%-69% F =Below60%

Boolean variables // Program to generate a table of prime numbers #include int main (void) { int p, d; int isPrime; for ( p = 2; p <= 50; ++p ) { isPrime = 1; for ( d = 2; d < p; ++d ) if ( p % d == 0 ) isPrime = 0; if ( isPrime != 0 ) printf ("%i ", p); } printf ("\n"); return 0; } Equivalent test: (more in C-style): if (isPrime) A flag : assumes only one of two different values. The value of a flag is usually tested in the program to see if it is “on” (TRUE) or “off ” (FALSE)

Boolean variables // Program to generate a table of prime numbers - rewritten #include int main (void) { int p, d; bool isPrime; for ( p = 2; p <= 50; ++p ) { isPrime = true; for ( d = 2; d < p; ++d ) if ( p % d == 0 ) isPrime = false; if ( isPrime ) printf ("%i ", p); } printf ("\n"); return 0; }

Precedence of operators !, ++, --, (type) *, /, % +, -, >=, ==, != && || = Example for operator precedence: a > b && b > c || b > d Is equivalent to: ((a > b) && (b > c)) || (b > d) Precedence

Testing for ranges if (x >= 5 && x <= 10) printf(“in range"); if (5 <= x <= 10) printf(“in range");

Testing for ranges if (x >= 5 && x <= 10) printf(“in range"); if (5 <= x <= 10) printf(“in range"); Syntactically correct, but semantically an error !!! Because the order of evaluation for the <= operator is left-to-right, the test expression is interpreted as follows: (5<= x) <= 10 The subexpression 5 <= x either has the value 1 (for true) or 0 (for false). Either value is less than 10, so the whole expression is always true, regardless of x !

Testing for character ranges char ch; scanf(“%c”,&ch); if (ch >= 'a' && ch <= 'z') printf("lowercase char\n"); if (ch >= ‘A' && ch <= ‘Z') printf(“uppercase char\n"); if (ch >= ‘0' && ch <= ‘9') printf(“digit char\n"); This works for character codes such as ASCII, in which the codes for consecutive letters are consecutive numbers. However, this is not true for some codes (i.e. EBCDIC) A more portable way of doing this test is to use functions from islower(ch), isupper(ch), isdigit(ch)

Nested if statements if (number > 5) if (number < 10) printf(“1111\n"); else printf(“2222\n"); if (number > 5) { if (number < 10) printf(“1111\n"); } else printf(“2222\n"); Rule: an else goes with the most recent if, unless braces indicate otherwise

Example: else-if // Program to implement the sign function #include int main (void) { int number, sign; printf ("Please type in a number: "); scanf ("%i", &number); if ( number < 0 ) sign = -1; else if ( number == 0 ) sign = 0; else // Must be positive sign = 1; printf ("Sign = %i\n", sign); return 0; }

Multiple choices – else-if if else if else int number; negative zero positive if ( expression 1) program statement 1 else if ( expression 2) program statement 2 else program statement 3 Program style: this unindented formatting improves the readability of the statement and makes it clearer that a three-way decision is being made.

Example: else-if // Program to categorize a single character // that is entered at the terminal #include int main (void) { char c; printf ("Enter a single character:\n"); scanf ("%c", &c); if ( (c >= 'a' && c = 'A' && c <= 'Z') ) printf ("It's an alphabetic character.\n"); else if ( c >= '0' && c <= '9' ) printf ("It's a digit.\n"); else printf ("It's a special character.\n"); return 0; }

Example – multiple choices /* Program to evaluate simple expressions of the form number operator number */ #include int main (void) { float value1, value2; char operator; printf ("Type in your expression.\n"); scanf ("%f %c %f", &value1, &operator, &value2); if ( operator == '+' ) printf ("%.2f\n", value1 + value2); else if ( operator == '-' ) printf ("%.2f\n", value1 - value2); else if ( operator == '*' ) printf ("%.2f\n", value1 * value2); else if ( operator == '/' ) printf ("%.2f\n", value1 / value2); else printf ("Unknown operator.\n"); return 0; }

Example - switch /* Program to evaluate simple expressions of the form value operator value */ #include int main (void) { float value1, value2; char operator; printf ("Type in your expression.\n"); scanf ("%f %c %f", &value1, &operator, &value2); switch (operator) { case '+': printf ("%.2f\n", value1 + value2); break; case '-': printf ("%.2f\n", value1 - value2); break; case '*': printf ("%.2f\n", value1 * value2); break; case '/': if ( value2 == 0 ) printf ("Division by zero.\n"); else printf ("%.2f\n", value1 / value2); break; default: printf ("Unknown operator.\n"); break; } return 0; }

The switch statement switch ( expression ) { case value1: program statement... break; case value2: program statement... break;... case valuen: program statement... break; default: program statement... break; } The expression is successively compared against the values value1, value2,..., valuen. If a case is found whose value is equal to the value of expression, the program statements that follow the case are executed. The switch test expression must be one with an integer value (including type char) (No float !). The case values must be integer- type constants or integer constant expressions (You can't use a variable for a case label !)

The switch statement (cont) switch (operator) {... case '*': case 'x': printf ("%.2f\n", value1 * value2); break;... } Break can miss ! Statement list on a case can miss !

The conditional operator maxValue = ( a > b ) ? a : b; Equivalent to: if ( a > b ) maxValue = a; else maxValue = b; condition ? expression1 : expression2 condition is an expression that is evaluated first. If the result of the evaluation of condition is TRUE (nonzero), then expression1 is evaluated and the result of the evaluation becomes the result of the operation. If condition is FALSE (zero), then expression2 is evaluated and its result becomes the result of the operation