You can select between blocks of statements by using the selection construct IF statement is used as a selection construct Four types of IF constructs.

Slides:



Advertisements
Similar presentations
Exercise (1).
Advertisements

Chapter 7 Introduction to Procedures. So far, all programs written in such way that all subtasks are integrated in one single large program. There is.
Programming in Visual Basic
Need for Arrays Exercise Read the IDs and the grades for all ICS 101 students. Compute and print the average of the students. Print the grades and IDs.
LAB-04 IF Structure I Putu Danu Raharja Information & Computer Science Department CCSE - King Fahd University of Petroleum & Minerals.
James Tam Making Decisions In Pascal In this section of notes you will learn how to have your Pascal programs choose between alternative courses of action.
BACS 287 Programming Logic 3. BACS 287 Iteration Constructs Iteration constructs are used when you want to execute a segment of code several times. In.
Decisions (Conditional Programming) Chapter 5 (Sec. 5.1 & 5.2)
If Statements. COMP104 If / Slide 2 Three Program Structures * Sequence - executable statements which the computer processes in the given order * Choice.
Control Flow IF Statement Visualisation of the IF Statement IF... THEN... ELSE Construct Visualisation of the IF... THEN Construct Visualisation of the.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 1, 2005.
Chapter 4 MATLAB Programming Logical Structures Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Chapter 3 (Tutorial).
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
CHAPTER#3 STRUCTURED PROGRAM DEVELOPMENT IN C++ 1 st semester King Saud University College of Applied studies and Community Service Csc 1101.
Large problems can be divided into smaller sub - problems ♦ each sub - problem can be solved separately in order to reach to the solution of the original.
Announcements 1st homework is due on July 16, next Wednesday, at 19:00 Submit to SUCourse About the homework: Add the following at the end of your code.
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
Selection Structures (if & switch statements) (CS1123)
1 CSC 1401 S1 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University
Conditional Control Flow Constructs. Sequential Control Flow Execution order follows the textual order straight line flow Many simple problems can not.
Decision II. CSCE 1062 Outline  Boolean expressions  switch statement (section 4.8)
More While Loop Examples CS303E: Elements of Computers and Programming.
You can select between blocks of statements by using the selection construct IF statement is used as a selection construct Four types of IF constructs.
Why Files? ♦ the amount of data read and / or produced is huge ♦ repetitive data is needed for more than one program ♦ data may be entered by other people.
Chapter 6: Control Structures Computer Programming Skills Second Term Department of Computer Science Foundation Year Program Umm Alqura.
Programming Fundamentals Lecture 4. In the Previous Lecture Basic structure of C program Variables and Data types Operators ‘cout’ and ‘cin’ for output.
CSE1222: Lecture 6The Ohio State University1. Common Mistakes with Conditions (1)  Consider the following code: int age(26); if (age = 18) { cout
CHAPTER 8 CONTROL STRUCTURES Prepared by: Lec. Ghader R. Kurdi.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 21, 2005 Lecture Number: 10.
Branches and Program Design
1 Compound Assignment C++ has a large set of operators for applying an operation to an object and then storing the result back into the object Examples.
1 09/15/04CS150 Introduction to Computer Science 1 Life is Full of Alternatives Part 2.
BACS 287 Programming Logic 2. BACS 287 Sequence Construct The sequence construct is the default execution mode for the CPU. The instructions are executed.
EXERCISES for ALGORITHMS WRITING
Count and add list of numbers From user input and from file.
1 Chapter 4, Part 1 If Control Construct A mechanism for deciding whether an action should be taken JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S.
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
Chapter 4 Control Structures: Selection We introduced the three fundamental control structures from which all programs are developed: 1. Sequence structures.
1 Agenda If Statement True/False Logical Operators Nested If / Switch Exercises & Misc.
EGR 115 Introduction to Computing for Engineers Branching & Program Design – Part 3 Friday 03 Oct 2014 EGR 115 Introduction to Computing for Engineers.
 Constants A constant is a fixed value of a data type that cannot be changed Integer Constants Whole numbers → Do not have decimal points Examples: 83.
Programming Fundamentals by Dr. Nadia Y. Yousif1 Control Structures (Selections) Topics to cover here: Selection statements in the algorithmic language:
If statement.  It is made up of three main components:  The keyword itself,  an expression that is tested for its truth value,  and a code suite to.
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.
1 Chapter 8: File Processing Part 1. 2 Outline  Why Files?  Opening Files  Reading from Files  Writing to Files  Working with Multiple Files  Closing.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 15, 2004 Lecture Number: 11.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Unsur-unsur Algoritma
Conditional or Decision Logic
Control Structures Combine individual statements into a single logical unit with one entry point and one exit point. Used to regulate the flow of execution.
CMPT 201 if-else statement
Chapter 4 MATLAB Programming
PROGRAM CONTROL STRUCTURE
Announcements General rules about homeworks
Chapter 4: Control Structures
Condition Statements.
SELECTION STATEMENTS (1)
Control Statement Examples
If statement.
Control Structure Senior Lecturer
Building Java Programs
FILE PROCESSING Opening Files Why Files?
Announcements Homework 1 will be assigned this week,
Building Java Programs
Repetition (While Loop) LAB 9
Building Java Programs
REPETITION Why Repetition?
OUTPUT DESIGN PRINT K, expression list K FORMAT (specification list)
Presentation transcript:

You can select between blocks of statements by using the selection construct IF statement is used as a selection construct Four types of IF constructs ■ IF-ELSE Construct ■ IF Construct ■ IF-ELSEIF Construct ■ Simple IF Construct SELECTION CONSTRUCTS

IF-ELSE Construct The general form of the IF- ELSE construct is as follows: IF ( condition ) THEN BLOCK1 ELSE BLOCK2 ENDIF Where - condition is a logical expression - each of block1 and block2 consists of one or more FORTRAN statements - condition is.TRUE. ( execute block1 ) - condition is.FALSE. ( execute block2 ) - the condition should be between parentheses - IF condition THEN in the same line - ELSE should be in a line by itself - ENDIF should be in a line by itself

Example 1: Write a FORTRAN program that reads two integer numbers and prints the maximum. Solution: INTEGER NUM1, NUM2 PRINT*, 'ENTER TWO DIFFERENT INTEGER NUMBERS' READ*, NUM1, NUM2 PRINT*, 'INPUT: ', NUM1, NUM2 IF (NUM1.GT. NUM2) THEN PRINT*, 'MAXIMUM IS ', NUM1 ELSE PRINT*, 'MAXIMUM IS ', NUM2 ENDIF END

Example 2 : Write a FORTRAN program that reads an integer number and finds out if the number is even or odd. The program should print a proper message. Solution: INTEGER K PRINT*, 'ENTER AN INTEGER NUMBER' READ*, K PRINT*, 'INPUT: ', K IF (K/2*2.EQ. K) THEN PRINT*, ' THE NUMBER IS EVEN' ELSE PRINT*, ' THE NUMBER IS ODD' ENDIF END

IF Construct The general form of the IF construct is as follows: IF ( condition ) THEN BLOCK ENDIF

Example : Write a FORTRAN program that reads a grade. If the grade is not zero, the program must add 2 points to the grade. Then, the new grade should be printed. Solution: REAL GRADE PRINT*, 'ENTER A GRADE' READ*, GRADE PRINT*, 'ORIGINAL GRADE IS', GRADE IF (GRADE.GT. 0) THEN GRADE = GRADE PRINT*, 'SCALED GRADE IS ', GRADE ENDIF END

REAL A, B, C READ*, A, B, C IF ( A.LT. B ) THEN PRINT*, A + B IF( B.GT. 4.0 ) THEN PRINT*, B*C ELSE PRINT*, C ENDIF ELSE PRINT*, A*B*C ENDIF END Assume the input for the program is: What is the output of the following program ? Exercises

INTEGER A, B, C READ*, A, B, C IF (A.GT.B) THEN IF (B.LT.C) THEN PRINT*, B ELSE PRINT*, C ENDIF ELSE PRINT*, A ENDIF PRINT*, A, B, C END Assume the input for the program is: What is the output of the following program ?

REAL A, B INTEGER K READ*, A, K, B IF (A.LT. 3.0) THEN PRINT*, A + K IF (B.LT. 2.5) THEN PRINT*, B**K ENDIF ELSE PRINT*, A*B*K ENDIF END Assume the input for the program is:

IF-ELSEIF Construct The general form of the IF- ELSEIF construct is as follows: IF ( condition–1 ) THEN BLOCK1 ELSEIF ( condition–2 ) THEN BLOCK2 ELSEIF ( condition–3 ) THEN BLOCK3... ELSEIF ( condition–n ) THEN BLOCKn ELSE BLOCKn+1 ENDIF

Example : Write a FORTRAN program that reads a student ID and his GPA out of 4.0. The program should print a message according to the following: REAL GPA INTEGER ID CHARACTER*10 STATE READ*, ID, GPA PRINT*, 'INPUT:', ID, GPA IF (GPA.GE. 3.5) THEN STATE = 'EXCELLENT' ELSEIF (GPA.GE. 3.0) THEN STATE = 'VERY GOOD' ELSEIF (GPA.GE. 2.5) THEN STATE = 'GOOD' ELSEIF (GPA.GE. 2.0) THEN STATE = 'FAIR' ELSE STATE = 'POOR' ENDIF PRINT*, ID,' ', STATE END ConditionMessage GPA >= 3.5EXCELLENT 3.5 > GPA >= 3.0VERY GOOD 3.0 > GPA >= 2.5GOOD 2.5 > GPA >= 2.0FAIR GPA < 2.0POOR

Example : Write a FORTRAN program that reads three integer numbers and finds and prints the maximum. Use IF-ELSEIF construct. Solution: INTEGER X1, X2, X3, MAX PRINT*, 'ENTER THREE DIFFERENT INTEGER NUMBERS' READ*, X1, X2, X3 PRINT*, 'THE NUMBERS ARE', X1, X2, X3 IF (X1.GT. X2. AND. X1.GT. X3) THEN MAX = X1 ELSEIF (X2.GT. X3) THEN MAX = X2 ELSE MAX = X3 ENDIF PRINT*, 'THE MAXIMUM OF THE THREE NUMBERS =', MAX END

Simple IF Construct It has the following general form: IF ( condition ) STATEMENT Example : Use simple IF constructs to write a FORTRAN program that reads a student ID and his GPA out of 4.0. The program should print a message according to the following: INTEGER ID REAL GPA CHARACTER*10 STATE READ*, ID, GPA PRINT*, 'INPUT:', ID, GPA IF (GPA.GE. 3.5) STATE = 'EXCELLENT' IF (GPA.GE. 3.0.AND. GPA.LT. 3.5) STATE = 'VERY GOOD' IF (GPA.GE. 2.5.AND. GPA.LT. 3.0) STATE = 'GOOD' IF (GPA.GE. 2.0.AND. GPA.LT. 2.5) STATE = 'FAIR' IF (GPA.LT. 2.0) STATE = 'POOR' PRINT*, ID, ' ', STATE END ConditionMessage GPA >= 3.5EXCELLENT 3.5 > GPA >= 3.0VERY GOOD 3.0 > GPA >= 2.5GOOD 2.5 > GPA >= 2.0FAIR GPA < 2.0POOR

Example : Write a FORTRAN program that reads three Integer numbers and finds and prints the maximum. Use simple IF constructs. Solution: INTEGER X1, X2, X3, MAX PRINT*, 'ENTER THREE DIFFERENT INTEGER NUMBERS' READ*, X1, X2, X3 PRINT*, 'THE NUMBERS ARE', X1, X2, X3 MAX = X1 IF (X2.GT. MAX) MAX = X2 IF (X3.GT. MAX) MAX = X3 PRINT*, 'THE MAXIMUM OF THE THREE NUMBERS IS', MAX END

Exercise What is the output of the following program ? INTEGER N, M N = 15 M = 10 IF (M.GE. N) THEN M = M + 1 IF (N.EQ. M) THEN N = N + 5 ELSEIF (N.GT. 0) THEN N = N + 10 ENDIF M = M - 1 ENDIF M = M - 1 PRINT*, M, N END