CS 199 Computer Programming. Spring 2018 Lecture 5 Control Statements

Similar documents
BIL101E: Introduction to Computers and Information systems Lecture 8

Computational Expression

Flow of Control. Flow of control The order in which statements are executed. Transfer of control

V2 2/4/ Ch Programming in C. Flow of Control. Flow of Control. Flow of control The order in which statements are executed

Iterative Statements. Iterative Statements: Examples. Counter-Controlled Loops. ICOM 4036 Programming Languages Statement-Level Control Structure

Lecture 5 Tao Wang 1

Decision Making -Branching. Class Incharge: S. Sasirekha

Loops! Loops! Loops! Lecture 5 COP 3014 Fall September 25, 2017

Objectives. Chapter 4: Control Structures I (Selection) Objectives (cont d.) Control Structures. Control Structures (cont d.) Relational Operators

Chapter 4: Control Structures I (Selection) Objectives. Objectives (cont d.) Control Structures. Control Structures (cont d.

Kingdom of Saudi Arabia Princes Nora bint Abdul Rahman University College of Computer Since and Information System CS240 BRANCHING STATEMENTS

CS 199 Computer Programming. Spring 2018 Lecture 2 Problem Solving

5. Selection: If and Switch Controls

Loops and Conditionals. HORT Lecture 11 Instructor: Kranthi Varala

Introduction to Programming

Structured Program Development

Chapter 8 Statement-Level Control Structures

LECTURE 5 Control Structures Part 2

C++ Programming: From Problem Analysis to Program Design, Third Edition

Programming Basics and Practice GEDB029 Decision Making, Branching and Looping. Prof. Dr. Mannan Saeed Muhammad bit.ly/gedb029

Computers and FORTRAN Language Fortran 95/2003. Dr. Isaac Gang Tuesday March 1, 2011 Lecture 3 notes. Topics:

Theory of control structures

Introduction. C provides two styles of flow control:

Control Structures. Lecture 4 COP 3014 Fall September 18, 2017

C++ Programming Lecture 7 Control Structure I (Repetition) Part I

Relational Expressions. Boolean Expressions. Boolean Expressions. ICOM 4036 Programming Languages. Boolean Expressions

C++ Programming: From Problem Analysis to Program Design, Fourth Edition. Chapter 4: Control Structures I (Selection)

COP 1220 Introduction to Programming in C++ Course Justification

Problem Solving through Programming In C Prof. Anupam Basu Department of Computer Science & Engineering Indian Institute of Technology, Kharagpur

Flow Control. CSC215 Lecture

Chapter 7. - FORTRAN I control statements were based directly on IBM 704 hardware

Introduction to C/C++ Lecture 3 - Program Flow Control

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

Module 4: Decision-making and forming loops

Java. Programming: Chapter Objectives. Why Is Repetition Needed? Chapter 5: Control Structures II. Program Design Including Data Structures

LECTURE 04 MAKING DECISIONS

Chapter 4 - Notes Control Structures I (Selection)

Chapter 5: Control Structures

Chapter 7: Javascript: Control Statements. Background and Terminology. Background and Terminology. Background and Terminology

Chapter 4: Control Structures I (Selection)

Chapter 4: Making Decisions

Objectives. Chapter 4: Control Structures I (Selection) Objectives (cont d.) Control Structures

Control Structures. A program can proceed: Sequentially Selectively (branch) - making a choice Repetitively (iteratively) - looping

CP FAQS Q-1) Define flowchart and explain Various symbols of flowchart Q-2) Explain basic structure of c language Documentation section :

Chapter 4: Making Decisions

Chapter 8. Statement-Level Control Structures

Control Structures. Outline. In Text: Chapter 8. Control structures Selection. Iteration. Gotos Guarded statements. One-way Two-way Multi-way

Computer Programming: C++

Week 2. Relational Operators. Block or compound statement. if/else. Branching & Looping. Gaddis: Chapters 4 & 5. CS 5301 Spring 2018.

There are algorithms, however, that need to execute statements in some other kind of ordering depending on certain conditions.

Microsoft Visual Basic 2005: Reloaded

CIS 120. Introduction to Programming

Information Science 1

BITG 1223: Selection Control Structure by: ZARITA (FTMK) LECTURE 4 (Sem 1, 16/17)

IS 0020 Program Design and Software Tools

CHAPTER 2.1 CONTROL STRUCTURES (SELECTION) Dr. Shady Yehia Elmashad

Control Structures in Java if-else and switch

Chapter 3 Structured Program Development

Chapter 5: Control Structures II (Repetition) Objectives (cont d.) Objectives. while Looping (Repetition) Structure. Why Is Repetition Needed?

Chapter 8 Statement-Level Control Structure

Chapter 4: Control structures. Repetition

C++ Programming: From Problem Analysis to Program Design, Third Edition

Introduction to C Programming

Chapter 8. Statement-Level Control Structures

Ch. 7: Control Structures

C++ PROGRAMMING SKILLS Part 2 Programming Structures

Chapter 4: Control structures

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal

STUDENT OUTLINE. Lesson 8: Structured Programming, Control Structures, if-else Statements, Pseudocode

Structured Program Development in C

Chapter 8. Statement-Level Control Structures

CSE 1223: Introduction to Computer Programming in Java Chapter 3 Branching

Selection Control Structure CSC128: FUNDAMENTALS OF COMPUTER PROBLEM SOLVING

Control Structures in Java if-else and switch

Control Structures. Code can be purely arithmetic assignments. At some point we will need some kind of control or decision making process to occur

Condition-Controlled Loop. Condition-Controlled Loop. If Statement. Various Forms. Conditional-Controlled Loop. Loop Caution.

REPETITION CONTROL STRUCTURE LOGO

Add Subtract Multiply Divide

Score score < score < score < 65 Score < 50

Information Science 1

Programming Logic and Design Ninth Edition

CS Computer Science I

Math 180 Written Homework Solutions Assignment #1 Due Thursday, September 4th at the beginning of your discussion class.

Computational Physics - Fortran February 1997

CS 302: INTRODUCTION TO PROGRAMMING. Lectures 7&8

Statement level control structures

In this chapter you will learn:

INTRODUCTION TO COMPUTER SCIENCE - LAB

Chapter 4 C Program Control

Chapter 8. Statement-Level Control Structures ISBN

Chapter 4 C Program Control

Chapter 2 - Control Structures

Chapter 5: Control Structures II (Repetition)

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal

Lecture 7 Tao Wang 1

Introduction to OpenMP

Fundamentals of Programming. Lecture 6: Structured Development (part one)

Solving Problems Flow Control in C++ CS 16: Solving Problems with Computers I Lecture #3

Excel Functions & Tables

CHAPTER 2.2 CONTROL STRUCTURES (ITERATION) Dr. Shady Yehia Elmashad

Transcription:

CS 199 Computer Programming Spring 2018 Lecture 5 Control Statements

Control Structures 3 control structures Sequence structure Programs executed sequentially by default Branch structure Unconditional branch goto Control proceeds dependent on conditions if, if/else, switch Repetition structures (loop instructions) Control repeated until condition met while, do/while, for

Control Structures (cont.)

Conditional Statements A conditional statement allows the program to make a decision or comparison and then select one of two paths, depending on the result of the comparison. Two constructs if statement if if-else if-else-if Select case statement

Basic If-Statement Syntax if (expression) then body End if Semantics: if the expression is then execute body Body is either a single statement or a group of statements. Example 1: if (v > 0) then v = 0 end if expression body 5

Condition Condition is a logical expression that evaluates to or. It could be a relational or Boolean expression. Simple conditions are built using Relational Operators: <, >, >=, <= Equality Operators: ==, /= Beware of mistaking the assignment = for the equality == Standard algebraic equality operator or relational operator Relational operators FORTRAN equality or relational operator Example of FORTRAN condition Meaning of FORTRAN condition > > x > y x is greater than y < < x < y x is less than y >= x >= y x is greater than or equal to y <= x <= y x is less than or equal to y Equality operators = == x == y x is equal to y /= x /= y x is not equal to y 6

Example 1 The following FORTRAN program finds the absolute value of an integer. N START Input N1 N1<0 Print The absolute value is, N1 Y N1 -N1 Integer :: N1 Write (*,*) "Enter an integer: " Read (*,*) N1 if (N1 < 0) then End if N1 = -N1 Write (*,*) "The absolute value is, N1 end STOP

Logical Operators Logical operators are used to combine more than one condition forming a complex condition. FORTRAN logical operators are.or. (Logical OR) only one of the conditions must be for the compound condition to be Syntax (Condition_1.or. Condition_2) Example if ( x = = 1.or. x = = y).and. (Logical AND) All of the conditions must be for the compound condition to be Syntax (Condition_1.and. Condition_2) Example if (2 < x.and. x < 7 )

If-else Statement Syntax if (expression) then body1 else body2 end if Semantics if expression is then execute body1 otherwise execute body2 Example body1 expression body2 if (v == 0) then write (*,*) "v is 0 else write (*,*) "v is not 0 End if 9

Example 2 Write a program to determine a student s final grade and indicate whether it is passing or failing. The final grade is calculated as the average of four marks N PRINT PASS START Input M1,M2,M3,M4 GRADE (M1+M2+M3+M4)/4 IS GRADE<50 STOP Y PRINT FAIL Integer :: M1, M2,M3,M4,Grade Read (*,*) M1,M2,M3,M4 Grade = (M1+M2+M3+M4)/4 If (Grade<50) then write (*,*) fail else write (*,*) pass End if end

Multiple Selections: Nested if Nesting: one control statement in another Integer :: g1 Write (*,*) your garde" Read (*,*) g1 if (g1 >=85) then write (*,*) excellent Write a program that calculate the student grade according to his score else if (g1>=75) then write (*,*) very good else if (g1>=65) then write (*,*) good else if (g1>=50) then write (*,*) pass else Print E score>=85 Print VG Print G score>=75 score>=65 write (*,*) fail End if Print P score>=50 Print F end 11

Switch Statement Syntax Select case (expression) case (constant1) statements case (constant2) statements Case default statements End select case a case b... case z Default action(s) case action(s) case b action(s) case z action(s) Semantics The value of the expression is matched against a case value,the statements execute If the value of the expression does not match any of the case values, the statements following the default label execute. If there is no default, the entire switch statement is skipped. 12

Select case Example 1 SELECT CASE (I) CASE(1) Write(*,*) "I=1" CASE(2:6,7,8,9) Write(*,*) "I >=2 and I<=9" CASE(10:) Write(*,*) "I >=10" CASE DEFAULT Write(*,*) "I<1 " END SELECT The result If I = 1, output is I=1 If I = 2, output is I >=2 and I<=9 If I = 5, output is I >=2 and I<=9 If I = 10, output is I >=10 If I = 15, output is I >=10 If I = 0, output is I< 1

Example 2 We want to create a flowchart that prints out the word Honour if the number input is 70, if the number is less than 40 print out the word Fail, otherwise print out the word Pass. START Read I Yes I=70 No No I<40 Yes honor pass Fail END

Select case Example 2 Integer :: A Read (*,*) A SELECT CASE (A) CASE(70) Write(*,*) Honor" CASE(:39) Write(*,*) Fail" CASE DEFAULT Write(*,*) pass " END SELECT The result If A = 70, output is Honor If A = 40, output is pass If A = 30, output is Fail If A = 50, output is pass If A = 75, output is pass

Unconditional GO TO This is the only GOTO in FORTRAN 77 Syntax: GO TO label Unconditional transfer to labeled statement 10 -code- GO TO 30 -code that is bypassed- 30 -code that is target of GOTO- -more code- GO TO 10 Flowchart: GOTO 30 30 Problem: leads to confusing spaghetti code