Lecture #7 CONTROL STRUCTURE & FLOW CHARTS

Slides:



Advertisements
Similar presentations
Control Statements. Define the way of flow in which the program statements should take place. Control Statements Implement decisions and repetitions.
Advertisements

If Statements & Relational Operators Programming.
1 Lecture 8:Control Structures I (Selection) (cont.) Introduction to Computer Science Spring 2006.
Selection Statements choice of one among several blocks of code Java supports 3 kinds of selection statements: if statement – selects one block or leaves.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
If Statements. COMP104 If / Slide 2 Three Program Structures * Sequence - executable statements which the computer processes in the given order * Choice.
1 Basic control structures Overview l Relational and Logical Operations l Selection structures »if statement »switch statement l Preview:
Tutorial 4 Decision Making with Control Structures and Statements Section A - Decision Making JavaScript Tutorial 4 -Decision Making with Control.
Chapter 4: Control Structures: Selection
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
Outlines Chapter 3 –Chapter 3 – Loops & Revision –Loops while do … while – revision 1.
Computer Science Department Relational Operators And Decisions.
DECISIONS In the Name of Allah The Most Merciful The Most Compassionate DECISIONS
CPS120: Introduction to Computer Science Decision Making in Programs.
Chapter 3 Control Flow Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University.
Selection Structures (if & switch statements) (CS1123)
Basic Of Computer Science
5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,
1 CSC103: Introduction to Computer and Programming Lecture No 7.
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
Lecture 2: Logical Problems with Choices. Problem Solving Before writing a program Have a thorough understanding of the problem Carefully plan an approach.
Programming Fundamentals Lecture 4. In the Previous Lecture Basic structure of C program Variables and Data types Operators ‘cout’ and ‘cin’ for output.
© 2000 Scott S Albert Selection Structures Structured Programming 256 Chapter 4.
PROBLEM SOLVING & ALGORITHMS CHAPTER 5: CONTROL STRUCTURES - SELECTION.
Chapter 3. Outline Relational Operators Loops Decisions Logical Operators Precedence Summary.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 21, 2005 Lecture Number: 10.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
Chapter 05 (Part III) Control Statements: Part II.
Conditional Statement Chapter 8. Conditional Statements Are statements that check an expression then may or may not execute a statement or group of statement.
Decision making statements. Decision making statements are used to skip or to execute a group of statements based on the result of some condition. The.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Selection Statements Selection Switch Conditional.
Conditional Structures UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) ADNAN BABAR MT14028 CR
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Lecture #6 OPERATORS AND ITS TYPES By Shahid Naseem (Lecturer)
Lesson - 5. Introduction While programming, we usually need to decide the path of the program flow according to the parameters and conditions. Actually.
CHAPTER 5 MAKING DECISION Hidayah Elias BFC2042 – Computer Programming.
Chapter 4 Control Structures I. Chapter Objectives Learn about control structures Examine relational and logical operators Explore how to form and evaluate.
If Statements Programming. COMP104 Lecture 7 / Slide 2 Review: Rules for Division l C++ treats integers different than doubles. 100 is an int. l 100.0,
IT CS 200: C ONDITION Lect. Napat Amphaiphan. T HE ABILITY TO CONTROL THE FLOW OF YOUR PROGRAM, LETTING IT MAKE DECISIONS ON WHAT CODE TO EXECUTE 2.
Control statements Mostafa Abdallah
CPS120: Introduction to Computer Science Decision Making in Programs.
Control Flow Statements
CSI 3125, Preliminaries, page 1 Control Statements.
CPS120: Introduction to Computer Science Decision Making in Programs.
Lecture 7 Computer Programming -1-. Conditional Statements 1- if Statement. 2- if ….. else Statement. 3- switch.
Control structures in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
CHAPTER#3 STRUCTURED PROGRAM DEVELOPMENT IN C++ 2 nd semester King Saud University College of Applied studies and Community Service Csc 1101.
Control Statements: Part1  if, if…else, switch 1.
Program Flow Control Addis Ababa Institute of Technology Yared Semu April 2012.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
 By the end of this section you should be able to: ◦ Differentiate between sequence, selection, and repetition structure. ◦ Differentiae between single,
LECTURE # 7 : STRUCTURED PROGRAMMING Selection Statements Tr.Hadeel.
Lecture #8 SWITCH STATEMENT By Shahid Naseem (Lecturer)
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
 Very often when you write code, you want to perform different actions for different decisions. You can use conditional statements in your code to do.
Asif Nawaz University Institute of Information Technology, PMAS-AAUR Lecture 05: Object Oriented Programming:2014 Object-Oriented Programming in C++ Exception.
BY ILTAF MEHDI(MCS,MCSE, CCNA) 1. INSTRUCTOR: ILTAF MEHDI (MCS, MCSE, CCNA, Web Developer) BY ILTAF MEHDI(MCS,MCSE, CCNA) 2 Programming Fundamentals Chapter.
THE DECISIONS CONTROL STRUCTURE! CHAPTER 2. Transfer of control statement: o The statement of computer program are executed one after the other in the.
Branching statements.
Decisions Chapter 4.
Control Structures.
Lecture 2: Logical Problems with Choices
Intro to Programming Week # 3 If-else Statement Lecture # 6
IF if (condition) { Process… }
Control Structures: Selection Statement
There many situations comes in real life when we need to make some decisions and based on these decisions, we decide what should we do next. Similar situations.
Control Structures: Selection Statement
Programming Fundamental
Presentation transcript:

Lecture #7 CONTROL STRUCTURE & FLOW CHARTS PROGRAMMING LANGUAGE Lecture #7 CONTROL STRUCTURE & FLOW CHARTS By Shahid Naseem (Lecturer)

LECTURE OUTLINES

LECTURE OUTLINES IF Statement & Flow Chart IF-ELSE statement & Flow Chart The “NESTED IF” Statement & Flow Chart The “NESTED IF-ELSE” statement & Flow Chart The “SWITCH” Statement The “BREAK” Statement Difference between “nested if-else” & “Switch” Statements. The “goto” Statement. Control Structure (Civil Engineering Department)

THE “IF” STATEMENT Syntax The “IF” statement is used to execute a set of statements after testing a condition. The “IF” statement evaluates a condition , if the given condition is true, the statement following the “if statement” is executed. If the condition is false, the statement following the “if statement” condition is ignored and the control transfers to the next statement. Syntax if(condition) statement-1; statement-2; Control Structure (Civil Engineering Department)

Statements after if structure THE “IF” STATEMENT FALSE TRUE CONDITION Set of statements Statements after if structure Control Structure (Civil Engineering Department)

THE “IF” STATEMENT Write a program to input a number. If the number is divisible by 3 then print the message on the screen that the number is divisible by 3. Use “IF statement”. #include<iostream.h> void main() { int n; cout<<“enter a number?”; cin>>n; if(n%3==0) { cout<<“the number”<<“is divisible by 3”; } Control Structure (Civil Engineering Department)

ASSIGNMENT #2 Write a program to calculate the electricity bill. The rates of electricity per units are as follow. 1. If the units consumed are equal or less than 300, then the cost is Rs. 3/- per unit. 2. If the units consumed are more than 300, then the cost is Rs.3.5/- per unit and a surcharge of 5% of bill is added. Control Structure (Civil Engineering Department)

THE “IF-ELSE STATEMENT The “IF-ELSE statement” is used for making two-way decisions. In this statement, one condition and two blocks of statements are given. Either one of the two blocks of statements is executed after evaluating a condition. The “IF-ELSE” statement tests the given relational condition. If the condition is true then the first block of statements is executed else the other statements if the condition is false. Control Structure (Civil Engineering Department)

THE “IF-ELSE STATEMENT Syntax if (condition) { statement-1; statement-2; -------------- statement-n; } else { statement-1; statement-2; } Control Structure (Civil Engineering Department)

THE “IF-ELSE” STATEMENT FALSE TRUE CONDITION Block-2 Block-1 Statements after if structure Control Structure (Civil Engineering Department)

THE “IF-ELSE STATEMENT Write a program to input a number from the keyboard. Use IF-ELSE statement to find out whether the number is less than or greater than 100. #include<iostream.h> void main () { int n; cout<<“enter an integer value?”; cin>>n; if (n>100) cout<<“number is greater than 100”; else cout<<“number is less than 100”; } Control Structure (Civil Engineering Department)

ASSIGNMENT #3 House Rent is 45% of the basic pay. Write a program to calculate the “NET PAY” of an employee. Input the basic pay and calculate the “NET PAY” and calculate the NET PAY as follows. House Rent is 45% of the basic pay. Medical allowance is 2% of basic if basic is greater than Rs. 5000. It is 5% of basic pay if the pay is less than Rs.5000. Conveyance allowance is Rs.96/- if basic pay is less than Rs. 5000. It is Rs.193/- if the basic pay is more than Rs.5000/- Net Pay is calculated by adding basic pay, medical allowance, conveyance allowance and house rent. Control Structure (Civil Engineering Department)

THE “NESTED-IF” STATEMENT When an “IF statement” is used within another “IF statement”, it is called the “nested if statement”. The “nested if statement” is used for multi-way decision making. Syntax if (condition) { statement-1; } statement-2; Control Structure (Civil Engineering Department)

THE “NESTED-IF” STATEMENT FALSE TRUE CONDITION-1 TRUE FALSE CONDITION-2 Block-2 Block-1 Next Statement Control Structure (Civil Engineering Department)

THE “NESTED-IF” STATEMENT Write a program to input three integer values. Compare the three values to find out if they are equal. Use “Nested if statement” and print the message “ all values are equal” if they are equal. Otherwise print the message “These values are Different”. #include<iostream.h> Void main () { int a,b,c; cout<<“enter first integer?”; cin>>a; Control Structure (Civil Engineering Department)

THE “NESTED-IF” STATEMENT cout<<“enter second integer?”; cin>>b; cout<<“enter third integer?”; cin>>c; if(a==b ) { if(a==c) cout<<“All values are equal”; } else cout<<“These values are different”; Control Structure (Civil Engineering Department)

THE “NESTED-IF-ELSE” STATEMENT When an “if-else” structure is placed in another “if-else “structure, it is called “nested-if-else” structure. It is used for multiple selection. Syntax if (condition-1) statement-1; else if (condition-2) statement-2; else statement-3; Control Structure (Civil Engineering Department)

THE “NESTED-IF-ELSE” STATEMENT TRUE Block-1 CONDITION-1 FALSE TRUE Block-2 CONDITION-2 FALSE Statement after if-else structure Control Structure (Civil Engineering Department)

THE “NESTED-IF-ELSE” STATEMENT Write a program to perform simple arithmetic operation by using “nested –if-else” structure. #include<iostream.h> Void main () { int a,b; char op; cout<<“enter first integer, operator & second integer/n ”; cout<<“press enter key”; cin>>a>>op>>b; Control Structure (Civil Engineering Department)

THE “NESTED-IF-ELSE” STATEMENT If (op==‘+’) cout<<“Addition=“<<(a+b); Elseif (op==‘-’) cout<<“Subtraction=“<<(a-b); Elseif (op==‘*’) cout<<“Multiplication=“<<(a*b); Else if (op=‘/’) cout<<“Division=“<<(a/b); Else if (op=‘%’) cout<<“Remainder=“<<(a%b); Else cout<<“Invalid input”; } Control Structure (Civil Engineering Department)

THE “SWITCH” STATEMENT The “Switch” statement, is used as a substitute of “Nested-if-else statements”. It is used when multiple choices are given and one choice is to be selected. The “nested-if-else” structure becomes complicated in multiple choices. The “Switch Statement” is used in such situations. Only one condition is given in the “switch statement” and multiple choices are given inside the main body. Control Structure (Civil Engineering Department)

THE “SWITCH” STATEMENT Syntax switch (expression) { case const-1: statements; break; case const-2: default: statement; } Control Structure (Civil Engineering Department)

THE “SWITCH” STATEMENT Write a program to input an integer value. Test the integer value if the value is divisible by 2, then print the message “Divisible by 2” otherwise “Not divisible by 2” by using switch statement. #include<iostream.h> Void main () { int n; cout<<“enter any value”<<endl; cin>>n; Switch(n%2) Control Structure (Civil Engineering Department)

THE “SWITCH” STATEMENT { Case o: cout<<“Divisible by 2”<<endl; Break; case 1: cout<<“Not divisible by 2”<<endl; } Cout<<“ok”<<endl; Control Structure (Civil Engineering Department)

THE “BREAK” STATEMENT The “BREAK” statement is used to exit from the body of the switch structure. In the switch statement, the break statement is normally used at the end of statements in each case. It exits the control from the body of switch structure. If it is not used then the statements of other cases that come after the matching case will also be exectued. Control Structure (Civil Engineering Department)

ASSIGNMENT #4 Write a program to perform simple arithmetic operation by using SWITCH STATEMENT Control Structure (Civil Engineering Department)

DIFFERENCE B/W “NESTED-IF-ELSE” AND “SWITCH” STATEMENTS NESTED IF-ELSE STATEMENT SWITCH STATEMENT i. It becomes complicated for multiple selections. It is easy to understand for multiple selections. ii. It uses an independent expression for each case. It uses a single expression for all cases, but each case must have a constant value of integer type or character type. iii. The test condition can be given in a special range of value. If the given condition matches then the statements under it will be executed. Only a single expression is given in the switch statement which returns a single value. The test condition cannot be given in a specified range. It is drawback. Control Structure (Civil Engineering Department)