Week 4 Program Control Structure

Slides:



Advertisements
Similar presentations
Decisions If statements in C.
Advertisements

1 Conditional Statement. 2 Conditional Statements Allow different sets of instructions to be executed depending on truth or falsity of a logical condition.
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
1 Lecture 8:Control Structures I (Selection) (cont.) Introduction to Computer Science Spring 2006.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
COMP 14 Introduction to Programming Miguel A. Otaduy May 18, 2004.
Chapter 4: Control Structures I (Selection)
Java Programming: From Problem Analysis to Program Design, 4e Chapter 4 Control Structures I: Selection.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 30, 2005.
EGR 2261 Unit 4 Control Structures I: Selection  Read Malik, Chapter 4.  Homework #4 and Lab #4 due next week.  Quiz next week.
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.
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 4: Control Structures I (Selection)
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational operators – Discover.
Chapter 4: Control Structures I J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition Second.
Visual C# 2005 Decision Structures. Visual C# Objectives Understand decision making Learn how to make decisions using the if statement Learn how.
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 (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
CPS120: Introduction to Computer Science Decision Making in Programs.
Selection Structures (if & switch statements) (CS1123)
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 4: Control Structures I (Selection)
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.
CPS120: Introduction to Computer Science Decision Making in Programs.
PROBLEM SOLVING & ALGORITHMS CHAPTER 5: CONTROL STRUCTURES - SELECTION.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 21, 2005 Lecture Number: 10.
 Learn about control structures  Examine relational and logical operators  Explore how to form and evaluate logical (Boolean) expressions  Learn how.
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 Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection Structure 3.6The.
Programming 1 DCT 1033 Control Structures I (Selection) if selection statement If..else double selection statement Switch multiple selection statement.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 4 Control Structures I: Selection.
Chapter 4 Control Structures I. Chapter Objectives Learn about control structures Examine relational and logical operators Explore how to form and evaluate.
ICT Introduction to Programming Chapter 4 – Control Structures I.
Chapter 5: Control Structures I (Selection). Objectives In this chapter you will: Learn about control structures Examine relational and logical operators.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
CPS120: Introduction to Computer Science Decision Making in Programs.
CPS120: Introduction to Computer Science Decision Making in Programs.
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.
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#3 STRUCTURED PROGRAM DEVELOPMENT IN C 1 A.AlOsaimi King Saud University College of Applied studies and Community Service Csc 1101.
 By the end of this section you should be able to: ◦ Differentiate between sequence, selection, and repetition structure. ◦ Differentiae between single,
Branching statements.
Chapter 4: Control Structures I
The if…else Selection Statement
More on the Selection Structure
Selections Java.
Decisions Chapter 4.
Week 3 C Program Structures (Selection Structures)
EKT150 INTRODUCTION TO COMPUTER PROGRAMMING
Week 3 – Selection Structures
DKT121: Fundamental of Computer Programming
Lecture 2: Logical Problems with Choices
Structured Program
Chapter 4 Selection.
Chapter 4: Control Structures I (Selection)
Week 3 – Program Control Structure
Structured Program Development in C++
Control Statements Paritosh Srivastava.
Structural Program Development: If, If-Else
Presentation transcript:

Week 4 Program Control Structure Sequence and Selection control structure Sem1-06/07 EKT120: Computer Programming

EKT120: Computer Programming Outline Types of program control structure Sequence control Selection Types of selection One-way selection Two-way selection Multi-selection Compound statement Nested if Conditional operator Switch structure Sem1-06/07 EKT120: Computer Programming

EKT120: Computer Programming Discussion on Lab Exercise 3 : Introduction to LINUX Lab Exercise 4 : Getting Started with C Programming Sem1-06/07 EKT120: Computer Programming

Types of Program Control Structure Sequence Branch or Decision Loop Sem1-06/07 EKT120: Computer Programming

Sequence Control Structure The most common and basic structure of a program A set of instructions in sequence from top to bottom Statements executed one after the other in the order in which they are written-sequential execution Sem1-06/07 EKT120: Computer Programming

EKT120: Computer Programming Example Write a program that creates a table of degrees Celsius with degrees Fahrenheit. Begins at 0 degree and proceed to 100 degree in 20 degree increments. Use no more than 2 variable in your program. Solution The equation F = C + 32 C = degree Celsius F = degree Fahrenheit 9 5 Sem1-06/07 EKT120: Computer Programming

EKT120: Computer Programming Specify Example Perform the pre calculation to expect the correct output for example…. C F 32 20 68 40 104 60 140 80 176 100 212 Sem1-06/07 EKT120: Computer Programming

EKT120: Computer Programming Algorithm (pseudo code) Begin, Declare variables Print headings of table set C = 0 calculate F print C and F set C = 20 . End Sem1-06/07 EKT120: Computer Programming

EKT120: Computer Programming Start Float C,F Algorithm (Flow chart) C=0 F=(9/5)*C + 32 Print C,F C=20 F=(9/5)*C + 32 Print C,F End Sem1-06/07 EKT120: Computer Programming

EKT120: Computer Programming Pseudocode Flowchart C Program Begin Declare variable Print heading Set C = 0 Calculate F Print C and F Set C = 20 Set C = 40 Set C = 60 Set C = 80 Set C = 100 End #include <stdio.h> Void main (void) { float C, F; printf(“Table of Celcius and Fahrenheit”); printf(“Celcius\t\tFahrenheit”); C =20.; F =(9./5.)*C + 32.; printf(“%8.2f\t\t%8.2f”,C, F); C =40.; F :=(9./5.)*C + 32.; C =60.; F :=(9./5.)*C + 32.; C :=80.; F :=(9./5.)*C + 32.; C :=100.; F :=(9./5.)*C + 32.; } Sem1-06/07 EKT120: Computer Programming

EKT120: Computer Programming Explanation 9 5 The equation F = C + 32 The program equation F =(9/5)*C + 32; Command printf(“%8.2f\t\t%8.2f”,C, F); Print format to print float number with 8 numbers or field width and at 2 decimal points. Semicolon ; must have at the end of C command. Sem1-06/07 EKT120: Computer Programming

EKT120: Computer Programming Selection structure Used to choose among alternative courses of action C has three types: if, if..else switch Sem1-06/07 EKT120: Computer Programming

EKT120: Computer Programming Selection Statements Used to control the flow of a program Also called as decision or branches Branches are conditions or choices used to enable selection of program flow Sem1-06/07 EKT120: Computer Programming

EKT120: Computer Programming Types of selection One-way selection = if Two-way selection = if..else Multi-selection Nested if Switch structure = switch Sem1-06/07 EKT120: Computer Programming

Review: Relational Operator Equal To (equality) == Less Than < Greater Than > Less than or equal to <= Greater than or equal to >= Not Equal to !> Sem1-06/07 EKT120: Computer Programming

The if selection structure if structure is a single-entry/single-exit structure Pseudo code Flowchart begin if student’s grade is greater than or equal to 60 Print “Passed” end False Sem1-06/07 EKT120: Computer Programming

EKT120: Computer Programming One-way selection in C Example 1: if (grade >= 60) printf(“Passed\n”); ….. Comment.. (grade>=60) – This is a relation statement which result in logic TRUE or FALSE. If the statement (grade >=60) is true the the command printf(Passed\n) shall be executed. Sem1-06/07 EKT120: Computer Programming

One-way Selection- example char grade; …… if(markah>= 90) grade = 'A'; …... printf(“Grade is : %c\n”, grade); Sem1-06/07 EKT120: Computer Programming

EKT120: Computer Programming One-way Selection = if In C, a condition is represented by a logical (Boolean) expression True and False are logical (Boolean) values The syntax of one-way selection is: if (expression) statement; If the value of the expression is true, statement is executed; if false, statement is not executed and the computer goes on to the next statement in the program. Sem1-06/07 EKT120: Computer Programming

EKT120: Computer Programming One-way Selection = if Example 3: if (temperature is greater than 27 degree and it is not raining) recommended activity is golfing bool rain=false; … If ((temp > 27) && !(rain)) printf(“recommended activity is golfing”); And not Sem1-06/07 EKT120: Computer Programming

EKT120: Computer Programming One-way Selection = if Common Errors if score >= 90 //no parentheses grade = 'A'; if(score >= 90); //; not here Sem1-06/07 EKT120: Computer Programming

Two-way Selection = if..else The syntax of two-way selection is: if (expression) statement1; else statement2; If the value of the expression is true, statement1 is executed; if false, statement2 is executed Relational exp, or logical exp Sem1-06/07 EKT120: Computer Programming

The if..else selection structure Specifies an action to be performed both when the condition is true and when it is false Pseudocode Flowchart begin if student’s grade greater than or equal to 60 then Print “Passed” else Print “Failed” end Display FAILED PASSED Sem1-06/07 EKT120: Computer Programming

Two-way Selection - if..else in C ……… if (grade >=60) printf(“Passed\n”); else printf(“Failed\n”); …… Execute if true Execute if false Sem1-06/07 EKT120: Computer Programming

Two-way Selection = if..else Another example: if (hours > 40.0) //Line 1 wages = 40.0*rate+1.5*rate*hours - 40.0); //Line 2 else //Line 3 wages = hours * rate; //Line 4 If hours is 50, then the statement at Line 2 executes If hours is 30, then the statement at Line 4 executes Sem1-06/07 EKT120: Computer Programming

Multi-selection = if-else if The syntax is: if(exp1) statement1; else if(exp2) statement2; else if(exp3) statement3; … else statement n; An if-else if control structure shifts program control, step by step, through a series of statement blocks. Sem1-06/07 EKT120: Computer Programming

Multi-selection if-else if Example 1: Write a C program that can display the results and the corresponding grades as illustrated in the table Hint : Use flowchart or pseudocode. marks grade 90 – 100 A 80 – 89 B 70 – 79 C 60 – 69 D 0 – 59 F Sem1-06/07 EKT120: Computer Programming

EKT120: Computer Programming Flowchart Sem1-06/07 EKT120: Computer Programming

Multi-selection - if-else if in C if(mark >= 90) printf( “Grade = A\n”); else if(mark >=80) printf(“Grade = B\n”); else if(mark >=70) printf(“Grade = C\n”); else if (mark >= 60) printf( “Grade = D\n”); else printf(“Grade = F); Sem1-06/07 EKT120: Computer Programming

Compound (Block of) Statement Several statements in a body. A set of statements contained within a pair braces. A compound statement (also called a block of statements) takes the form { statement 1; statement 2; . statement n; } It is considered a single statement Sem1-06/07 EKT120: Computer Programming

Compound (Block of) Statement Example: if(mark >= 90) { printf(“Pass\n”); printf(“Grade = A\n”);} else if(mark >=80){ printf(“Pass”\n”); printf(“Grade = B\n”);} else if(mark >=70){ printf(“Grade = C\n”);} else if (mark >= 60){ printf( “Grade = D\n”);} else {printf(“Pass”\n”); printf(“Grade = F);} A Block if statement. If the relational expression is true, the statement in “true” block is (between 2 braces) are executed. Otherwise, the entire block of statements is ignored) False ? Sem1-06/07 EKT120: Computer Programming

EKT120: Computer Programming Nested if When one control statement is within another, it is said to be nested if(exp1) if(exp2) { statement1; statement2; } { statement1; statement2; } Sem1-06/07 EKT120: Computer Programming

EKT120: Computer Programming Nested if Example if (temperature >= 50) { if (temperature >= 80) printf( "Good day for swimming.\n”); else printf( "Good day for golfing.\n“); } printf("Good day to play tennis.\n“); Sem1-06/07 EKT120: Computer Programming

The Conditional Operator The syntax of using the conditional operator is: expression1 ? expression2 : expression3; This is called a conditional expression. The statement: if (a >= b) max = a; else max = b; Is equivalent to the statement: max = (a >= b) ? a : b; Sem1-06/07 EKT120: Computer Programming

EKT120: Computer Programming Switch Structures Similar to if-else if control structure The general form (syntax): switch (expression) { case value1: statements1; break; case value2: statements2; break; . case valuen: statementsn; break; default: statements; } Sem1-06/07 EKT120: Computer Programming

EKT120: Computer Programming Switch Structures The break statement has a special meaning and may or may not appear after each statement. In C, switch, case, break, and default are reserved words. In a switch structure, first the expression is evaluated. The value of the expression is then used to perform the corresponding action. Sem1-06/07 EKT120: Computer Programming

EKT120: Computer Programming Switch Structures The expression is usually an identifier. The value of the expression can be only integral. The expression is sometimes called the selector. Its value determines which statement is selected for execution. A particular case value should appear only once. One or more statements may follow a case label, so you do not need to use braces to turn multiple statements into a single compound statement. The break statement may or may not appear after each statement. Sem1-06/07 EKT120: Computer Programming

EKT120: Computer Programming Switch Structures 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.“); } where, grade is a variable of the type char. If the value of grade is, say 'A', the output is The grade is A. Sem1-06/07 EKT120: Computer Programming

EKT120: Computer Programming Switch Structures The switch statement executes according to the following rules: When the value of the expression is matched against a case value (also called a label), the statements execute until either a break statement is found or the end of the switch structure is reached. If the value of the expression does not match any of the case values, the statements following the default label execute. If the switch structure has no default label, and if the value of the expression does not match any of the case values, the entire switch statement is skipped. A break statement causes an immediate exit from the switch structure Sem1-06/07 EKT120: Computer Programming

EKT120: Computer Programming Summary Sem1-06/07 EKT120: Computer Programming