CHAPTER 8 CONTROL STRUCTURES Prepared by: Lec. Ghader R. Kurdi.

Slides:



Advertisements
Similar presentations
If Statements & Relational Operators Programming.
Advertisements

Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
Week 4 Selections This week shows how to use selection statements for more flexible programs. It also describes the various integral types that are available.
Switch Switch case statements are a substitute for long if statements that compare a variable to several "integral" values ("integral" values are simply.
1 10/20/08CS150 Introduction to Computer Science 1 do/while and Nested Loops Section 5.5 & 5.11.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
If Statements. COMP104 If / Slide 2 Three Program Structures * Sequence - executable statements which the computer processes in the given order * Choice.
The If/Else Statement, Boolean Flags, and Menus Page 180
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.
Programming in C++ Lecture Notes 2 – Choice Statements Andreas Savva.
CHAPTER 5 CONTROL STRUCTURES II (Repetition). In this chapter, you will:  Learn about repetition (looping) control structures  Explore how to construct.
Outlines Chapter 3 –Chapter 3 – Loops & Revision –Loops while do … while – revision 1.
Selection Statements in C++ If Statement in C++ Semantics: These statements have the same meaning as in the algorithmic language. 2- Two way selection:
Lecture 5 Selection Control Structures Selection Control Structures Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor
Computer Science Department Relational Operators And Decisions.
More on Input Output Input Stream : A sequence of characters from an input device (like the keyboard) to the computer (the program running). Output Stream.
Selection Structures (if & switch statements) (CS1123)
Chapter 4 Selection Structures: Making Decisions.
Basic Of Computer Science
CS102 Introduction to Computer Programming Chapter 4 Making Decisions.
Quiz Answers 1. Show the output from the following code fragment: int a = 5, b = 2, c = 3; cout
CONTROLLING PROGRAM FLOW
Chapter 6: Control Structures Computer Programming Skills Second Term Department of Computer Science Foundation Year Program Umm Alqura.
Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP For loop is a counter controlled loop. For loop is a pretest loop. Used when number.
PROBLEM SOLVING & ALGORITHMS CHAPTER 5: CONTROL STRUCTURES - SELECTION.
Previously Repetition Structures While, Do-While, For.
Chapter 4 Loops Write code that prints out the numbers Very often, we want to repeat a (group of) statement(s). In C++, we have 3 major ways of.
Chapter 7 Additional Control Structures. 2 2 void GetYesOrNo (/* out */ char& response) // Inputs a character from the user // Postcondition: response.
1 09/20/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 21, 2005 Lecture Number: 10.
PROGRAM FLOW CHAPTER3 PART1. Objectives By the end of this section you should be able to: Differentiate between sequence, selection, and repetition structure.
Conditional Structures UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) ADNAN BABAR MT14028 CR
Lecture 2 Control Structure. Relational Operators -- From the previous lecture Relational Operator Meaning == is equal to < is less than > is greater.
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.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
CSC1201: Programming Language 2 Lecture 1 Level 2 Course Nouf Aljaffan (C) CSC 1201 Course at KSU1.
Chapter 6. else-if & switch Copyright © 2012 Pearson Education, Inc.
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.
Control Structures RepetitionorIterationorLooping Part I.
Lecture 4 Function example. Example1 int max (int a, int b) { int c; if (a > b) c = a; else c = b; return (c); } void main ( ) {int x, y; cin>>x>>y; cout.
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,
Programming Fundamentals by Dr. Nadia Y. Yousif1 Control Structures (Selections) Topics to cover here: Selection statements in the algorithmic language:
Lecture 7 Computer Programming -1-. Conditional Statements 1- if Statement. 2- if ….. else Statement. 3- switch.
Chapter 3. Control Structure C++ Programing. Conditional structure C++ programming language provides following types of decision making statements. Click.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 15, 2004 Lecture Number: 11.
LECTURE # 7 : STRUCTURED PROGRAMMING Selection Statements Tr.Hadeel.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 23, 2005 Lecture Number: 11.
Infinite for Loop If you omit the test condition, the value is assumed to be TRUE so the loop will continue indefinitely unless you provide some other.
Basic concepts of C++ Presented by Prof. Satyajit De
Chapter 3 Selection Statements
Decisions Chapter 4.
A mechanism for deciding whether an action should be taken
Chapter 2.1 Control Structures (Selection)
Chapter 4: Control Structures
Intro to Programming Week # 4 Switch Statement Lecture # 7
SELECTION STATEMENTS (1)
TOPIC 4: REPETITION CONTROL STRUCTURE
COMS 261 Computer Science I
Compound Assignment Operators in C++
CSE 1020:Control Structure
Chapter#3 Structured Program Development in C++
Self study.
Branching statements Kingdom of Saudi Arabia
COMS 261 Computer Science I
C++ Basics CSci 107. A C++ program //include headers; these are modules that include functions that you may use in your //program; we will almost always.
Selection Control Structure
Presentation transcript:

CHAPTER 8 CONTROL STRUCTURES Prepared by: Lec. Ghader R. Kurdi

Conditional Constructs They provide the ability to control whether a list of statements is executed. Besides, it is a mechanism for deciding whether an action should be taken. It comes in one of the two types: If statement (if statement, if-else statement and Nested if-else structure) Switch statement.

The Basic If Structure

Example

#include void main() { int value, temp; cout<<"Enter an integer number: "; cin>>value; temp=value; If (value<0) value = - value ; cout<<"the absolute value of "<<temp<<“ is "<<value; } Outputs:

Example: Sorting Two Numbers #include void main() { cout << "Enter two integers: "; int value1, value2; cin >> value1 >> value2; if (value1 > value2) { int rememberValue1 = value1; value1 = value2; value2 = rememberValue1; } cout << "The input in sorted order: “; cout << value1 << " " << value2 << endl; } Outputs:

Example #include void main() { cout << "Enter two integers: "; int num1, num2, largest; cin>>num1>>num2; if (num1>num2) largest = num1; if (num2>num1) largest = num2; cout<<"\nThe larger is: "<<largest; } Outputs:

Example #include void main() { int num1, num2; cin>>num1>>num2; if (num1>num2) cout<<"\nThe largest number is: "<<num1; if (num2>num1) cout<<"\nThe largeest number is: "<<num2; } Outputs:

Example #include void main() { int num; cout<<"Enter an integer number: "; cin>>num; if(num>0) cout<<num<<" is positive”<< endl; if(num<0) cout<<num<<" is negative"<< endl; if(num==0) cout<<num<<" is zero"<< endl; } Outputs:

The If-Else Structure

Example

#include void main() { cout << "Enter two integers: "; int value1, value2, max; cin >> value1 >> value2; if (value1 < value2) { max = value2; } else { max = value1; } cout << "Maximum of inputs is: " << max << endl; } Outputs:

Selection It is often the case that depending upon the value of an expression we want to perform a particular action. There are two major ways of accomplishing this choice: –Nested if structure if-else statements "glued" together –Switch statement An advanced construct

Nested if syntax if(boolean_expression1) { // Executes when the boolean expression 1 is true } else if( boolean_expression2) { // Executes when the boolean expression 2 is true } else { // executes when the none of the above condition is true. }

Example #include void main() { int num ; cout<<"Enter an integer number: "; cin>> num; if (num < 0 ) cout << num << " is negative" << endl; else if (num > 0 ) cout << num << " is positive" << endl; else cout << num << " is zero" << endl; } Outputs:

Example #include void main() { int grade; cin>>grade; if (grade>=90) cout<<"A"; elseif (grade>=80) cout<<"B"; elseif (grade>=70) cout<<"C"; elseif (grade>=60) cout<<"D"; else cout<<"F“; } Outputs:

Switch statement syntax switch ( variable ) { case value1 : statement;// Process value1 break; case value2 : statement;// Process for value2 break; default : statement;// Process for all other cases }

Example #include void main() { char grade; cin>>grade; switch (grade) { case 'A' : cout << "Excellent!" << endl; break; case 'B' : cout << "Well done" << endl; break; case 'C' : cout << “Not bad" << endl; break; case 'D' : cout << "You passed" << endl; break; case 'F' : cout << "Better try again" << endl; break; default : cout << "Invalid grade" << endl; } Outputs :

Exercises Write a program that asks the user to type an integer and writes "YOU WIN" if the value is between 56 and 78 (both included). In the other case it writes "YOU LOSE". Write a C++ program that receives the current temperature as input. If the temperature is 35 degrees or more, output a message telling the user to “go swimming”, otherwise, if the temperature is 25 degrees or more, output a message to “go running”, otherwise “stay inside”. Read an integer value. Assume it is the number of a month of the year; print out the name of that month.