12-2 Know how if and switch C statements control the sequence of execution of statements. Be able to use relational and logical operators in the conditional.

Slides:



Advertisements
Similar presentations
Decisions If statements in C.
Advertisements

Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
BBS514 Structured Programming (Yapısal Programlama)1 Functions and Structured Programming.
BBS514 Structured Programming (Yapısal Programlama)1 Selective Structures.
11-2 Identify the parts of the “main” function, which include Preprocessor Directives main function header main function body which includes Declaration.
Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman Chapter 4 (Conditional Statements) © CPCS
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 4 – C Program Control Outline 4.1Introduction.
1 9/26/08CS150 Introduction to Computer Science 1 Logical Operators and if/else statement.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 4- 1.
1 Chapter 3 Flow of Control. 2 Outline  How to specify conditions?  Relational, Equality and Logical Operators  Statements  Statements: compound statement.
If Statements. COMP104 If / Slide 2 Three Program Structures * Sequence - executable statements which the computer processes in the given order * Choice.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Chapter 4 Making Decisions
C++ for Engineers and Scientists Third Edition
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.
CHAPTER#3 STRUCTURED PROGRAM DEVELOPMENT IN C++ 1 st semester King Saud University College of Applied studies and Community Service Csc 1101.
Algorithms and Computing Lecture 3 Control Statements By Dr. M. Tahir Khaleeq.
16&17-2 Grasp the concept of top-down programming Identify Function Headers and Prototypes Understand when and where prototypes used Understand how arguments.
13&14-2 Know the forms of loop statements in C (while,do/while,for). Understanding how data conversion occurs. Read/Write data in files using Unix redirection.
18-2 Understand “Scope” of an Identifier Know the Storage Classes of variables and functions Related Chapter: ABC 5.10, 5.11.
Programming C for Engineers An exercise is posted on the web site! Due in one week Single submission.
CMSC 104, Version 8/061L11Relational&LogicalOps.ppt Relational and Logical Operators Topics Relational Operators and Expressions The if Statement The if-else.
1 If statement and relational operators –, >=, ==, != Finding min/max of 2 numbers Finding the min of 3 numbers Forming Complex relational expressions.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
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.
Pseudocode When designing an ALGORITHM to solve a problem, Pseudocode, can be used. –Artificial, informal language used to develop algorithms –Similar.
Chapter 6: Control Structures Computer Programming Skills Second Term Department of Computer Science Foundation Year Program Umm Alqura.
Making Decisions. 4.1 Relational Operators Used to compare numbers to determine relative order Operators: > Greater than < Less than >= Greater than.
PROBLEM SOLVING & ALGORITHMS CHAPTER 5: CONTROL STRUCTURES - SELECTION.
A First Book of ANSI C Fourth Edition Chapter 4 Selection.
Branches and Program Design
Chapter 5: Structured Programming
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4: Making Decisions.
Programming C for Engineers An exercise is posted on the web site! Due in one week Single submission.
Chapter 5: Structured Programming
CHAPTER#3 STRUCTURED PROGRAM DEVELOPMENT IN C++ 1 st semester King Saud University College of Applied studies and Community Service Csc 1101.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
19&20-2 Know how to declare pointer variables. Understand the & (address) and *(indirection) operators. Dynamic Memory Allocation Related Chapter: ABC.
COS120 Software Development Using C++ AUBG Fall semester 2010 Ref book: Problem Solving, Abstraction and Design Using C++ Authors: Frank Friedman, Elliot.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 4: Making Decisions.
Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.
Chapter Making Decisions 4. Relational Operators 4.1.
If Statements Programming. COMP104 Lecture 7 / Slide 2 Review: Rules for Division l C++ treats integers different than doubles. 100 is an int. l 100.0,
1 Agenda If Statement True/False Logical Operators Nested If / Switch Exercises & Misc.
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Conditions - compare the values of variables, constants and literals using one or more relational.
1 Lecture03: Control Flow 9/24/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
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.
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Condition – any expression that evaluates to true/false value Relational operators are BINARY.
1 Chapter 4 - Control Statements: Part 1 Outline 4.1 Introduction 4.4 Control Structures 4.5 if Selection Structure 4.6 if/else Selection Structure 4.7.
Decision making If.. else statement.
Chapter 4 – C Program Control
Decisions Chapter 4.
Chapter 4: Making Decisions.
Chapter 4 (Conditional Statements)
Lecture 13 & 14.
Chapter 4: Making Decisions.
Control Statement Examples
Lec 5.
CS1100 Computational Engineering
Chapter 5 Decision Making and Branching
Structured Program
Decision making If statement.
Chapter 6: User-Defined Functions I
Week 3 – Program Control Structure
Relational and Logical Operators
Relational and Logical Operators
Lecture 4: Selection Control Structure
Functions in C Math Library Functions Functions Function Definitions
Presentation transcript:

12-2 Know how if and switch C statements control the sequence of execution of statements. Be able to use relational and logical operators in the conditional part of an if or a switch statement. Related Chapters: ABC Chapter & 4.16

Problem Definition Write a program that reads a number and computes the square root if the number is non-negative. 2. Refine, Generalize, Decompose the problem definition (i.e., identify subproblems, I/O, etc.) Input = real number Output=real number 3. Develop Algorithm (processing steps to solve problem)

12-4 Flowchart Print “enter value” Read value value >= 0.0 Print sqrt(value) True False

12-5 /* C Program to compute the square root of a positive number */ #include #include void main(void) { double value; /* Declare variables. */ /* request user input */ printf("Please enter a non-negative number :"); /* read value */ scanf("%lf", &value); /* Output the square root. */ if (value >= 0.0) printf("square_root(%lf) = %lf \n", value, sqrt(value)); }

12-6 if(expression) statement; if expression evaluates to true, the statement is executed; otherwise execution passes to the next statement in the program. if Selection Structure if(value >= 0.0); printf("square_root(%lf) = %lf \n", value,sqrt(value)) ; /* Error! Don’t put semicolon here */ /* This is an example of a logical error */

Problem Definition Modify the previous program to notify the user when the input is invalid.

12-8 Flowchart Print “enter value” Read value value >= 0.0 Print sqrt(value); True False Print “invalid input”

12-9 /* C Program to compute the square root of a positive number */ #include #include void main(void) { double value; /* Declare variables. */ /*request user input*/ printf(”Please enter a non-negative number :”); scanf("%lf", &value); /* read value */ /* Output the square root. */ if (value >= 0.0) printf("square_root(%lf) = %lf \n", value,sqrt(value)); else printf("invalid user input, please enter non-negative value\n"); }

in header file math.h Arguments (parameters) for each of the following functions are assumed to be of type double. If not, a type double copy is made for use in the function. To compile a program that contains math functions you need to use the -lm (Lm not 1m )option for gcc. > gcc file.c -lm See next page

fabs (x) - |x| (not the same as the abs(x) function) sqrt (x) - square root of x pow (x, a) - x a exp (x) - e x (e = …) log (x) - ln x = log e x log10 (x) - log 10 x sin (x) - sine function (x in radians) cos (x) - cosine function (x in radians) tan (x) - tangent function (x in radians) ceil (x) - smallest integer >= x floor (x) - largest integer <= x

12-12 if (expression) statement1; else statement2; -if expression evaluates to true, statement1 is executed and execution skips statement2 -If expression evaluates to false, execution skips statement1, statement2 is executed

12-13 We can also execute multiple statements when a given expression is true: if (expression) { statement1; statement2; statementn; } Example - if(b < a) { temp = a; a = b; b = temp; } or if (expression) { statement1; statementn; } else { statement1; statementm; } (what does this code do?)

Problem Definition Modify the previous program to compute the following: You must check that the value is legal, i.e. value >= 1.0 or value <= -1.0

12-15 Flowchart Print “enter value” Read value value >= 1.0 or value <= -1.0 Print sqrt(value*value -1.0) ; True False Print “invalid input”

12-16 /* Compute the square root of value*value-1.0 */ #include #include void main(void) { double value; /* Declare variables. */ /* request user input*/ printf("Please enter value >= 1.0 or <= -1.0 :"); scanf("%lf", &value); /* read value */ /* Output the square root. */ if ((value >= 1.0) || (value = 1.0 or <= -1.0 \n"); } }

12-17 In logical expressions (which evaluate to true or false), we can use the following Relational operators: Relational Operator Type of Test == equal to (don’t use =) != not equal to > greater than >= greater than or equal to < less than <= less than or equal to

12-18 ABA && BA || B TRUE FALSE TRUE FALSETRUEFALSETRUE FALSE A!A TRUEFALSE TRUE

12-19 if ( 5 ) printf("True"); /* prints True */ In C the value for False is represented by the value zero and True is represented by any nonzero value. The value False can be any zero value such as the number 0 or 0.0 or null character ‘ \0 ’ or the NULL pointer. Example 2 : int x = 0; /* x declared as an integer variable */ /* and initialized to the value 0 */ if (x = 0) /* note the error, == should be used */ printf(" x is zero\n"); /*message not printed, why?*/ Example 1 :

12-20 Avoid using == to test real numbers for equality! Example double x = ; /* ten digits of 3s */ if (x == 1.0/3.0) printf("equal!\n"); else printf(" not equal!\n"); /* prints not equal! */ if ( fabs(x - 1.0/3.0) < 1.0e-10 ) printf("equal!\n"); /* prints equal! */ else printf(" not equal!\n");

Problem Definition Write a program that returns a letter grade based on a quiz score. The input will be the integer score from a 10 point quiz. The letter grades are assigned by: “A” “B” “C” “D” < 3 “F” 2. Refine, Generalize, Decompose the problem definition (i.e., identify subproblems, I/O, etc.) Input = integer score Output=character “grade” 3. Develop Algorithm (processing steps to solve problem)

12-22 Flowchart Print “enter score” Read score score == 10 || score == 9 Print “A” TrueFalse (continued on next slide) (skip else part of statement)

12-23 False score == 8 || score == 7 Print “B” ; True (skip else part of statement) (continued on next slide) False

12-24 False score == 6 || score == 5 Print “C” ; True (skip else part of statement) (continued on next slide) False

12-25 False score == 4 || score == 3 Print “D” True False Print “F”

12-26 /* C Program to compute the letter grade for a quiz. */ #include void main(void) { int score; /* Declare variables. */ /* request user input */ printf("Please enter a score :"); scanf("%i", &score); /* read value */ /* Output the grade */ /* continued on the next slide */

12-27 if ((score == 10) || (score == 9)) printf("A\n"); else if ((score == 8) || (score == 7)) printf("B\n"); else if ((score == 6) || (score == 5)) printf("C\n"); else if ((score == 4) || (score == 3)) printf("D\n"); else printf("F\n"); } /* end of program */ Unless { } are used the else matches the first if in the code above.

Problem Definition Redo the previous problem by using a switch statement rather than the nested if-else statement. Pseudo-code Algorithm print “enter score” read score switch score score = 9,10 print “A” score = 7,8 print “B” score = 5,6 print “C” score = 3,4 print “D” otherwise print “F”

12-29 /* C Program to compute the letter grade for a quiz. */ #include void main(void) { int score; /* Declare variables. */ /* request user input */ printf("Please enter a score :"); scanf("%i", &score); /* read value */ /* Output the grade */ /* continued on the next slide */

12-30 switch (score) { case 10: case 9: printf("A\n"); break; case 8: case 7: printf("B\n"); break; case 6: case 5: printf("C\n"); break; case 4: case 3: printf("D\n"); break; default: printf("F\n"); } /* end of switch */ } /* end of program */

12-31 The switch structure can be used when we have multiple alternatives based on a value of type int or type char (but not float or double). This structure has the general form: switch (controlling expression) { case label1: label1_statement(s); case label2: label2_statement(s); default: default_statement(s); } (controlling expr. must evaluate to an integer or a character value; each case should end with a break stmt.)