Program Looping Making Decisions Copyright © 2012 by Yong-Gu Lee

Slides:



Advertisements
Similar presentations
Control Structures.
Advertisements

Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Expressions and Statements. 2 Contents Side effects: expressions and statements Expression notations Expression evaluation orders Conditional statements.
The Preprocessor Underlying C Language Features Copyright © 2012 by Yong-Gu Lee
Selection Statements Selects statements to execute based on the value of an expression The expression is sometimes called the controlling expression Selection.
Computer programming Lecture 3. Lecture 3: Outline Program Looping [Kochan – chap.5] –The for Statement –Relational Operators –Nested for Loops –Increment.
July 13 th.  If/ Else if / Else  Variable Scope  Nested if/else's  Switch statements  Conditional Operator.
True or false A variable of type char can hold the value 301. ( F )
1 Engineering Problem Solving With C++ An Object Based Approach Chapter 3 Control Structures.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
C++ for Engineers and Scientists Third Edition
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
Introduction to Computer Programming in c
 Decision making statements Decision making statements if statement if...else statement Nested if...else statement (if...elseif....else Statement) 
True or False: Boolean Expression Boolean expression are expressions which evaluate to "true" or "false“ Primary operators to combine expressions: && (and),
Programming C for Engineers An exercise is posted on the web site! Due in one week Single submission.
Lecture 10: Reviews. Control Structures All C programs written in term of 3 control structures Sequence structures Programs executed sequentially by default.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational operators – Discover.
Computer programming Lecture 4. Lecture 4: Outline Making Decisions [chap 6 – Kochan] –The if Statement –The if-else Construct –Logical Operators –Boolean.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
1 Conditional statements Dept. of Computer Engineering Faculty of Engineering, Kasetsart University Bangkok, Thailand.
5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
Additional Control Structures. Chapter 9 Topics Switch Statement for Multi-way Branching Do-While Statement for Looping For Statement for Looping Using.
CPS120: Introduction to Computer Science Decision Making in Programs.
1 Chapter 9 Additional Control Structures Dale/Weems.
Chapter 5 Control Structure (Repetition). Objectives In this chapter, you will: Learn about repetition (looping) control structures Explore how to construct.
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.
Chapter 2 Flow of Control. Learning Objectives Boolean Expressions – Building, Evaluating & Precedence Rules Branching Mechanisms – if-else – switch –
Lecture 4 Control Structures MIT – AITI What are Control Structures? Control structures are a way to alter the natural sequence of execution in.
Programming C for Engineers An exercise is posted on the web site! Due in one week Single submission.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Selection Statements Selection Switch Conditional.
Control Statements in C 1.Decision making statements 2.Looping statements 3.Branching statements
CCSA 221 Programming in C CHAPTER 6 MAKING DECISIONS 1.
Quiz 3 is due Friday September 18 th Lab 6 is going to be lab practical hursSept_10/exampleLabFinal/
Sections © Copyright by Pearson Education, Inc. All Rights Reserved.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
Week 4 Program Control Structure
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
Control statements Mostafa Abdallah
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
CPS120: Introduction to Computer Science Decision Making in Programs.
CGS 3460 Program looping n Why we need loop lMake code concise for repetitive processes n When to use loop lRun a block of code repetitively lProcess multiple.
1 Programming in C++ Dale/Weems/Headington Chapter 9 Additional Control Structures (Switch, Do..While, For statements)
COMP Loop Statements Yi Hong May 21, 2015.
Chapter 2: Fundamental Programming Structures in Java Adapted from MIT AITI Slides Control Structures.
Controlling Behavior The if and for Statements. Function Behavior The behavior of a function is determined by the statements within the function. Statements.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Computer C programming Chapter 3. CHAPTER 3 Program Looping –The for Statement –Nested for Loops –for Loop Variants –The while Statement –The do Statement.
Chapter 7 Control Structures. Java has very flexible three looping mechanisms. You can use one of the following three loops:  while Loop  do...while.
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
LECTURE # 7 : STRUCTURED PROGRAMMING Selection Statements Tr.Hadeel.
Chapter 4 – C Program Control
More on the Selection Structure
JavaScript: Control Statements.
- Additional C Statements
IF if (condition) { Process… }
CS1100 Computational Engineering
Chapter 7 Additional Control Structures
Chapter 4 - Program Control
3 Control Statements:.
CSC215 Lecture Flow Control.
CSC215 Lecture Control Flow.
Computer programming Lecture 3.
Week 3 – Program Control Structure
Chapter 4 - Program Control
Decision making and control functions
CSC215 Lecture Control Flow.
Presentation transcript:

Program Looping Making Decisions Copyright © 2012 by Yong-Gu Lee

Looping for ( init_expression; loop_condition; loop_expression ) program statement #import int main (int argc, const char * argv[]) { int i, sum; sum = 0; for ( i = 0; i < 100; i = i + 1 ) sum += i; NSLog sum of %i", sum); return 0; } :02: prog2[3216:707] The sum of i i++ --i i--

== is different from = Relational operators have lower precedence than all arithmetic operators

Execution of the for statement 1. The initial expression is evaluated first. This expression usually sets a variable that is used inside the loop, generally referred to as an index variable, to some initial value (such as 0 or 1). 2. The looping condition is evaluated. If the condition is not satisfied (the expression is false), the loop immediately terminates. Execution continues with the program statement that immediately follows the loop. 3. The program statement that constitutes the body of the loop is executed. 4. The looping expression is evaluated. This expression is generally used to change the value of the index variable, frequently by adding 1 to it or subtracting 1 from it. 5. Return to step 2.

Keyboard input #import int main (int argc, const char * argv[]) { int number; NSLog number"); scanf ("%i", &number); NSLog number was %i\n", number); return 0; } :30: prog2[3816:707] Input number :30: prog2[3816:707] Input number was 3

The while Statement while ( expression ) program statement #import int main (int argc, const char * argv[]) { int count = 1; while ( count <= 5 ) { NSLog count); ++count; } return 0; } [Switching to process 3867 thread 0x0] :35: prog2[3867:707] :35: prog2[3867:707] :35: prog2[3867:707] :35: prog2[3867:707] :35: prog2[3867:707] 5

The do statement do program statement while ( expression ); #import int main (int argc, const char * argv[]) { int i; NSLog your number."); scanf ("%i", &i); do { NSLog i); i -= 1; } while ( i != 0 ); } [Switching to process 3968 thread 0x0] :45: prog2[3968:707] Enter your number :45: prog2[3968:707] :45: prog2[3968:707] :45: prog2[3968:707] :45: prog2[3968:707] :45: prog2[3968:707] 1

The break Statement Sometimes when executing a loop, you’ll want to leave the loop as soon as a certain condition occurs—for instance, maybe you detect an error condition or find the data you’re looking for in a list of data. You can use the break statement for this purpose. Execution of the break statement causes the program to immediately exit from the loop it is executing, whether it’s a for, while, or do loop. Subsequent statements in the loop are skipped and execution of the loop is terminated. Execution continues with whatever statement follows the loop. If a break is executed from within a set of nested loops, only the innermost loop in which the break is executed is terminated. The format of the break statement is simply the keyword break followed by a semicolon, like so: break;

The continue Statement The continue statement is similar to the break statement, except that it doesn’t cause the loop to terminate. At the point that the continue statement is executed, any statements that appear after the continue statement up to the end of the loop are skipped. Execution of the loop otherwise continues as normal.

The if Statement if ( expression ) program statement The if-else Statement if ( expression ) program statement else program statement

Compound Relational Tests #import int main (int argc, const char * argv[]) { int score = 85; if ( score >= 80 && score <= 90 ) is B\n"); } :01: prog2[4094:707] grade is B || is for ‘or’

The else if Construct if (expression 1 ) program statement 1 else if ( expression 2 ) program statement 2 else program statement 3 #import int main (int argc, const char * argv[]) { int number, sign; NSLog type in a number: "); scanf ("%i", &number); if ( number < 0 ) sign = -1; else if ( number == 0 ) sign = 0; else sign = 1; NSLog = %i", sign); // Must be positive 1; } prog2[4155:707] Please type in a number: 5 prog2[4155:707] Sign = 1

The switch Statement switch ( expression ) { case value1: program statement... break; case value2: program statement... break;... case valuen: program statement... break; default: program statement... break; }

#import int main (int argc, const char * argv[]) { int n; printf("Please enter a number: "); scanf("%d", &n); switch (n) { case 1: { is equal to 1!\n"); break; } case 2: { is equal to 2!\n"); break; } case 3: { is equal to 3!\n"); break; } default: { isn't equal to 1, 2, or 3.\n"); break; } Please enter a number: :27: prog2[4298:707] n isn't equal to 1, 2, or 3.

Boolean Variables A couple of built-in features in Objective-C make working with Boolean variables a little easier. One is the special type BOOL, which can be used to declare variables that will contain either a true or a false value. The other is the built-in values YES and NO. Using these predefined values in your programs can make them easier to write and read. #import int main (int argc, const char * argv[]) { int p, d; BOOL isPrime; for ( p = 2; p <= 50; ++p ) { isPrime = YES; for ( d = 2; d < p; ++d ) if ( p % d == 0 ) isPrime = NO; if ( isPrime == YES ) NSLog ", p); } [Switching to process 4354 thread 0x0] :34: prog2[4354:707] :34: prog2[4354:707] :34: prog2[4354:707] :34: prog2[4354:707] :34: prog2[4354:707] :34: prog2[4354:707] :34: prog2[4354:707] :34: prog2[4354:707] :34: prog2[4354:707] :34: prog2[4354:707] :34: prog2[4354:707] :34: prog2[4354:707] :34: prog2[4354:707] :34: prog2[4354:707] :34: prog2[4354:707] 47

The Conditional Operator condition ? expression1 : expression2 s = ( x < 0 ) ? -1 : x * x;