CHAPTER#3 STRUCTURED PROGRAM DEVELOPMENT IN C 1 A.AlOsaimi King Saud University College of Applied studies and Community Service Csc 1101.

Slides:



Advertisements
Similar presentations
BBS514 Structured Programming (Yapısal Programlama)1 Selective Structures.
Advertisements

C How to Program, 6/e Summary © by Pearson Education, Inc. All Rights Reserved.
Structured Program Development in C
CHAPTER#3 STRUCTURED PROGRAM DEVELOPMENT IN C++ 1 st semester King Saud University College of Applied studies and Community Service Csc 1101.
Week 3 – Selection Structures UniMAP SemPGT C PROGRAMMING1.
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.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Structural Program Development: If, If-Else Outline.
Chapter 4: Control Structures I J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition Second.
UniMAP Sem II-09/10EKT120: Computer Programming1 Week 3 – Selection Structures.
Chapter 4: Control Structures I J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
Chapter 4: Control Structures I
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.
Chapter 04 Control Statements: Part II. OBJECTIVES In this part you will learn: if…else Double-Selection Statement. while Repetition Statement.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 21, 2005 Lecture Number: 10.
Control Structures 1. Control Structures Java Programming: From Problem Analysis to Program Design, D.S. Malik 2.
Chapter 4: Control Structures SELECTION STATEMENTS.
Lecture 3 – Selection. Outline Recall selection control structure Types of selection One-way selection Two-way selection Multi-selection Compound statement.
CHAPTER#3 STRUCTURED PROGRAM DEVELOPMENT IN C++ 1 st semester King Saud University College of Applied studies and Community Service Csc 1101.
Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection Structure 3.6The.
Lecture 2 Control Structure. Relational Operators -- From the previous lecture Relational Operator Meaning == is equal to < is less than > is greater.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 3 (2) & 4 September 8 & 10, 2009.
Lecture 4: C/C++ Control Structures Computer Programming Control Structures Lecture No. 4.
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.
Structured Program Development Angela Chih-Wei Tang ( 唐 之 瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan 2010.
Chapter 4 Control Structures I. Chapter Objectives Learn about control structures Examine relational and logical operators Explore how to form and evaluate.
Chapter#3 Part1 Structured Program Development in C++
Chapter 4: Control Structures I J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition Second.
Week 4 Program Control Structure
CONTROL STRUCTURE. 2 CHAPTER OBJECTIVES  Learn about control structures.  Examine relational and logical operators.  Explore how to form and evaluate.
CONTROL STRUCTURE Chapter 3. CONTROL STRUCTURES ONE-WAY SELECTION Syntax: if (expression) statement Expression referred to as decision maker. Statement.
C++ Programming Lecture 5 Control Structure I (Selection) – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook.
Programming Language C++ Lecture 3. Control Structures  C++ provides control structures that serve to specify what has to be done to perform our program.
CHAPTER#3 STRUCTURED PROGRAM DEVELOPMENT IN C++ 2 nd semester King Saud University College of Applied studies and Community Service Csc 1101.
Control Statements: Part1  if, if…else, switch 1.
 By the end of this section you should be able to: ◦ Differentiate between sequence, selection, and repetition structure. ◦ Differentiae between single,
Chapter 4: Control Structures I J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition Second.
Chapter 3 Structured Program Development in C C How to Program, 8/e, GE © 2016 Pearson Education, Ltd. All rights reserved.1.
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.
Branching statements.
Chapter 4: Control Structures I
The if…else Selection Statement
- Standard C Statements
Chapter 4: Control Structures I
Week 3 C Program Structures (Selection Structures)
Week 3 – Selection Structures
Chapter 2.1 Control Structures (Selection)
CSC113: Computer Programming (Theory = 03, Lab = 01)
DKT121: Fundamental of Computer Programming
Programming Fundamentals
Lecturer CS & IT Department UOS MBDIN
SELECTION STATEMENTS (1)
Chapter 4: Control Structures I
Lecture 2: Logical Problems with Choices
Structured Program
3 Control Statements:.
Chapter#3 Structured Program Development in C++
Decision making If statement.
Control Structure Chapter 3.
Week 3 – Program Control Structure
Control Structures Lecture 6.
Structured Program Development in C++
Control Statements Paritosh Srivastava.
Branching statements Kingdom of Saudi Arabia
Dale Roberts, Lecturer IUPUI
Control Structure.
Structural Program Development: If, If-Else
Presentation transcript:

CHAPTER#3 STRUCTURED PROGRAM DEVELOPMENT IN C 1 A.AlOsaimi King Saud University College of Applied studies and Community Service Csc 1101

Control structure  Normally, statements in a program are executed one after the other in the order in which they’re written.This is called sequential execution.  Various C statements we’ll soon discuss enable you to specify that the next statement to be executed may be other than the next one in sequence.This is called transfer of control. 2 A.AlOsaimi

Types of Control structure  could be written in terms of only three control structures:  sequence structure  selection structure  repetition structure. 3 A.AlOsaimi

Types of Control Structures A.AlOsaimi 4

Selection structure  C provides three types of selection structures in the form of statements:  The if selection statement either performs (selects) an action if a condition is true or skips the action if the condition is false.  The if … else selection statement performs an action if a condition is true and performs a different action if the condition is false.  The switch selection statement performs one of many different actions depending on the value of an expression. 5 A.AlOsaimi

selection structure : if  The if statement is called a single-selection statement because it selects or ignores a single action.  Syntax: if (expression) statement Expression referred to as decision maker. Statement referred to as action statement. 6 A.AlOsaimi

If – example  Write a program that reads a grade and prints “passed” if the grade is greater than or equal 60. if ( grade >= 60 ) { printf( "Passed\n" ); } /* end if */ 7 A.AlOsaimi

selection structure : if.. else  The if…else statement is called a double- selection statement because it selects between two different actions.  Syntax: if (expression) statement else statement 8 A.AlOsaimi

If..else- Example  For example, the pseudocode statement  If student’s grade is greater than or equal to 60 Print “Passed” else Print “Failed” if ( grade >= 60 ) printf( "Passed\n" ); else printf( "Failed\n" ); 9 A.AlOsaimi

Compound (Block of) Statements A.AlOsaimi 10  Syntax:  {  statement1  statement2 .  statementn  }

Compound (Block of) Statements A.AlOsaimi 11 if ( grade >= 60 ) printf( "Passed\n" ); else { printf( "Failed\n" ); printf( “you have to take this course again\n" ); } Java Programming: From Problem Analysis to Program Design, D.S. Malik

Multiple Selection: Nested if if (expression1) statement1 else if(expression2) statement2 else statement3 Multiple if statements can be used if there is more than two alternatives else is associated with the most recent if that does not have an else syntax 12 A.AlOsaimi

Nested if - example  Many C programmers prefer to write the preceding if statement as if ( grade >= 90 ) printf( "A\n" ); else if ( grade >= 80 ) printf( "B\n" ); else if ( grade >= 70 ) printf( "C\n" ); else if ( grade >= 60 ) printf( "D\n" ); else printf( "F\n" ); 13 A.AlOsaimi

Nested if - example if( tempreture >= 50 ) if (tempreture >= 80) printf (“Good swimming day”); else printf (“Good golfing day”); else printf (“Good tennis day”); 14 A.AlOsaimi

Multiple Selection: Nested if A.AlOsaimi 15 if( tempreture >= 50 ) if (tempreture >= 80) printf(“Good swimming day”); else printf(“Good golfing day”);

Multiple Selection: Nested if A.AlOsaimi 16 //The following code does not work as intended. For example if GPA=3.8 if ( GPA >= 2.0 ) if (GPA >= 3.9) printf(“Deen Honor list\n”); else printf(“GPA below graduation requirement\n”); //This is the correct way of writing the above code if ( GPA >= 2.0 ) { if (GPA >= 3.9) printf(“Deen Honor list”); } else printf(“GPA below graduation requirement\n”);

If …else = conditional expression  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. Syntax:  expression1 ? expression2 : expression3 If expression1 = true, then the result of the condition is expression2.  Otherwise, the result of the condition is expression3. 17 A.AlOsaimi

Conditional (? :) Operator- example A.AlOsaimi 18 Example: int x = 5, y =3, min ; if (x< = y) min = x ; else min = y ; The above statement can be written using the conditional operator : min = ( x<= y ) ? x : y ;

selection structure : switch  The switch statement is called a multiple- selection statement because it selects among many different actions. 19 A.AlOsaimi

Switch Structures A.AlOsaimi 20 Expression is also known as selector. Expression can be an identifier. Value can only be integral. switch (expression) { case value1: statements1 break; case value2: statements2 break;... case valuen: statementsn break; default: statements }

Switch With break Statements A.AlOsaimi 21 switch (N) { case 1: x = 10; break; case 2: x = 20; break; case 3: x = 30; break; } x = 10; false true N == 1 ? x = 20; x = 30; N == 2 ? N == 3 ? false true break;

A.AlOsaimi 22 Switch With No break Statements switch (N) { case 1: x = 10; case 2: x = 20; case 3: x = 30; } x = 10; false true N == 1 ? x = 20; x = 30; N == 2 ? N == 3 ? false true

Switch With break Statements A.AlOsaimi 23 Example switch (grade) { case 'A': printf("The grade is A."); break; case 'B': printf("The grade is B."); break; case 'C': printf("The grade is C."); break; case 'D': printf("The grade is D."); break; case 'F': printf("The grade is F."); break; default: printf("The grade is invalid."); }

Previous Example - With Nested If A.AlOsaimi 24  if (grade == 'A') printf("The grade is A."); else if (grade == 'B') printf("The grade is B."); else if (grade == 'C') printf("The grade is C."); else if (grade == 'D') printf("The grade is D."); else if (grade == 'F') printf("The grade is F."); else printf("The grade is invalid.");

Exercise  Write a program that reads the price and the production date ( month and year of an item ) then the program prints the price after applying the discount rate which equals to :  50% if it is produced before  30% if it is produced in 2011 but before the 4 th month.  10% if it is produced after 4,2011 and before 8, A.AlOsaimi