COMPUTER PROGRAMMING. Control Structures A program is usually not limited to a linear sequence of instructions. During its process it may repeat code.

Slides:



Advertisements
Similar presentations
Flow of Control Chapter 3.
Advertisements

Decision Structures - If / Else If / Else. Decisions Often we need to make decisions based on information that we receive. Often we need to make decisions.
Control Statements. Define the way of flow in which the program statements should take place. Control Statements Implement decisions and repetitions.
1 Conditional Statement. 2 Conditional Statements Allow different sets of instructions to be executed depending on truth or falsity of a logical condition.
Lecture 2 Introduction to C Programming
Introduction to C Programming
1 CS101 Introduction to Computing Lecture 23 Flow Control & Loops (Web Development Lecture 8)
True or false A variable of type char can hold the value 301. ( F )
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
true (any other value but zero) false (zero) expression Statement 2
1 Conditionals In many cases we want our program to make a decision about whether a piece of code should be executed or not, based on the truth of a condition.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
C++ for Engineers and Scientists Third Edition
Introduction to C Programming
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements II.
Quiz 1 Exam 1 Next Week. Nested if Statements if (myGrade >= 80) if (myGrade >= 90) cout
The switch Statement, DecimalFormat, and Introduction to Looping
UNIT II Decision Making And Branching Decision Making And Looping
COMPUTER PROGRAMMING. Data Types “Hello world” program Does it do a useful work? Writing several lines of code. Compiling the program. Executing the program.
Java Programming Constructs 1 MIS 3023 Business Programming Concepts II The University of Tulsa Professor: Akhilesh Bajaj All slides in this presentation.
Department of Computer Science and Engineering, HKUST 1 HKUST Summer Programming Course 2008 C++ Control Statements ~ Selection and Iteration.
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
1 Chapter 4: Selection Structures. In this chapter, you will learn about: – Selection criteria – The if-else statement – Nested if statements – The switch.
Control Structures – Selection Chapter 4 2 Chapter Topics  Control Structures  Relational Operators  Logical (Boolean) Operators  Logical Expressions.
Lecture 4 C Program Control Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
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.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
CPS120: Introduction to Computer Science Decision Making in Programs.
CPS120 Introduction to Computer Science Iteration (Looping)
PHP Conditional Statements Conditional statements in PHP are used to perform different actions based on different conditions. Conditional Statements Very.
CPS120: Introduction to Computer Science Decision Making in Programs.
Control Structures By Shyam Gurram. Control Structure In this chapter we have two different types of structures. Conditional Structure Iterative Control.
Controlling Execution Dong Shao, Nanjing Unviersity.
1 Conditionals In many cases we want our program to make a decision about whether a piece of code should be executed or not, based on the truth of a condition.
Conditional Statement Chapter 8. Conditional Statements Are statements that check an expression then may or may not execute a statement or group of statement.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
COMPUTER PROGRAMMING. Functions’ review What is a function? A function is a group of statements that is executed when it is called from some point of.
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Selection Statements. Introduction Today we learn more about learn to make decisions in Turing ▫Nested if statements, ▫case statements.
CPS120 Introduction to Computer Science Iteration (Looping)
Java Programming Fifth Edition Chapter 5 Making Decisions.
CPS120: Introduction to Computer Science Decision Making in Programs.
Control Flow Statements
CRE Programming Club - Class 4 Robert Eckstein and Robert Heard.
1 CS428 Web Engineering Lecture 13 Flow Control & Loops (JavaScript - III)
CPS120: Introduction to Computer Science Decision Making in Programs.
Programming Language C++ Lecture 3. Control Structures  C++ provides control structures that serve to specify what has to be done to perform our program.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Chapter 7 Control Structures. Java has very flexible three looping mechanisms. You can use one of the following three loops:  while Loop  do...while.
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
 By the end of this section you should be able to: ◦ Differentiate between sequence, selection, and repetition structure. ◦ Differentiae between single,
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
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.
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
PHP Condtions and Loops Prepared by Dr. Maher Abuhamdeh.
Java Programming Fifth Edition
Chapter 4 – C Program Control
Decision Making in C.
Decisions Chapter 4.
Chapter 3 Branching Statements
Chapter 4: Making Decisions.
Chapter 6: Conditional Statements and Loops
JavaScript: Control Statements.
FLOW OF CONTROL.
Control Structures Part 3
Control Structures Part 1
Lecture Notes – Week 2 Lecture-2
Control Statements Paritosh Srivastava.
Presentation transcript:

COMPUTER PROGRAMMING

Control Structures A program is usually not limited to a linear sequence of instructions. During its process it may repeat code or take decisions. For that purpose, C++ provides control structures that serve to specify what has to be done by our program, when and under which circumstances. With the introduction of control structures we are going to have to introduce a new concept: the compoundstatement or block. A block is a group of statements which are separated by semicolons (;) like all C++ statements, but grouped together in a block enclosed in braces: { }:

{ statement1; statement2; statement3; } Conditional structure: if and else The if keyword is used to execute a statement or block only if a condition is fulfilled. Its form is: if (condition) statement

Where condition is the expression that is being evaluated. If this condition is true, statement is executed. If it is false, statement is ignored (not executed) and the program continues right after this conditional structure. For example, the following code fragment prints x is 100 only if the value stored in the x variable is indeed 100: if (x == 100) cout << "x is 100";

If else If we want more than a single statement to be executed in case that the condition is true we can specify a block using braces { }: if (x == 100) { cout << "x is "; cout << x; }

If else conditional statement We can additionally specify what we want to happen if the condition is not fulfilled by using the keyword else. Its form used in conjunction with if is: if (condition) statement1 else statement2 for example: if (x == 100) cout << "x is 100"; else cout << "x is not 100";

If else if prints on the screen “x is 100” if indeed x has a value of 100, but if it has not -and only if not- it prints out “x is not 100”. If else if conditional statement The if else structures can be concatenated with the intention of verifying a range of values. The structure of “ if else if” is as the following: if (condition) statement1 else if (condition) statement2 …. else statement3

if else if The following example shows its use telling if the value currently stored in x is positive, negative or none of them (i.e. zero): if (x > 0) cout << "x is positive"; else if (x < 0) cout << "x is negative"; else cout << "x is 0"; Remember that in case that we want more than a single statement to be executed, we must group them in a block by enclosing them in braces { }.

The selective structure switch : The syntax of the switch statement is a bit peculiar. Its objective is to check several possible constant values for an expression. Something similar to what we did with the concatenation of several if and else if instructions. Its form is the following:

Switch syntax switch (expression) { case constant1: group of statements 1; break; case constant2: group of statements 2; break;... default: default group of statements }

Switch It works in the following way: switch evaluates expression and checks if it is equivalent to constant1, if it is, it executes group of statements 1 until it finds the break statement. When it finds this break statement the program jumps to the end of the switch selective structure. If expression was not equal to constant1 it will be checked against constant2. If it is equal to this, it will execute group of statements 2 until a break keyword is found, and then will jump to the end of the switch selective structure.

Switch Finally, if the value of expression did not match any of the previously specified constants, the program will execute the statements included after the default: label, if it exists (since it is optional). Example: switch (x) { case 1: cout << "x is 1"; break; case 2: cout << "x is 2"; break; default: cout << "value of x unknown"; }

Switch Both of the following code fragments have the same behavior: switch exampleif-else equivalent switch (x) { case 1: cout << "x is 1"; break; case 2: cout << "x is 2"; break; default: cout << "value of x unknown"; } if (x == 1) { cout << "x is 1"; } else if (x == 2) { cout << "x is 2"; } else { cout << "value of x unknown"; }

Switch The switch statement is a bit strange within the C++ language because it uses labels instead of blocks. This forces us to put break statements after the group of statements that we want to be executed for a specific condition. Otherwise the remainder statements -including those corresponding to other labels- will also be executed until the end of the switch selective block or a break statement is reached.

Switch For example, if we did not include a break statement after the first group for case one, the program will not automatically jump to the end of the switch selective block and it would continue executing the rest of statements until it reaches either a break instruction or the end of the switch selective block. This makes unnecessary to include braces { } surrounding the statements for each of the cases. Notice that switch can only be used to compare an expression against constants. Therefore we cannot put variables as labels (for example case n: where n is a variable) or ranges (case (1..3):) because they are not valid C++ constants. If you need to check ranges or values that are not constants, use a concatenation of if and else if statements.