Decision making If statement.

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.
1 Conditional Statement. 2 Conditional Statements Allow different sets of instructions to be executed depending on truth or falsity of a logical condition.
1 CSC103: Introduction to Computer and Programming Lecture No 8.
Chapter 3 - Structured Program Development
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 3 - Structured Program Development Outline.
Introduction to Computer Programming Decisions If/Else Booleans.
Introduction to Computers and Programming Class 6 Introduction to C Professor Avi Rosenfeld.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control.
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;
Chapter 4 MATLAB Programming Logical Structures Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Structured Program Development in C
The switch Statement.  Occasionally, an algorithm will contain a series of decisions in which a variable or expression is tested separately for each.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Structural Program Development: If, If-Else Outline.
1 CSC103: Introduction to Computer and Programming Lecture No 7.
Lecture 2: Logical Problems with Choices. Problem Solving Before writing a program Have a thorough understanding of the problem Carefully plan an approach.
Pseudocode When designing an ALGORITHM to solve a problem, Pseudocode, can be used. –Artificial, informal language used to develop algorithms –Similar.
Nested LOOPS.
PROBLEM SOLVING & ALGORITHMS CHAPTER 5: CONTROL STRUCTURES - SELECTION.
Lecture 3 – Selection. Outline Recall selection control structure Types of selection One-way selection Two-way selection Multi-selection Compound statement.
The switch Statement.  Occasionally, an algorithm will contain a series of decisions in which a variable or expression is tested separately for each.
Lecture 2 Control Structure. Relational Operators -- From the previous lecture Relational Operator Meaning == is equal to < is less than > is greater.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X1 Chapter 3 Control Statements.
CHAPTER#3 PART1 STRUCTURED PROGRAM DEVELOPMENT IN C++ 2 nd semester King Saud University College of Applied studies and Community Service Csc.
CHAPTER#3 PART1 STRUCTURED PROGRAM DEVELOPMENT IN C++ 2 nd semester King Saud University College of Applied studies and Community Service Csc.
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#3 Part1 Structured Program Development in C++
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.
 2003 Prentice Hall, Inc. All rights reserved. 1 Will not cover 4.14, Thinking About Objects: Identifying Class Attributes Chapter 4 - Control Structures.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
 2003 Prentice Hall, Inc. All rights reserved. 1 Basic C++ Programming.
Control Statements: Part1  if, if…else, switch 1.
CHAPTER#3 STRUCTURED PROGRAM DEVELOPMENT IN C 1 A.AlOsaimi King Saud University College of Applied studies and Community Service Csc 1101.
Chapter 3 Structured Program Development in C C How to Program, 8/e, GE © 2016 Pearson Education, Ltd. All rights reserved.1.
Repetition statements
Decision making If.. else statement.
The if…else Selection Statement
Algorithm: procedure in terms of
- Standard C Statements
Control Statements: Part 1
EKT150 INTRODUCTION TO COMPUTER PROGRAMMING
Chapter 2.1 Control Structures (Selection)
CSC113: Computer Programming (Theory = 03, Lab = 01)
Chapter 4: Control Structures
REPETITION STATEMENTS
Programming Fundamentals
SELECTION STATEMENTS (1)
Control Statement Examples
Lecture 2: Logical Problems with Choices
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
SELECTION STATEMENTS (2)
Structured Program
1) C program development 2) Selection structure
Chapter 3 - Structured Program Development
3 Control Statements:.
Chapter 3 - Structured Program Development
Selection Control Structure
REPETITION STATEMENTS
Week 3 – Program Control Structure
EPSII 59:006 Spring 2004.
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
Dale Roberts, Lecturer IUPUI
HNDIT11034 More Operators.
Selection Control Structure
Lecture 9: Implementing Complex Logic
Control Structures.
Structural Program Development: If, If-Else
Presentation transcript:

Decision making If statement

1. Decision making – fig. 1 END Block of Statements (2) Condition? Block of Statements (2) Block of Statements (1) True False Block of Statements (3) Dr. Soha S. Zaghloul 2

2.  The if…else Statement The if…else selection statement allows you to specify that different actions are to be performed when the condition is true and when it is false. Syntax: if (condition) { block of statements 1; }// end if else block of statements 2; }//end else block of statements 3; Dr. Soha S. Zaghloul 3

Dr. Soha S. Zaghloul 4 ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

3.  if…else - EXAMPLE Write a program that prints “Passed” or “Failed” depending on the student’s grade. The program should finally print a “Good bye” message. Get the grade from the user Check if grade is greater than or equal to 60 Condition true: print “passed” Condition false: print “failed” Print a “Good bye” message ALGORITHM Dr. Soha S. Zaghloul 5

3.if…else – EXAMPLE Flowchart START READ grade grade >= 60? Print “passed” True False Print “failed” Print “Good bye” END FLOWCHART Dr. Soha S. Zaghloul 6

3.if…else – EXAMPLE pseudocode Scanf grade If (grade >= 60) Condition true: Printf “passed” Condition false: Printf “failed” Printf “Good bye” PSEUDOCODE Dr. Soha S. Zaghloul 7

3.if…else – EXAMPLE code scanf (“%d”, &grade); //read grade from user Scanf grade If (grade >= 60) Condition true: Printf “passed” Condition false: Printf “failed” Printf “Good bye” PSEUDOCODE Dr. Soha S. Zaghloul 8

3.if…else – EXAMPLE code printf (“Enter grade: \n”); // prompt scanf (“%d”, &grade); // read grade from user Scanf grade If (grade >= 60) Condition true: Printf “passed” Condition false: Printf “failed” Printf “Good bye” PSEUDOCODE Dr. Soha S. Zaghloul 9

3.if…else – EXAMPLE code int grade; //declaration of grade printf (“Enter grade: \n”); //prompt scanf (“%d”, &grade); //read grade from user Scanf grade If (grade >= 60) Condition true: Printf “passed” Condition false: Printf “failed” Printf “Good bye” PSEUDOCODE Dr. Soha S. Zaghloul 10

3.if…else – EXAMPLE code PSEUDOCODE Scanf grade If (grade >= 60) Condition true: Printf “passed” Condition false: Printf “failed” Printf “Good bye” PSEUDOCODE int grade; //declaration of grade printf (“Enter grade: \n”); //prompt scanf (“%d”, &grade); //read grade from user if (grade >=60) { printf (“passed \n”); // if condition is true } // end if else printf (“failed \n”); // if condition is false } //end else Dr. Soha S. Zaghloul 11

3.if…else – EXAMPLE code PSEUDOCODE Scanf grade If (grade >= 60) Condition true: Printf “passed” Condition false: Printf “failed” Printf “Good bye” PSEUDOCODE int grade; //declaration of grade printf (“Enter grade: \n”); //prompt scanf (“%d”, &grade); //read grade from user if (grade >=60) { printf (“passed \n”); // if condition is true } // end if else printf (“failed \n”); // if condition is false } //end else printf (“Good bye \n”); Dr. Soha S. Zaghloul 12

3.if…else – EXAMPLE code Scanf grade If (grade >= 60) Condition true: Printf “passed” Condition false: Printf “failed” Printf “Good bye” PSEUDOCODE #include <stdio.h> int main (void) { int grade; //declaration of grade printf (“Enter grade: \n”); //prompt scanf (“%d”, &grade); //read grade from user if (grade >=60) printf (“passed \n”); // if condition is true } // end if else printf (“failed \n”); // if condition is false } //end else printf (“Good bye \n”); } // end of main Dr. Soha S. Zaghloul 13

4. The logical operators - AND The AND Operator is written as && in C It is a binary operator Its truth table is as follows: The result of an AND operation is TRUE only when both operands are TRUE; otherwise it is FALSE. Operand 1 Operand 2 Result True False Dr. Soha S. Zaghloul 14

5. The logical operators - OR The OR Operator is written as || in C It is a binary operator Its truth table is as follows: The result of an OR operation is FALSE only when both operands are FALSE; otherwise it is TRUE. Operand 1 Operand 2 Result True False Dr. Soha S. Zaghloul 15

6. The logical operators - NOT The NOT Operator is written as ! in C It is a unary operator Its truth table is as follows: Operand Result True False Dr. Soha S. Zaghloul 16

7. The logical operators - example Write a program that outputs the letter grade of a student given his score. Get the score from the user Check if score is greater than or equal to 90 Condition true: the letter grade is ‘A’ Check if score is greater than or equal to 80 and less than 90 Condition true: the letter grade is ‘B’ Check if score is greater than or equal to 70 and less than 80 Condition true: the letter grade is ‘C’ Check if score is greater than or equal to 60 and less than 70 Condition true: the letter grade is ‘D’ Check if score is less than 60 Condition true: the letter grade is ‘F’ ALGORITHM Dr. Soha S. Zaghloul 17

7. The logical operators - example START READ SCORE SCORE >= 90? GRADE=‘A’ FALSE SCORE >= 80 AND <90? TRUE GRADE=‘B’ PRINT GRADE FALSE SCORE >= 70 AND <80? END TRUE GRADE=‘C’ FALSE SCORE >= 60 AND <70? GRADE=‘F’ FALSE TRUE GRADE=‘D’ FLOWCHART Dr. Soha S. Zaghloul 18

7. The logical operators - example Scanf score If (score >= 90) then grade = ‘A’ If (score >= 80) and (score < 90) then grade = ‘B’ If (score >= 70) and (score < 80) then grade = ‘C’ If (score >= 60) and (score < 70) then grade = ‘D’ If (score < 60) then grade = ‘F’ Printf grade PSEUDOCODE Dr. Soha S. Zaghloul 19

7. The logical operators - example #include <stdio.h> // This program calculates the letter grade given the score int main (void) { int score; // student’s score char grade; // stduent’s letter grade printf (“Enter student’s score \n”); //prompt scanf (“%d”, &score); //get score from user } //end of main Dr. Soha S. Zaghloul 20

7. The logical operators - example #include <stdio.h> // This program calculates the letter grade given the score int main (void) { int score; // student’s score char grade; // stduent’s letter grade printf (“Enter student’s score \n”); //prompt scanf (“%d”, &score); //get score from user if (score >= 90) grade = ‘A’; } //end (score >=90) } //end of main Dr. Soha S. Zaghloul 21

7. The logical operators - example #include <stdio.h> // This program calculates the letter grade given the score int main (void) { int score; // student’s score char grade; // stduent’s letter grade printf (“Enter student’s score \n”); //prompt scanf (“%d”, &score); //get score from user if (score >= 90) grade = ‘A’; } //end (score >=90) if ((score >=80) && (score < 90)) grade = ‘B’; } // end ((score >=80) && (score < 90)) } //end of main Dr. Soha S. Zaghloul 22

7. The logical operators - example if ((score >= 80) && (score < 90)) { grade = ‘B’; } // end ((score >=80) && (score < 90)) if ((score >= 70) && (score < 80)) grade = ‘C’; } // end ((score >= 70) && (score < 80)) if ((score >= 60) && (score < 70)) grade = ‘D’; } // end ((score >= 60) && (score < 70)) if (score < 60) grade = ‘F’; } // end (score < 60) Dr. Soha S. Zaghloul 23

7. The logical operators - example printf (“The grade corresponding to %d is %c”, score, grade); } //end of main Dr. Soha S. Zaghloul 24

8. an alternative solution 1 if ((score >= 80) && (score < 90)) { grade = ‘B’; } // end ((score >=80) && (score < 90)) if ((score >= 70) && (score < 80)) grade = ‘C’; } // end ((score >= 70) && (score < 80)) if ((score >= 60) && (score < 70)) grade = ‘D’; } // end ((score >= 60) && (score < 70)) if (score < 60) else grade = ‘F’; } // end of else Dr. Soha S. Zaghloul 25

9. an alternative solution 2 #include <stdio.h> int main (void) { int score; char grade; printf (“Enter student’s score \n”); scanf (“%d”, &score); if (score >= 90) { grade = ‘A’; } else { if ((score >= 80) && (score < 90)) { grade = ‘B’;} } { if ((score >= 70) && (score < 80)) { grade = ‘C’;} } { if ((score >= 60) && (score < 70)) { grade = ‘D’;} } { grade = ‘C’ }; printf (“The grade corresponding to %d is %c”, score, grade); } //end of main One Statement Dr. Soha S. Zaghloul 26

10. NESTED IF NESTED IF #include <stdio.h> int main (void) { int score; char grade; printf (“Enter student’s score \n”); scanf (“%d”, &score); if (score >= 90) { grade = ‘A’; } else { if ((score >= 80) && (score < 90)) { grade = ‘B’;} } { if ((score >= 70) && (score < 80)) { grade = ‘C’;} } { if ((score >= 60) && (score < 70)) { grade = ‘D’;} } { grade = ‘C’ }; printf (“The grade corresponding to %d is %c”, score, grade); } //end of main NESTED IF Dr. Soha S. Zaghloul 27

11.  nested if - syntax Nested if statements are used for multiple-alternative decision. Syntax: if (condition 1) Statement 1 else if (condition 2) Statement 2 … else if (condition n) statement n Dr. Soha S. Zaghloul 28

12. NESTED IF - EXAMPLE Consider the following code segment num_pos counts the number of positive numbers. num_neg counts the number of negative numbers. num_zero counts the number of zeroes. if (x > 0) num_pos = num_pos +1; else if (x < 0) num_neg = num_neg +1; else /* x equals zero */ num_zero = num_zero +1; Dr. Soha S. Zaghloul 29

13. The ternary “?” conditional operator C provides the conditional operator (?:) which is closely related to the if…else statement. The conditional operator is C’s only ternary operator—it takes three operands. These together with the conditional operator form a conditional expression. The first operand is a condition. The second operand is the value for the entire conditional expression if the condition is true The third operand is the value for the entire conditional expression if the condition is false. Dr. Soha S. Zaghloul 30

13. The “?” operator - example #include <stdio.h> int main (void) { int grade; //declaration of grade printf (“Enter grade: \n”); //prompt scanf (“%d”, &grade); //read grade from user if (grade >=60) printf (“P \n”); // if condition is true } // end if else printf (“F \n”); // if condition is false } //end else printf (“Good bye \n”); } // end of main Char Res; Res =((grade >=60)? “P”:”F”); printf (“Result = %c \n”, Res); Dr. Soha S. Zaghloul 31