Chapter 4: Control Structures

Slides:



Advertisements
Similar presentations
ALGORITHMS AND FLOWCHARTS
Advertisements

Relational and Equality Operators Table 4-1. Sample Conditions Table 4-2.
ALGORITHMS AND FLOWCHARTS
Exercise (1).
MS-Excel XP Lesson 5. Exponentiation 1.A1  2 A2  3 A3  =A1^A2 B1  =2^4 2.^ for exponentiation.
Al-Karma Language School Computer Department Prep. 3.
 Control structures  Algorithm & flowchart  If statements  While statements.
Conditions and if/else. Conditions score > 90 Evaluates to true (1) or false (0) Generally … variable operator variable variable operator constant.
School of Computing Science CMT1000 Ed Currie © Middlesex University 1 CMT1000: Introduction to Programming Ed Currie Lecture 5B: Branch Statements - Making.
Chapter 4 MATLAB Programming Logical Structures Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Programming with MATLAB. Relational Operators The arithmetic operators has precedence over relational operators.
division algorithm Before we study divisibility, we must remember the division algorithm. r dividend = (divisor ⋅ quotient) + remainder.
ALGORITHMS AND FLOWCHARTS
Using Java MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE Lecture 7 Decision Making : selection statements.
Flowcharts! January 13, 2005 These are today’s notes! Do you think we will get more snow?
Lecture 2: Logical Problems with Choices. Problem Solving Before writing a program Have a thorough understanding of the problem Carefully plan an approach.
Chapter 6: Control Structures Computer Programming Skills Second Term Department of Computer Science Foundation Year Program Umm Alqura.
PROBLEM SOLVING & ALGORITHMS CHAPTER 5: CONTROL STRUCTURES - SELECTION.
CHAPTER 8 CONTROL STRUCTURES Prepared by: Lec. Ghader R. Kurdi.
ALGORITHMS AND FLOWCHARTS CSCI 105 – Computer Fluency.
1 Chapter 2 - Algorithms and Design print Statement input Statement and Variables Assignment Statement if Statement Flowcharts Flow of Control Looping.
Chapter 5 Logic; Got Any?. Flow of Control The order in which the computer executes statements in a program Control Structure A statement used to alter.
If…else statements. Boolean Expressions Boolean expression - An expression whose value is either true or false true = 1 false = 0 Datatype: boolean.
Lecture 2 Control Structure. Relational Operators -- From the previous lecture Relational Operator Meaning == is equal to < is less than > is greater.
Lecture 4: C/C++ Control Structures Computer Programming Control Structures Lecture No. 4.
Unit 2 – Algorithms & Pseudocode. Algorithms Computer problems solved by executing series of action in order Procedure –The Actions to execute –The Order.
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
COMP 110 Branching Statements and Boolean Expressions Luv Kohli September 8, 2008 MWF 2-2:50 pm Sitterson
Examples of Flow Charts Pseudocodes
Why Repetition? Read 8 real numbers and compute their average REAL X1, X2, X3, X4, X5, X6, X7, X8 REAL SUM, AVG READ *, X1, X2, X3, X4, X5, X6, X7, X8.
ALGORITHMS AND FLOWCHARTS. A typical programming task can be divided into two phases: Problem solving phase  produce an ordered sequence of steps that.
Lecture 2: Introduction to Programming EEE2108: 공학프로그래밍 서강대학교 전자공학과 2011 학년도 2 학기 - Algorithms and Flowcharts -
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.
ALGORITHMS AND FLOWCHARTS
Line Continuation, Output Formatting, and Decision Structures
COVERED BASICS ABOUT ALGORITHMS AND FLOWCHARTS
Chapter 4 MATLAB Programming
EKT150 INTRODUCTION TO COMPUTER PROGRAMMING
Algorithms and Flowcharts
Bell Work 9/15/17 Solve the Inequalities for x and give two possible solutions. (what are some numbers that would work for x?) 1. 2
Computer Science 101 While Statement.
SELECTION STATEMENTS (1)
Control Statement Examples
ALGORITHMS AND FLOWCHARTS
Line Continuation, Output Formatting, and Decision Structures
ALGORITHMS AND FLOWCHARTS
PROBLEM SOLVING CSC 111.
Control Structure Senior Lecturer
SELECTION STATEMENTS (2)
1) C program development 2) Selection structure
Algorithm Discovery and Design
Iteration: Beyond the Basic PERFORM
ALGORITHMS AND FLOWCHARTS
MATH TERMS Terms we need to know!.
3 Control Statements:.
Decision making If statement.
Chapter 5: Control Structure
A statement made based on observations
Flowcharts and Pseudo Code
CHAPTER 5: Control Flow Tools (if statement)
Control Statements.
Dividing Integers ÷ = + ÷ = + ÷ = + ÷ =.
If-Statements and If/Else Statements
Chapter 2 Sets Active Learning Lecture Slides
Life is Full of Alternatives Part 3
Lecture 9: Implementing Complex Logic
REPETITION Why Repetition?
Control Structures.
Presentation transcript:

Chapter 4: Control Structures Selection Statement Selection Structure

Control Structures

Selection Structures One way selection (single if) Two way selection (if…else) Multiple Selection (Nested if…else)

One-Way Selection

If G more than or equal to 60 End Write a program that read the grade of the student and if it is more than or equal 60; print “Pass” Start Start Read the grade G If G more than or equal to 60 Print “Pass” End Read G If G >= 60 False True Print “pass” End

Write a program that divide one integer by another only if the divisor not equal to 0. Start Read first number (dividend ) Read second number (divisor) If divisor not equal 0 Result = dividend / divisor Print Result End Start Read D1 Read D2 If D2 != 0 True False Result= D1 / D2 Print Result End

Two-Way Selection

Write an algorithm to read two numbers and print the largest one Start Read first number (num1) Read second number (num2) If num1 is larger than num2 Print num1 else Print num2 Start Read num1 Read num2 If num1 > num2 False True Print num1 Print num2 End

Write a program that read a temperature and if it is more than 35 print “Summer”, otherwise print “winter” Start Read D If D>35 Print “Summer” Else Print “winter” End

Start Read g1,g2,g3 Average = (g1+g2+g3)/3 If Average >= 60 Else Write a program that read 3 grades for a student and find the average. If the average more than or equal 60; print “pass”, otherwise print “fail” Start Read g1,g2,g3 Start Read g1,g2,g3 Average = (g1+g2+g3)/3 If Average >= 60 Print “pass” Else Print “fail” End Avg = (g1+g2+g3)/3 If avg >= 60 False True Print “Pass” Print “Fail” End

Multiple Selection: Nested if

Start Read a number (N) If N greater than 0 Else if N less than 0 Else Write a program that read a number then print a message saying whether the number is positive, negative or zero. Start Start Read a number (N) If N greater than 0 Print “Positive” Else if N less than 0 Print “Negative” Else Print “Zero” Read N If N > 0 T Print “Positive” F If N < 0 T F Print “Negative” Print “Zero” End

Bad Solution!! Start Read N T If N > 0 F Print “Positive” Why check again ?!! If N < 0 T F Print “Negative” Print “Zero”

If students grade is greater than or equal to 90 Display ‘A’ write a pseudocode / algorithm to work out what grade a student got based on the following information :- if student (above or equal) to 90 -> A , 80 –> B, 70 –> C, 60 –> D, less then or equal 60 –> F Start Read grade If students grade is greater than or equal to 90 Display ‘A’ Else If students grade is greater than or equal to 80 Display ‘B’ Else If students grade is greater than or equal to 70 Display ‘C’ Else If students grade is greater than or equal to 60 Display ‘D’ Else Display ‘F’ End

Start Read G If G >= 90 T F Print “A” If G >= 80 T F If G >= 70 F T Print “B” If G >= 60 T F Print “C” Print “D” Print “F” End