Chap 5 Control Structure Boolean Expression and the IF Statement.

Slides:



Advertisements
Similar presentations
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Advertisements

CSCI 51 Introduction to Programming Dr. Joshua Stough February 10, 2009.
BBS514 Structured Programming (Yapısal Programlama)1 Functions and Structured Programming.
Chap-7 Robust_Input. A Package for Robust Input Read input values by calls to procedure Get overloading “Get” procedure for Integer and Float values There.
Single-Result Functions Section /25/11. Programming Assignment On website.
Chapter 5 Functions.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 5 Function Basics.
July 13 th.  If/ Else if / Else  Variable Scope  Nested if/else's  Switch statements  Conditional Operator.
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
Copyright © 2012 Pearson Education, Inc. Chapter 6 More Conditionals and Loops Java Software Solutions Foundations of Program Design Seventh Edition John.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 5, 2005.
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
Sequential Statements Module F3.2. Sequential Statements Statements executed sequentially within a process If Statements Case Statements Loop Statements.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
COMP 14 Introduction to Programming Miguel A. Otaduy May 18, 2004.
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 30, 2005.
COMP 110 Introduction to Programming Mr. Joshua Stough September 19, 2007.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design First Edition by Tony Gaddis.
PAGES:51-59 SECTION: CONTROL1 : DECISIONS Decisions.
1. We’ve learned that our programs are read by the compiler in order, from top to bottom, just as they are written The order of statement execution is.
Rational Expressions and selection structures Relational operators Logical operators Selection structures.
Chapter 7 Other Loop forms, Procedures; Exception Handling.
6/3/2016 CSI Chapter 02 1 Introduction of Flow of Control There are times when you need to vary the way your program executes based on given input.
If…else statements. Boolean Expressions Boolean expression - An expression whose value is either true or false true = 1 false = 0 Datatype: boolean.
Chap 7- Control Structures : The WHILE Statement.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
03 August 2004 NLP-AI Java Lecture No. 4 Operators & Decision Constructs Satish Dethe.
System Structures b Package b Abstract Data Type.
Python Conditionals chapter 5
Chapter 6 Counting Loops; Subtypes In preceding chapters, 1.Sequence and 2.Conditional structures In this chapter, 3.Repetition, or iteration.
Functions: Part 2 of /11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park 1.
ICT Introduction to Programming Chapter 4 – Control Structures I.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
CS241 PASCAL I - Control Structures1 PASCAL Control Structures Modified Slides of Philip Fees.
Slide: 1 Copyright © AdaCore Your First Ada Program Presented by Quentin Ochem University.adacore.com.
Arithmetic Expressions Addition (+) Subtraction (-) Multiplication (*) Division (/) –Integer –Real Number Mod Operator (%) Same as regular Depends on the.
Control statements Mostafa Abdallah
Chapter 3 The General Structure of Ada Programs. General Form of an Ada Program With package1; With package2;... With packagen; Procedure pname IS - -
Midterm Exam Topics (Prof. Chang's section) CMSC 201.
Expressions Methods if else Statements Loops Potpourri.
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.
Dr. Sajib Datta Jan 21,  Declare a variable ◦ int height; [note that no value is still assigned]  Assign a variable a value ◦ height =
Chapter 3 Control Statements
Subtype Enumeration type
Selections Java.
Functions, Part 2 of 2 Topics Functions That Return a Value
Chapter 5 Functions DDC 2133 Programming II.
Chapter 5 Function Basics
CMPT 201 Functions.
Chapter 3 Control Statements Lecturer: Mrs Rohani Hassan
Chapter 4: Decision Structures and Boolean Logic
The switch Statement The switch statement provides another way to decide which statement to execute next The switch statement evaluates an expression,
Sequential Statements
Chapter 4: Decision Structures and Boolean Logic
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Pages:51-59 Section: Control1 : decisions
Summary Two basic concepts: variables and assignments Basic types:
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Understanding Conditions
C:\> sort_3_numbers.exe |
A simple function.
Single-Result Functions & Modularity
Pages:51-59 Section: Control1 : decisions
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Chapter 4: Decision Structures and Boolean Logic
Arrays, Part 1 of 2 Topics Definition of a Data Structure
REPETITION Why Repetition?
Presentation transcript:

Chap 5 Control Structure Boolean Expression and the IF Statement

IF Statement (One Alternative) IF condition THEN statement sequence T statement sequence T END IF; if condition is true then if condition is true then execute the statement sequence T execute the statement sequence T

Example for IF statement IF X>=18 THEN Adult := Adult +1 Adult := Adult +1 END IF;

IF Statement (Two Alternative) IF Condition THEN statement sequence T statement sequence TELSE statement sequence F statement sequence F END IF;

IF THEN ELSE if condition is true then execute statement sequence T and statement sequence F is skipped otherwise, statement sequence F is executed and statement sequence F is skipped

Example IF X >= 0.0 THEN Ada.TEXT_IO.Put (Item => “ X is Positive “); Ada.TEXT_IO.Put (Item => “ X is Positive “);ELSE Ada.TEXT_IO.Put (Item => “ X is Negative”); Ada.TEXT_IO.Put (Item => “ X is Negative”); END IF;

Boolean Expression variable relational operator variable e.g. X > Y X > Y variable relational operator constant e.g. X >= 0.0 X >= 0.0

Relational Operator <= less or equal to < less than >= greater than or equal to > greater than = equal to /= not equal to

Let us test how much we understand Suppose x, y, z : integer; if x := 2; if x := 2; y := 5; y := 5; z := 0; z := 0; Find the value for z after executing IF x >= y THEN IF x >= y THEN z := z +1; z := z +1; ELSE ELSE z := z +2; z := z +2; END IF END IF

The Multiple-Alternative IF Statement IF condition-1 THEN statement sequence 1; statement sequence 1; ELSIF condition-2 THEN statement sequence 2; statement sequence 2; ELSIF condition-k THEN statement sequence k; statement sequence k;ELSE statement sequence n; statement sequence n; END IF;

Example IF N>= 0 THEN Ada.Text_IO.Put (Item=> “ Positive”); Ada.Text_IO.Put (Item=> “ Positive”); ELSIF N=0 THEN Ada.Text_IO.Put (Item => “Zero”); Ada.Text_IO.Put (Item => “Zero”);ELSE Ada.Text_IO.Put (Item => “Negative”); Ada.Text_IO.Put (Item => “Negative”); END IF;

More Example Exam ScoreGrade and aboveA 80-89B 70-79C 60-69D belowF

Grade problem grade assignment IF score >= 90 THEN Ada.Text_IO.Put ( Item => “ You got, A”); Ada.Text_IO.Put ( Item => “ You got, A”); ELSIF score >= 80 THEN Ada.Text_IO.Put ( Item => “ You got, B”); Ada.Text_IO.Put ( Item => “ You got, B”); ELSIF score >= 70 THEN Ada.Text_IO.Put ( Item => “ You got, C”); Ada.Text_IO.Put ( Item => “ You got, C”); ELSIF score >= 60 THEN Ada.Text_IO.Put ( Item => “ You got, D”); Ada.Text_IO.Put ( Item => “ You got, D”);ELSE Ada.Text_IO.Put ( Item => “ You got, F”); Ada.Text_IO.Put ( Item => “ You got, F”); END IF

Think about this problem if you speed up in 35 mile limit zone Fees are as follow: Fees are as follow: $20 for under 50 mph, $20 for under 50 mph, $40 for under 75 mph and $40 for under 75 mph and $60 for more than 75 mph $60 for more than 75 mph

Is This Answer “A” Ok? IF Speed > 35 THEN Fee := 20.00; Fee := 20.00; ELSIF Speed >50 THEN Fee := Fee := ELSIF Speed >75 THEN Fee := Fee := END IF;

Or Is This Answer “B” OK ? IF Speed > 75 THEN Fee := 60.00; Fee := 60.00; ELSIF Speed > 50THEN Fee := 40.00; Fee := 40.00; ELSIF Speed > 35THEN Fee := 20.00; Fee := 20.00; END IF;

Using Ada’s Math Library With Ada.Numerics.Elementary_Functions; Example Subtype NonNegFloat IS FLOAT Range Float’Last; First : NonNegFloat; First:=Ada.Numerics.Elementary_Functions.Sqrt(X=> First);

Writing Functions Function Specifications In Chap 4, we found that Function Year (Date : Time) Return Year_Number; Function Month (Date: Time) Return Month_Number; Function Day (Date: Time) Return Day_Number;

Functions b Function Specification b Function Body

FUNCTION SPECIFICATION FUNCTION Func_Name ( formal parameter) RETURN result _type; RETURN result _type;Example FUNCTION Maximum (VaL1, Val2 : Integer) RETURN Integer; RETURN Integer;

FORM for FUNCTION BODY FUNCTION Func_Name (formal parameter) RETURN result_type IS local declaration section RETURN result_type IS local declaration section BEGIN BEGIN statement sequence statement sequence END Func_Name END Func_Name

Example for FUNCTION BODY FUNCTION Square (Num : Integer) RETURN Integer IS Result : Integer; Result : Integer; BEGIN BEGIN Result := Num * Num; Result := Num * Num; RETURN Result; RETURN Result; END Square; END Square;

Maximum Function FUNCTION Maximum ( VaL1, VaL2 : Integer) RETURN Integer IS Result : Integer; BEGIN IF VaL1 > VaL2 THEN IF VaL1 > VaL2 THEN Result := VaL1; Result := VaL1; ELSE ELSE Result := VaL2; Result := VaL2; End IF; RETURN Result; END Maximum;

Calling A Function Score1 := 78; Score2 := 89; Larger := Maximum( VaL1=> Score1,VaL2 => Score2 ); What is the value of Large ?

Example Program with Function With Ada.Text_IO; With Ada.Integer_Text_IO; Procedure Large Is - - This program is to find the maximum value of given two numbers Declare Variables Num1, Num2, Large : Integer;

Cont. Of Large_Small Program Function Specification FUNCTION Maximum (VaL1, Val2 : Integer) Return Integer; Function Body FUNCTION Maximum ( VaL1, VaL2 : Integer) RETURN Integer IS Result : Integer; BEGIN IF VaL1 > VaL2 THEN IF VaL1 > VaL2 THEN Result := VaL1; Result := VaL1; ELSE ELSE Result := VaL2; Result := VaL2; End IF; End IF; RETURN Result; RETURN Result; END Maximum;

Cont.. Begin Ada.Text_IO.Put (Item =>" Give Number 1 : "); Ada.Integer_Text_IO.Get(Item => Num1); Ada.Text_IO.New_Line; Ada.Text_IO.Put (Item =>" Give Number 2 : "); Ada.Integer_Text_IO.Get(Item => Num2); Ada.Text_IO.New_Line; Large := Maximum( VaL1=>Num1, VaL2=>Num2); Ada.Text_IO.Put (Item => " Large Number is : "); Ada.Integer_Text_IO.Put(Item => Large); Ada.Text_IO.New_Line; End Large;

Writing Package b Package Specification b Package Body

Package Specification PACKAGE Pack_Name IS list of specifications of resourses list of specifications of resourses provided by the package provided by the package END Pack_Name ;

Example for Package Specification PACKAGE Min_Max IS FUNCTION Minimum ( VaL1, VaL2 : Integer) RETURN Integer; FUNCTION Minimum ( VaL1, VaL2 : Integer) RETURN Integer; FUNCTION Maximum ( VaL1, VaL2 : Integer)RETURN Integer; FUNCTION Maximum ( VaL1, VaL2 : Integer)RETURN Integer; END Min_Max;

Form for Package Body PACKAGE BODY Pack_Name IS sequence of function and procedure bodies implementing the resources listed in the package specification for Pack_Name sequence of function and procedure bodies implementing the resources listed in the package specification for Pack_Name END Pack_Name

Example For Package Body PACKAGE BODY Min_Max IS bodies of functions for Min_Max package FUNCTION Minimum ( VaL1, VaL2 : Integer) RETURN Integer IS Result : Integer; BEGIN IF VaL1 <VaL2 THEN IF VaL1 <VaL2 THEN Result := VaL1; Result := VaL1; ELSE ELSE Result := VaL2; Result := VaL2; End IF; End IF; RETURN Result; RETURN Result; END Minimum;

Cont… Of The Min_Max PACKAGE BODY FUNCTION Maximum ( VaL1, VaL2 : Integer) RETURN Integer IS Result : Integer; BEGIN IF VaL1 > VaL2 THEN IF VaL1 > VaL2 THEN Result := VaL1; Result := VaL1; ELSE ELSE Result := VaL2; Result := VaL2; End IF; End IF; RETURN Result; RETURN Result; END Maximum; END Min_Max;

Calling Functions from Min_Max Package With Ada.Text_IO; With Ada.Integer_Text_IO; With Min_Max; our new package Num1, Num2, Large, Small : Integer; Large := Min_Max.Maximum( VaL1=>Num1, VaL2=>Num2); Small := Min_Max.Minimum( VaL1=>Num1, VaL2=>Num2);