1) C program development 2) Selection structure

Slides:



Advertisements
Similar presentations
More on Algorithms and Problem Solving
Advertisements

3 Decision Making: Equality and Relational Operators A condition is an expression that can be either true or false. Conditions can be formed using the.
PSEUDOCODE & FLOW CHART
C How to Program, 6/e Summary © by Pearson Education, Inc. All Rights Reserved.
 Control structures  Algorithm & flowchart  If statements  While statements.
Lecture 2 Introduction to C Programming
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.
Introduction to C Programming
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.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
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.
Introduction to C Programming
Structured Program Development in C
Spring 2005, Gülcihan Özdemir Dağ Lecture 3, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 3 Outline 3.1 Introduction.
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Structural Program Development: If, If-Else Outline.
Structured Program Development Outline 2.1Introduction 2.2Algorithms 2.3Pseudo code 2.4Control Structures 2.5The If Selection Structure 2.6The If/Else.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
C++ Programming Language Lecture 2 Problem Analysis and Solution Representation By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Lecture 2: Logical Problems with Choices. Problem Solving Before writing a program Have a thorough understanding of the problem Carefully plan an approach.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
C Lecture Notes 1 Structured Program Development.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
The switch Statement.  Occasionally, an algorithm will contain a series of decisions in which a variable or expression is tested separately for each.
Dale Roberts 1 Program Control - Algorithms Department of Computer and Information Science, School of Science, IUPUI CSCI N305.
Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection Structure 3.6The.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
The Hashemite University Computer Engineering Department
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
 2003 Prentice Hall, Inc. All rights reserved. 1 Basic C++ Programming.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
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.
Algorithm: procedure in terms of
ARITHMETIC IN C Operators.
CSC201: Computer Programming
Chapter 4 C Program Control Part I
- Standard C Statements
Chapter 2 - Introduction to C Programming
Lecture 7: Repeating a Known Number of Times
Chapter 2.1 Control Structures (Selection)
REPETITION STATEMENTS
Lecturer CS & IT Department UOS MBDIN
Chapter 4 – Control Structures Part 1
Programming Fundamentals
Chapter 2 - Introduction to C Programming
Chapter 4 Control Statements: Part I
Lecture 2: Logical Problems with Choices
Chapter 2 - Introduction to C Programming
Chapter 2 Introduction to C Programming
Chapter 2 - Introduction to C Programming
INPUT & OUTPUT scanf & printf.
Chapter 2 - Introduction to C Programming
Structured Program
Chapter 3 - Structured Program Development
Decision making If statement.
Chapter 3 - Structured Program Development
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Dale Roberts, Lecturer IUPUI
Introduction to C Programming
The while Looping Structure
Chapter 2, Part III Arithmetic Operators and Decision Making
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
Structural Program Development: If, If-Else
Presentation transcript:

1) C program development 2) Selection structure The if statement

1.  ALGORITHM Before writing a program to solve a particular problem, we must have a thorough understanding of the problem and a carefully planned solution approach. The solution to any computing problem involves executing a series of actions in a specific order. A procedure for solving a problem in terms of the actions to be executed, and the order in which these actions are to be executed is called an algorithm. Correctly specifying the order in which the actions are to be executed is important. Specifying the order in which statements are to be executed in a computer program is called program control. Dr. Soha S. Zaghloul 2 Copyright © Pearson, Inc. 2013. All Rights Reserved. ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

2.  flowchart After writing the algorithm and the pseudocode, we draw a flowchart of the program. In a flowchart, every action has its own symbol: Start or End Read or Print Assignment or calculation statement START END READ X PRINT X Y = 7 A = B + C Dr. Soha S. Zaghloul 3 ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

3.  Pseudocode Pseudocode is an artificial and informal language that helps you develop algorithms. Pseudocode is similar to everyday English; it’s convenient and user friendly although it is not an actual computer programming language. Pseudocode programs are not executed on computers. Rather, they merely help you “think out” a program before attempting to write it in a programming language like C. Pseudocode consists only of action statements—those that are executed when the program is been converted from pseudocode to C. Then, the C program is written. Definitions are not executable statements. They’re simply messages to the compiler. Dr. Soha S. Zaghloul 4 Copyright © Pearson, Inc. 2013. All Rights Reserved. ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

4. eXAMPLE: Adding Two Integers ALGORITHM Get the values of two numbers from the user Add the two numbers Store the result Print the result ALGORITHM Dr. Soha S. Zaghloul 5 ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

5. eXAMPLE: Adding Two Integers FLOWCHART Get the values of two numbers from the user Add the two numbers Store the result Print the result ALGORITHM START Read number1 Read number2 sum = number1 + number2 Print sum END FLOWCHART Dr. Soha S. Zaghloul 6 ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

6. eXAMPLE: Adding Two Integers PSEUDOCODE Get the values of two numbers from the user Add the two numbers Store the result Print the result ALGORITHM START Read number1 Read number2 sum = number1 + number2 Print sum END FLOWCHART Scanf integer1 Scanf integer2 sum = integer1 + integer2 Printf sum PSEUDOCODE Dr. Soha S. Zaghloul 7 ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

7. eXAMPLE: Adding Two Integers CODE scanf (“%d”, &integer1); //read integer1 Scanf integer1 Scanf integer2 sum = integer1 + integer2 Printf sum PSEUDOCODE Dr. Soha S. Zaghloul 8 ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

7. eXAMPLE: Adding Two Integers CODE printf (“Enter first integer: \n”); //prompt scanf (“%d”, &integer1); //read integer1 Scanf integer1 Scanf integer2 sum = integer1 + integer2 Printf sum PSEUDOCODE Dr. Soha S. Zaghloul 9 ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

7. eXAMPLE: Adding Two Integers CODE int integer1; //declaration of first number printf (“Enter first integer: \n”); //prompt scanf (“%d”, &integer1); //read integer1 Scanf integer1 Scanf integer2 sum = integer1 + integer2 Printf sum PSEUDOCODE Dr. Soha S. Zaghloul 10 ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

7. eXAMPLE: Adding Two Integers CODE int integer1; //declaration of first number int integer2; //declaration of second number printf (“Enter first integer: \n”); //prompt scanf (“%d”, &integer1); //read integer1 printf (“Enter second integer: \n”); //prompt scanf (“%d”, &integer2); //read integer2 Scanf integer1 Scanf integer2 sum = integer1 + integer2 Printf sum PSEUDOCODE Dr. Soha S. Zaghloul 11 ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

7. eXAMPLE: Adding Two Integers CODE int integer1; //declaration of first number int integer2; //declaration of second number int sum; //total of the two numbers printf (“Enter first integer: \n”); //prompt scanf (“%d”, &integer1); //read integer1 printf (“Enter second integer: \n”); //prompt scanf (“%d”, &integer2); //read integer2 sum = integer1 + integer2; //add the 2 numbers Scanf integer1 Scanf integer2 sum = integer1 + integer2 Printf sum PSEUDOCODE Dr. Soha S. Zaghloul 12 ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

7. eXAMPLE: Adding Two Integers CODE #include <stdio.h> // function main begins program execution int main (void) { int integer1; //declaration of first number int integer2; //declaration of second number int sum; //total of the two numbers printf (“Enter first integer: \n”); //prompt scanf (“%d”, &integer1); //read integer1 printf (“Enter second integer: \n”); //prompt scanf (“%d”, &integer2); //read integer2 sum = integer1 + integer2; //add the 2 numbers printf (“Total is %d \n”, sum); //print total } // end of main function Scanf integer1 Scanf integer2 sum = integer1 + integer2 Printf sum PSEUDOCODE Dr. Soha S. Zaghloul 13 ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

7. eXAMPLE: Adding Two Integers CODE Scanf integer1 Scanf integer2 sum = integer1 + integer2 Printf sum PSEUDOCODE Dr. Soha S. Zaghloul 14 Copyright © Pearson, Inc. 2013. All Rights Reserved. ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

8. Control structures All the examples we studied till now execute in a “sequential” way. Control structures control the flow of execution in a program. In other words, the flow of execution is not necessarily sequential. The control structure enables you to combine individual instructions into a single logical unit with one entry point and one exit point. Two control structure are essential in C: The decision making control The repetition control Dr. Soha S. Zaghloul 15 Copyright © Pearson, Inc. 2013. All Rights Reserved.

9. Decision Making – the if statement Executable C statements either perform actions (such as calculations and input or output of data) or make decisions. We might make a decision in a program, for example, to determine whether a person’s grade on an exam is greater than or equal to 60 and whether the program should print the message “Congratulations! You passed.” This section introduces a simple version of C’s if statement that allows a program to make a decision based on the truth or falsity of a statement of fact called a condition. Dr. Soha S. Zaghloul 16 Copyright © Pearson, Inc. 2013. All Rights Reserved. ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

10. The if statement Decision making tests a condition and specifies its flow of execution accordingly. It enables you to combine individual instructions into a single logical unit with one entry point and one exit point (see Figure 1). Dr. Soha S. Zaghloul 17 Copyright © Pearson, Inc. 2013. All Rights Reserved.

11. 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 18

12. The if statement If the condition is true (i.e., the condition is met) the “block of statements 1” in the body of the if statement is executed. If the condition is false (i.e., the condition isn’t met) the “block of statements 2” is not executed. Whether the body statement is executed or not, after the if statement completes, execution proceeds with the next statement after the if statement, which are “block of statements 3”. Conditions in if statements are formed by using the equality operators (== !=) and relational operators (< <= > >=). Dr. Soha S. Zaghloul 19 Copyright © Pearson, Inc. 2013. All Rights Reserved. ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

12. The if statement - sYNTAX If (condition) { block of statements 1; } Dr. Soha S. Zaghloul 20 ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

13. The if statement - example Write a program that prints the message “Congratulations! You passed.” in case a person’s grade on an exam is greater than or equal to 60. Get the person’s grade from the user Check if the grade is greater than or equal to 60 If the grade is greater than or equal to 60 print the “congratulations” message If the grade is less than 60 do nothing End the program ALGORITHM Dr. Soha S. Zaghloul 21

14. The if statement - flowchart Get the person’s grade from the user Check if the grade is greater than or equal to 60 If the grade is greater than or equal to 60 print the “congratulations” message If the grade is less than 60 do nothing End the program ALGORITHM START READ grade grade >= 60? Print “Congratulations! You passed  END True False FLOWCHART Dr. Soha S. Zaghloul 22

15. The if statement - pseudocode Scanf grade If (grade >= 60) Condition true: Printf “Congratulations! You passed” End of the program PSEUDOCODE START READ grade grade >= 60? Print “Congratulations! You passed  END True False FLOWCHART Dr. Soha S. Zaghloul 23

15. The if statement - code scanf (“%d”, &grade); //read grade from user Scanf grade If (grade >= 60) Condition true: Printf “Congratulations! You passed” End of the program PSEUDOCODE Dr. Soha S. Zaghloul 24

15. The if statement - code printf (“Enter your grade: \n”); //prompt scanf (“%d”, &grade); //read grade from user Scanf grade If (grade >= 60) Condition true: Printf “Congratulations! You passed” End of the program PSEUDOCODE Dr. Soha S. Zaghloul 25

15. The if statement - code int grade; //declaration of grade printf (“Enter your grade: \n”); //prompt scanf (“%d”, &grade); //read grade from user Scanf grade If (grade >= 60) Condition true: Printf “Congratulations! You passed” End of the program PSEUDOCODE Dr. Soha S. Zaghloul 26

15. The if statement - code int grade; //declaration of grade printf (“Enter your grade: \n”); //prompt scanf (“%d”, &grade); //read grade from user if (grade >= 60) printf (“Congratulations! You passed  \n”); //Congratulations message Scanf grade If (grade >= 60) Condition true: Printf “Congratulations! You passed” End of the program PSEUDOCODE Dr. Soha S. Zaghloul 27

15. The if statement - code #include <stdio.h> //program begins execution at main function int main (void) { int grade; //declaration of grade printf (“Enter your grade: \n”); //prompt scanf (“%d”, &grade); //read grade from user if (grade >= 60) printf (“Congratulations! You passed  \n”); //Congratulations message } //end of program Scanf grade If (grade >= 60) Condition true: Printf “Congratulations! You passed” End of the program PSEUDOCODE Dr. Soha S. Zaghloul 28

Relationship between two numbers 16. A SAMPLE PROGRAM: Relationship between two numbers Dr. Soha S. Zaghloul 29 Copyright © Pearson, Inc. 2013. All Rights Reserved. ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

Relationship between two numbers (Cont’d) 16. A SAMPLE PROGRAM: Relationship between two numbers (Cont’d) Dr. Soha S. Zaghloul 30 Copyright © Pearson, Inc. 2013. All Rights Reserved. ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

Relationship between two numbers - OUTPUT 16. A SAMPLE PROGRAM: Relationship between two numbers - OUTPUT Dr. Soha S. Zaghloul 31 Copyright © Pearson, Inc. 2013. All Rights Reserved. ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

16. A SAMPLE PROGRAM (Cont.) Comparing Numbers The if statement in lines 17–19 if ( num1 == num2 ) { printf( "%d is equal to %d\n", num1, num2 ); } compares the values of variables num1 and num2 to test for equality. If the values are equal, the statement in line 18 displays a line of text indicating that the numbers are equal. If the conditions are true in one or more of the if statements starting in lines 21, 25, 29, 33 and 37, the corresponding body statement displays an appropriate line of text. Indenting the body of each if statement and placing blank lines above and below each if statement enhances program readability. Dr. Soha S. Zaghloul 32 Copyright © Pearson, Inc. 2013. All Rights Reserved. ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

16. A SAMPLE PROGRAM (Cont.) A left brace, {, begins the body of each if statement (e.g., line 17). A corresponding right brace, }, ends each if statement’s body (e.g., line 19). Any number of statements can be placed in the body of an if statement. Dr. Soha S. Zaghloul 33 Copyright © Pearson, Inc. 2013. All Rights Reserved. ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

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