INC 161 , CPE 100 Computer Programming

Slides:



Advertisements
Similar presentations
For loops For loops are controlled by a counter variable. for( c =init_value;c
Advertisements

Dr. Yang, Qingxiong (with slides borrowed from Dr. Yuen, Joe) LT4: Control Flow - Loop CS2311 Computer Programming.
Selection Statements Selects statements to execute based on the value of an expression The expression is sometimes called the controlling expression Selection.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 4 – C Program Control Outline 4.1Introduction.
1 Lecture 11:Control Structures II (Repetition) (cont.) Introduction to Computer Science Spring 2006.
C Lecture Notes 1 Program Control (Cont...). C Lecture Notes 2 4.8The do / while Repetition Structure The do / while repetition structure –Similar to.
1 Chapter 3 Flow of Control. 2 Outline  How to specify conditions?  Relational, Equality and Logical Operators  Statements  Statements: compound statement.
לולאות 02 יולי יולי יולי 1502 יולי יולי יולי 1502 יולי יולי יולי 15 1 Department of Computer Science-BGU.
1 Agenda - Loops while for for & while Nested Loops do-while Misc. & Questions.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved The switch Multiple-Selection Statement switch.
ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 2: Control Flow.
 Decision making statements Decision making statements if statement if...else statement Nested if...else statement (if...elseif....else Statement) 
Algorithms and Computing Lecture 3 Control Statements By Dr. M. Tahir Khaleeq.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
Lecture 4 C Program Control Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Chapter 3 Control Flow Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Flow Control (Switch, do-while, break) Outline 4.7The.
Exam 2 – Nov 18th Room ACIV 008. Project 2 Update  Your code needs to use loops to create the multiplication table. Hint: use nested for loop (Lecture.
Real World Applications: Generating Prime Numbers  Problem Write a program that prints all positive prime integers less than or equal to n. A positive.
Incremental operators Used as a short-hand i++ or ++i  ==  i = i + 1 i-- or --i  ==  i = i – 1 i += a  ==  i = i + a i -= a  ==  i = i - a i *=
1 Flowchart notation and loops Implementation of loops in C –while loops –do-while loops –for loops Auxiliary Statements used inside the loops –break –continue.
Chapter 8 Iteration Dept of Computer Engineering Khon Kaen University.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
Loops cause a section of a program to be repeated a certain number of times. The repetition continues while a condition remains true. When a condition.
CS 161 Introduction to Programming and Problem Solving Chapter 18 Control Flow Through C++ Program Herbert G. Mayer, PSU Status 10/8/2014 Initial content.
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
For Loop Lecture No 8. Definition In computer science a for loop is a programming language statement which allows code to be repeatedly executed. A for.
CS 161 Introduction to Programming and Problem Solving Chapter 17 Nested Loops Herbert G. Mayer, PSU Status 9/8/2014 Initial content copied verbatim from.
Engineering Computing I Chapter 3 Control Flow. Chapter 3 - Control Flow The control-flow of a language specify the order in which computations are performed.
CONTENTS Loop Statements Parts of a loop Types of Loops Nested Loops
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
The following statements are for y = -1; if ( x ) if ( x>0 ) y = 1; else y = 0; A. y= -1 x0 B. y= 0 x0 C. y= 1 x
 Real numbers representation - Floating Point Notation  First C Program  Variables Declaration  Data Types in C ◦ char, short, int, long, float, double,
Control Structures (Repetition structure) Jump Statements
Chapter 4 – C Program Control
CSE 220 – C Programming Loops.
Flow of Control True and False in C Conditional Execution Iteration
Lecture 7: Repeating a Known Number of Times
Chapter 4 - Program Control
The nested repetition control structures & Continue Statements
2008/10/22: Lecture 12 CMSC 104, Section 0101 John Y. Park
CSI 121 Structure Programming Language Lecture 10: Iteration (Part 1)
Control Structures Lecture 7.
Looping.
- Additional C Statements
INC 161 , CPE 100 Computer Programming
2008/10/22: Lecture 12 CMSC 104, Section 0101 John Y. Park
Chapter 4 - Program Control
Exam 1 Date: Feb. 2nd, 2015 during class time (50 minutes) Coverage
Loops in C.
INC 161 , CPE 100 Computer Programming
Program Control Topics While loop For loop Switch statement
UMBC CMSC 104 – Section 01, Fall 2016
REPETITION STATEMENTS
Computer programming Lecture 3.
ECE 103 Engineering Programming Chapter 19 Nested Loops
ECE 103 Engineering Programming Chapter 20 Change in Flow of Control
More Loops Topics Counter-Controlled (Definite) Repetition
Dale Roberts, Lecturer IUPUI
Chapter 4 - Program Control
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Counter-Controlled (Definite) Repetition
CSC215 Lecture Control Flow.
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Counter-Controlled (Definite) Repetition
Programming Language  C Control Flow
Control Structure គោលបំណងនៃមេរៀន អ្វីជា Control structure ?
Presentation transcript:

INC 161 , CPE 100 Computer Programming Lecture 5 Flow Control (Cont.)

the controlling expression. For Loop The syntax of a for statement is as follows: for(expression1; expression2; expression3) statement The expression expression1 is evaluated as a void expression before the first evaluation of the controlling expression. The expression expression2 is the controlling expression that is evaluated before each execution of the loop body. The expression expression3 is evaluated as a void expression after each execution of the loop body. Both expression1 and expression3 can be omitted. An omitted expression2 is replaced by a nonzero constant. The for-loop is semantically equivalent to the following while-loop: expression1; while(expression2) { expression3; }

Flowchart of a for Loop 2 semi-colons inside The syntax of a for loop is as follows: for(expression1; expression2; expression3) statement

0 1 2 3 4 Output: for-loop is suitable for counter control Example: int i; for(i = 0; i < 5; i++) { printf(“%d ”, i); } Example: Output: 0 1 2 3 4 for-loop is suitable for counter control

Calculating a factorial 5!. The factorial n! is defined as n*(n-1)! Example: Calculating a factorial 5!. The factorial n! is defined as n*(n-1)! /* File: forloop.c */ #include <stdio.h> main() { unsigned int i, f, n; printf(“Please input a number\n”); scanf(“%d”, &n); for(i=1, f=1; i<=n; i++) { f = f*i; } printf(“factorial %d! = %d\n", n, f); Execution and Output: > forloop.c 5 factorial 5! = 120

Find factorial n= 4 f = 1 i = 2 f = 2 i = 3 f = 6 i = 4 f = 24 i = 5

Jump Statements (break, continue) Break Statements The break statement provides an early exit from the for, while, do-while, and for each loops as well as switch statement. A break causes the innermost enclosing loop or switch to be exited immediately. Example: int i; for(i=0; i<5; i++) { if(i == 3) { break; } printf("%d", i); Output: 0 1 2

int i; Continue Statements and do-while loop to begin. Example: The continue statement causes the next iteration of the enclosing for, while, and do-while loop to begin. A continue statement should only appear in a loop body. Example: int i; for(i=0; i<5; i++) { if(i == 3) { continue; } printf("%d", i); Output: 0 1 2 4

switch..case The switch command is used to decide the choices that the program will jump to. switch(p) { case 1: commands // if p==1 case 2: commands // if p==2 case 3: commands // if p==3 }

Example #include <stdio.h> main() { int p; printf("Please enter the choice: "); scanf("%d",&p); switch(p) { case 1: printf("Case 1\n"); case 2: printf("Case 2\n"); case 3: printf("Case 3\n"); }

Normally, we will use the break command to run only the case we want. #include <stdio.h> main() { int p; printf("Please enter the choice: "); scanf("%d",&p); switch(p) { case 1: printf("Case 1\n"); break; case 2: printf("Case 2\n"); case 3: printf("Case 3\n"); }

Default label is used when we want t handle other cases. #include <stdio.h> main() { int p; printf("Please enter the choice: "); scanf("%d",&p); switch(p) { case 1: printf("Case 1\n"); break; case 2: printf("Case 2\n"); default: printf("Case 3\n"); }

switch-case is the same as else-if The syntax for an else-if statement is as follows: if(expression1) statement1 else if(expression2) statement2 else if(expression3) statement3 else statement4

Nested Loop Nested loop = loop inside loop Program flow The inner loops must be finished before the outer loop resumes iteration.

Write a program to print a multiplication table. 1 2 3 4 5 6 7 8 9 10 ------------------------------------------ 1| 1 2 3 4 5 6 7 8 9 10 2| 2 4 6 8 10 12 14 16 18 20 3| 3 6 9 12 15 18 21 24 27 30 4| 4 8 12 16 20 24 28 32 36 40 5| 5 10 15 20 25 30 35 40 45 50 6| 6 12 18 24 30 36 42 48 54 60 7| 7 14 21 28 35 42 49 56 63 70 8| 8 16 24 32 40 48 56 64 72 80 9| 9 18 27 36 45 54 63 72 81 90 10| 10 20 30 40 50 60 70 80 90 100

A program to print a multiplication table. Example: A program to print a multiplication table. #include <stdio.h> main() { int i, j; printf(" 1 2 3 4 5 6 7 8 9 10\n"); printf(" ------------------------------------------\n"); for(i=1; i<= 10; i++) { /* outer loop */ printf("%4d|", i); for(j=1; j<= 10; j++) { /* inner loop */ printf("%4d", i*j); } printf("\n");

Small loop