Chapter 4. Flow of Control

Size: px
Start display at page:

Transcription

1 Chapter 4. Flow of Control Byoung-Tak Zhang TA: Hanock Kwak Biointelligence Laboratory School of Computer Science and Engineering Seoul National Univertisy

2 Sequential flow of control Flow of Control Statement in a program are normally executed one after another. Often it is desirable to alter the sequential flow of control to provide for a choice of action if, if-else, switch or a repetition of action while, for, do-while

3 True True and False Any non-zero integers are treated as true. False Zero means false. Logical and Relational Operations Results of the logical and relational operations are one(true) or zero(false) printf( %d, 2 == 2) ; // equal, 1 printf( %d,! 0) ; // negation, 1

4 Relational Operator expr < expr expr > expr expr <= expr expr >= expr <Examples> a < 3 a > b -1.3 >= (2.0 * x + 3.3) a < b If a is less than b, then 1 (true). If a is not less than b, then 0 (false).

5 Relational Operator

6 Relational Operator expr == expr expr!= expr a!= b If a is not equal to b, then 1 (true). If a is equal to b, then 0 (false). <Examples> c == A k!= z == x + y/4

7 Relational Operator!! A common programming error if (a = 1) if (a == 1)

8 Negation! expr negation If expr has value zero,! expr is 1 (true). If expr has non-zero value,! expr is 0 (false). <Examples>! a! (x + 7.7)! (a < b c < d)

9 Negation

10 Logical Operator expr expr (logical or) expr && expr (logical and) a b If a or b is 1, then 1 (true). If a and b are both 0, then 0 (false). a && b If a and b are both 1, then 1 (true). If a or b is 0, then 0 (false) <Examples> a && b a b 10 < a && a < 100

11 Logical Operator && has higher precedence than. Both of && and are of lower precedence than all unary, arithmetic, equality, and relational operators. Short-circuit Evaluation In evaluating the expr.s that are the operands of && and, the evaluation process stops as soon as the outcome true or false is known. expr1 && expr2, if expr1 has value zero, then expr2 is not evaluated expr1 expr2, if expr1 has nonzero value, then expr2 is not evaluated

12 Logical Operator

13 Statement Statement Statement is a smallest standalone element of a C source code. Expression Statement an expression followed by ; Empty statement written as a single semicolon useful where a statement is needed syntactically a = b; /* assignment statement */ a + b + c; /* legal, but no useful work gets done */ ; /* empty statement */ printf( %d\n, a); /* a function call */

14 Compound Statement Statement a series of declarations and statements surrounded by braces block for grouping statements into an executable unit It is itself a statement, thus it can be placed wherever a statement is placed. { } a = 1; { /* nested */ b = 2; c = 3; }

15 if and if-else Statement if ( expr ) statement If expr is nonzero, then statement is executed; otherwise, statement is skipped and control passes to the next statement. if (j < k) { min = j; printf( j is smaller than k\n ); }

16 if and if-else Statement if ( expr ) statement1 else statement2 if (c >= a && c <= z ) ++lc_cnt; else { ++other_cnt; printf( %c is not a lowercase letter\n, c); } If expr is nonzero, then statement1 is executed and then skip statement2; otherwise, statement1 is skipped and then statement2 is executed.

17 if and if-else Statement if (a ==1) if ( b == 2) /* if statement is itself a statement */ printf( ***\n ); dangling else problem (An else attaches to the nearest if.) if (a ==1) if ( b == 2) printf( ***\n ); else printf( ###\n ); if (a ==1) if ( b == 2) printf( ***\n ); else printf( ###\n );

18 Complex Example if and if-else Statement if (c == ) ++blank_cnt; else if (c >= 0 && c <= 9 ) ++digit_cnt; else if (c >= a && c <= z c >= a && c <= z ) ++letter_cnt; else if (c == \n ) ++nl_cnt; else ++other_cnt;

19 while Statement while ( expr ) statement First expr is evaluated. If it is nonzero, then statement is executed and control is passed back to expr. This repetition continues until expr is zero. // print i and increment it until it is less than 10 while ( i < 10 ) printf( %d, i++);

20 for ( expr1 ; expr2 ; expr3 ) statement for Statement First, expr1(initialization) is evaluated. expr1 ; while ( expr2 ) { } statement expr3; expr2 is evaluated. If it is nonzero, then statement is executed, expr3 is evaluated, and control is passed back to expr2. expr2 is a logical expression controlling the iteration. This process continues until expr2 is zero.

21 for Statement // print seven factorial value int i, factorial = 1; for ( i = 2; i <= 6; i++ ) factorial = factorial * i; printf( 7! is %d, factorial);

22 for Statement // print all cases of rolling two dices int i, j; for ( i = 1; i <= 6; i++ ) { for ( j = 1; j <= 6; j++) printf( (%d, %d), i, j); printf( \n ); }

23 Comma Operator expr1, expr2 expr1 is evaluated, and then expr2. for (sum = 0, i =1; i <= 10; ++i) sum += i;

24 do-while Statement do statement while ( expr ) First statement is executed and expr is evaluated. If the value of expr is nonzero, then control is passed back to statement. When expr is zero, it finishes the control.

25 do-while Statement // try to get positive integer do { printf( Input a positive integer: ); scanf( %d, &n); if (n < 0) printf( \nerror: Do it again!\n\n ); } while (n < 0);

26 break break; causes an exit from the inner most enclosing loop or switch statement while (1) { scanf( %lf, &x); if (x < 0.0) break; /* exit loop if x is negative */ printf( %f\n, sqrt(x)); } /* break jumps to here */

27 continue continue; causes the current iteration of a loop to stop and causes the next iteration of the loop to begin immediately /* print all even integer less than 100 */ for ( i = 0; i < 100; i++) { if ( i % 2 == 1) // if i is odd continue; // go to next iteration printf( %d\n, i); }

28 switch Statement A multiway conditional statement generalizing the if-else statement switch (c) { case a : printf( a\n ); break; case b : case B : printf( b or B\n ); break; default: printf( something else ); } (1) Evaluate the switch expression. (2) Go to the case label having a constant value that matches the value of the expression in (1), or, if a match is not found, go to the default label, or, if there is no default label, terminate the switch. (3) Terminate the switch when a break statement is encountered, or terminate the switch by falling off the end.

29 expr1? expr2 : expr3; Conditional Operator expr1 is evaluated. If expr1 is nonzero(true), then expr2 is evaluated, and that is the value of the conditional expression as a whole. If expr1 is zero(false), then expr3 is evaluated, and that is the value of the conditional expression as a whole. // x is 100 if y > 10, otherwise -5. x = (y > 10)? 100 : -5 ;

Chapter 2. Lexical Elements & Operators

Chapter 2. Lexical Elements & Operators Byoung-Tak Zhang TA: Hanock Kwak Biointelligence Laboratory School of Computer Science and Engineering Seoul National Univertisy http://bi.snu.ac.kr The C System

There are algorithms, however, that need to execute statements in some other kind of ordering depending on certain conditions.

Introduction In the programs that we have dealt with so far, all statements inside the main function were executed in sequence as they appeared, one after the other. This type of sequencing is adequate

Problem Solving and 'C' Programming

Problem Solving and 'C' Programming Targeted at: Entry Level Trainees Session 05: Selection and Control Structures 2007, Cognizant Technology Solutions. All Rights Reserved. The information contained herein

Flow Control. CSC215 Lecture

Flow Control CSC215 Lecture Outline Blocks and compound statements Conditional statements if - statement if-else - statement switch - statement? : opertator Nested conditional statements Repetitive statements

3 The L oop Control Structure

3 The L oop Control Structure Loops The while Loop Tips and Traps More Operators The for Loop Nesting of Loops Multiple Initialisations in the for Loop The Odd Loop The break Statement The continue Statement

n Group of statements that are executed repeatedly while some condition remains true

Looping 1 Loops n Group of statements that are executed repeatedly while some condition remains true n Each execution of the group of statements is called an iteration of the loop 2 Example counter 1,

Statements. Control Flow Statements. Relational Operators. Logical Expressions. Relational Operators. Relational Operators 1/30/14

Statements Control Flow Statements Based on slides from K. N. King Bryn Mawr College CS246 Programming Paradigm So far, we ve used return statements and expression statements. Most of C s remaining statements

Introduction. C provides two styles of flow control:

Introduction C provides two styles of flow control: Branching Looping Branching is deciding what actions to take and looping is deciding how many times to take a certain action. Branching constructs: if

INTRODUCTION TO C++ PROGRAM CONTROL. Dept. of Electronic Engineering, NCHU. Original slides are from

INTRODUCTION TO C++ PROGRAM CONTROL Original slides are from http://sites.google.com/site/progntut/ Dept. of Electronic Engineering, NCHU Outline 2 Repetition Statement for while do.. while break and continue

A Look Back at Arithmetic Operators: the Increment and Decrement

A Look Back at Arithmetic Operators: the Increment and Decrement Spring Semester 2016 Programming and Data Structure 27 Increment (++) and Decrement (--) Both of these are unary operators; they operate

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

Unit 1 Programming Language and Overview of C 1. State whether the following statements are true or false. a. Every line in a C program should end with a semicolon. b. In C language lowercase letters are

Flow of Control. Flow of control The order in which statements are executed. Transfer of control

1 Programming in C Flow of Control Flow of control The order in which statements are executed Transfer of control When the next statement executed is not the next one in sequence 2 Flow of Control Control

V2 2/4/ Ch Programming in C. Flow of Control. Flow of Control. Flow of control The order in which statements are executed

Programming in C 1 Flow of Control Flow of control The order in which statements are executed Transfer of control When the next statement executed is not the next one in sequence 2 Flow of Control Control

YOLOP Language Reference Manual

YOLOP Language Reference Manual Sasha McIntosh, Jonathan Liu & Lisa Li sam2270, jl3516 and ll2768 1. Introduction YOLOP (Your Octothorpean Language for Optical Processing) is an image manipulation language

Lecture 10. Daily Puzzle

Lecture 10 Daily Puzzle Imagine there is a ditch, 10 feet wide, which is far too wide to jump. Using only eight narrow planks, each no more than 9 feet long, construct a bridge across the ditch. Daily

Programming for Engineers Iteration

Programming for Engineers Iteration ICEN 200 Spring 2018 Prof. Dola Saha 1 Data type conversions Grade average example,-./0 class average = 23450-67 893/0298 Grade and number of students can be integers

Assoc. Prof. Dr. Marenglen Biba (C) 2010 Pearson Education, Inc. All for repetition statement do while repetition statement switch multiple-selection statement break statement continue statement Logical

Chapter 6. Loops. Iteration Statements. C s iteration statements are used to set up loops.

Chapter 6 Loops 1 Iteration Statements C s iteration statements are used to set up loops. A loop is a statement whose job is to repeatedly execute some other statement (the loop body). In C, every loop

Chapter 3. Fundamental Data Types

Chapter 3. Fundamental Data Types Byoung-Tak Zhang TA: Hanock Kwak Biointelligence Laboratory School of Computer Science and Engineering Seoul National Univertisy http://bi.snu.ac.kr Variable Declaration

Programming for Electrical and Computer Engineers. Loops

Programming for Electrical and Computer Engineers Loops Dr. D. J. Jackson Lecture 6-1 Iteration Statements C s iteration statements are used to set up loops. A loop is a statement whose job is to repeatedly

Decision Making -Branching. Class Incharge: S. Sasirekha

Decision Making -Branching Class Incharge: S. Sasirekha Branching The C language programs presented until now follows a sequential form of execution of statements. Many times it is required to alter the

ECE 2400 Computer Systems Programming Fall 2018 Topic 1: Introduction to C

ECE 2400 Computer Systems Programming Fall 2018 Topic 1: Introduction to C School of Electrical and Computer Engineering Cornell University revision: 2018-09-03-15-59 1 Statements, Syntax, Semantics, State

Chapter 4: Expressions Chapter 4 Expressions 1 Chapter 4: Expressions Operators Expressions are built from variables, constants, and operators. C has a rich collection of operators, including arithmetic

CS1100 Introduction to Programming

Decisions with Variables CS1100 Introduction to Programming Selection Statements Madhu Mutyam Department of Computer Science and Engineering Indian Institute of Technology Madras Course Material SD, SB,

Chapter 5: Control Structures

Chapter 5: Control Structures In this chapter you will learn about: Sequential structure Selection structure if if else switch Repetition Structure while do while for Continue and break statements S1 2017/18

DECISION CONTROL AND LOOPING STATEMENTS

DECISION CONTROL AND LOOPING STATEMENTS DECISION CONTROL STATEMENTS Decision control statements are used to alter the flow of a sequence of instructions. These statements help to jump from one part of

AN OVERVIEW OF C, PART 3. CSE 130: Introduction to Programming in C Stony Brook University

AN OVERVIEW OF C, PART 3 CSE 130: Introduction to Programming in C Stony Brook University FANCIER OUTPUT FORMATTING Recall that you can insert a text field width value into a printf() format specifier:

COP 2000 Introduction to Computer Programming Mid-Term Exam Review

he exam format will be different from the online quizzes. It will be written on the test paper with questions similar to those shown on the following pages. he exam will be closed book, but students can

COMP 208 Computers in Engineering

COMP 208 Computers in Engineering Lecture 14 Jun Wang School of Computer Science McGill University Fall 2007 COMP 208 - Lecture 14 1 Review: basics of C C is case sensitive 2 types of comments: /* */,

Dept. of CSE, IIT KGP

Control Flow: Looping CS10001: Programming & Data Structures Pallab Dasgupta Professor, Dept. of Computer Sc. & Engg., Indian Institute of Technology Kharagpur Types of Repeated Execution Loop: Group of

LESSON 6 FLOW OF CONTROL

LESSON 6 FLOW OF CONTROL This lesson discusses a variety of flow of control constructs. The break and continue statements are used to interrupt ordinary iterative flow of control in loops. In addition,

Chapter 4 C Program Control

1 Chapter 4 C Program Control Copyright 2007 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 2 Chapter 4 C Program Control Outline 4.1 Introduction 4.2 The Essentials of Repetition

C++ Programming: From Problem Analysis to Program Design, Third Edition

C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 5: Control Structures II (Repetition) Why Is Repetition Needed? Repetition allows you to efficiently use variables Can input,

Loops / Repetition Statements

Loops / Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops C has three kinds of repetition statements: the while loop the for

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University

ITC213: STRUCTURED PROGRAMMING Bhaskar Shrestha National College of Computer Studies Tribhuvan University Lecture 08: Control Statements Readings: Chapter 6 Control Statements and Their Types A control

Lecture 6. Statements

Lecture 6 Statements 1 Statements This chapter introduces the various forms of C++ statements for composing programs You will learn about Expressions Composed instructions Decision instructions Loop instructions

Introduction to C Programming

1 2 Introduction to C Programming 2.6 Decision Making: Equality and Relational Operators 2 Executable statements Perform actions (calculations, input/output of data) Perform decisions - May want to print

C Language Part 2 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

C Language Part 2 (Minor modifications by the instructor) 1 Scope Rules A variable declared inside a function is a local variable Each local variable in a function comes into existence when the function

REPETITION CONTROL STRUCTURE LOGO

CSC 128: FUNDAMENTALS OF COMPUTER PROBLEM SOLVING REPETITION CONTROL STRUCTURE 1 Contents 1 Introduction 2 for loop 3 while loop 4 do while loop 2 Introduction It is used when a statement or a block of

Overview of C, Part 2. CSE 130: Introduction to Programming in C Stony Brook University

Overview of C, Part 2 CSE 130: Introduction to Programming in C Stony Brook University Integer Arithmetic in C Addition, subtraction, and multiplication work as you would expect Division (/) returns the

Chapter 3. More Flow of Control. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Chapter 3 More Flow of Control Overview 3.1 Using Boolean Expressions 3.2 Multiway Branches 3.3 More about C++ Loop Statements 3.4 Designing Loops Slide 3-3 Flow Of Control Flow of control refers to the

Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade;

Control Statements Control Statements All programs could be written in terms of only one of three control structures: Sequence Structure Selection Structure Repetition Structure Sequence structure The

Introduction to the Java Basics: Control Flow Statements

Lesson 3: Introduction to the Java Basics: Control Flow Statements Repetition Structures THEORY Variable Assignment You can only assign a value to a variable that is consistent with the variable s declared

BBM 101 Introduc/on to Programming I Fall 2013, Lecture 4

BBM 101 Introduc/on to Programming I Fall 2013, Lecture 4 Instructors: Aykut Erdem, Erkut Erdem, Fuat Akal TAs: Ahmet Selman Bozkir, Gultekin Isik, Yasin Sahin 1 Today Condi/onal Branching Logical Expressions

MODULE 2: Branching and Looping

MODULE 2: Branching and Looping I. Statements in C are of following types: 1. Simple statements: Statements that ends with semicolon 2. Compound statements: are also called as block. Statements written

Chapter 4 C Program Control

Chapter C Program Control 1 Introduction 2 he Essentials of Repetition 3 Counter-Controlled Repetition he for Repetition Statement 5 he for Statement: Notes and Observations 6 Examples Using the for Statement

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

C Language Part 1 (Minor modifications by the instructor) References C for Python Programmers, by Carl Burch, 2011. http://www.toves.org/books/cpy/ The C Programming Language. 2nd ed., Kernighan, Brian,

ECE15: Introduction to Computer Programming Using the C Language. Lecture Unit 4: Flow of Control

ECE15: Introduction to Computer Programming Using the C Language Lecture Unit 4: Flow of Control Outline of this Lecture Examples of Statements in C Conditional Statements The if-else Conditional Statement

Lesson #4. Logical Operators and Selection Statements. 4. Logical Operators and Selection Statements - Copyright Denis Hamelin - Ryerson University

Lesson #4 Logical Operators and Selection Statements Control Structures Control structures combine individual instructions into a single logical unit with one entry point at the top and one exit point

Unit 3 Decision making, Looping and Arrays

Unit 3 Decision making, Looping and Arrays Decision Making During programming, we have a number of situations where we may have to change the order of execution of statements based on certain conditions.

C++ Programming: From Problem Analysis to Program Design, Third Edition

C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection) Control Structures A computer can proceed: In sequence Selectively (branch) - making

\n is used in a string to indicate the newline character. An expression produces data. The simplest expression

Chapter 1 Summary Comments are indicated by a hash sign # (also known as the pound or number sign). Text to the right of the hash sign is ignored. (But, hash loses its special meaning if it is part of

DECISION MAKING STATEMENTS

DECISION MAKING STATEMENTS If, else if, switch case These statements allow the execution of selective statements based on certain decision criteria. C language provides the following statements: if statement

3. EXPRESSIONS. It is a sequence of operands and operators that reduce to a single value.

3. EXPRESSIONS It is a sequence of operands and operators that reduce to a single value. Operator : It is a symbolic token that represents an action to be taken. Ex: * is an multiplication operator. Operand:

Lecture 8. Daily Puzzle

Lecture 8 if and switch Statements Daily Puzzle If a bottle, partly filled with liquid, has a round, square or rectangular bottom which is flat, can you find its volume using only a ruler? You may not

Chapter 3 Structured Program Development

1 Chapter 3 Structured Program Development Copyright 2007 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 3 - Structured Program Development Outline 3.1 Introduction

Theory of control structures

Theory of control structures Paper written by Bohm and Jacopini in 1966 proposed that all programs can be written using 3 types of control structures. Theory of control structures sequential structures

8. Control statements

8. Control statements A simple C++ statement is each of the individual instructions of a program, like the variable declarations and expressions seen in previous sections. They always end with a semicolon

(Not Quite) Minijava

(Not Quite) Minijava CMCS22620, Spring 2004 April 5, 2004 1 Syntax program mainclass classdecl mainclass class identifier { public static void main ( String [] identifier ) block } classdecl class identifier

STUDENT OUTLINE. Lesson 8: Structured Programming, Control Structures, if-else Statements, Pseudocode

STUDENT OUTLINE Lesson 8: Structured Programming, Control Structures, if- Statements, Pseudocode INTRODUCTION: This lesson is the first of four covering the standard control structures of a high-level

Module 4: Decision-making and forming loops

1 Module 4: Decision-making and forming loops 1. Introduction 2. Decision making 2.1. Simple if statement 2.2. The if else Statement 2.3. Nested if Statement 3. The switch case 4. Forming loops 4.1. The

Copyright 1996 David R. Hanson Computer Science 126, Fall 1996 3-1 Lecture 3. More About C Programming languages have their lingo Programming language Types are categories of values int, float, char Constants

C Program Control. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan

C Program Control Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan Outline The for repetition statement switch multiple selection statement break

Control Structures. Lecture 4 COP 3014 Fall September 18, 2017

Control Structures Lecture 4 COP 3014 Fall 2017 September 18, 2017 Control Flow Control flow refers to the specification of the order in which the individual statements, instructions or function calls

Conditional Statement

Conditional Statement 1 Logical Expressions n Uses relational and logical operators n Informally, specifies a condition which can be true or false n Evaluates to value 0 or 1 0 implies the condition is

Structured Program Development

Structured Program Development Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan Outline Introduction The selection statement if if.else switch The

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

Programming, Data Structures and Algorithms Prof. Shankar Balachandran Department of Computer Science and Engineering Indian Institute of Technology, Madras Lecture 05 I/O statements Printf, Scanf Simple

Structured Programming. Dr. Mohamed Khedr Lecture 9

Structured Programming Dr. Mohamed Khedr http://webmail.aast.edu/~khedr 1 Two Types of Loops count controlled loops repeat a specified number of times event-controlled loops some condition within the loop

Computer System and programming in C

1 Basic Data Types Integral Types Integers are stored in various sizes. They can be signed or unsigned. Example Suppose an integer is represented by a byte (8 bits). Leftmost bit is sign bit. If the sign

Java Programming: Guided Learning with Early Objects Chapter 5 Control Structures II: Repetition

Java Programming: Guided Learning with Early Objects Chapter 5 Control Structures II: Repetition Learn about repetition (looping) control structures Explore how to construct and use: o Counter-controlled

Note: unless otherwise stated, the questions are with reference to the C Programming Language. You may use extra sheets if need be.

CS 156 : COMPUTER SYSTEM CONCEPTS TEST 1 (C PROGRAMMING PART) FEBRUARY 6, 2001 Student s Name: MAXIMUM MARK: 100 Time allowed: 45 minutes Note: unless otherwise stated, the questions are with reference

XQ: An XML Query Language Language Reference Manual

XQ: An XML Query Language Language Reference Manual Kin Ng kn2006@columbia.edu 1. Introduction XQ is a query language for XML documents. This language enables programmers to express queries in a few simple

Functions In general, functions are blocks of code that perform a number of pre-defined commands to accomplish something productive. You can either use the built-in library functions or you can create

Using Boolean Expressions. Multiway Branches. More about C++ Loop Statements. Designing Loops. In this chapter, you will learn about:

Chapter 3 In this chapter, you will learn about: Using Boolean Expressions Multiway Branches More about C++ Loop Statements Designing Loops Boolean Expressions Take the Value true or false Boolean Value

Loops / Repetition Statements

Loops / Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops C has three kinds of repetition statements: the while loop the for

CHAPTER : 9 FLOW OF CONTROL

CHAPTER 9 FLOW OF CONTROL Statements-Statements are the instructions given to the Computer to perform any kind of action. Null Statement-A null statement is useful in those case where syntax of the language

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal Lesson Goals Understand Control Structures Understand how to control the flow of a program

DELHI PUBLIC SCHOOL TAPI

Loops Chapter-1 There may be a situation, when you need to execute a block of code several number of times. In general, statements are executed sequentially: The first statement in a function is executed

Chapter 5. Repetition. Contents. Introduction. Three Types of Program Control. Two Types of Repetition. Three Syntax Structures for Looping in C++

Repetition Contents 1 Repetition 1.1 Introduction 1.2 Three Types of Program Control Chapter 5 Introduction 1.3 Two Types of Repetition 1.4 Three Structures for Looping in C++ 1.5 The while Control Structure

CSE 130 Introduction to Programming in C Control Flow Revisited

CSE 130 Introduction to Programming in C Control Flow Revisited Spring 2018 Stony Brook University Instructor: Shebuti Rayana Control Flow Program Control Ø Program begins execution at the main() function.

Loops and Files. Chapter 04 MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz

Loops and Files Chapter 04 MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz Chapter Topics o The Increment and Decrement Operators o The while Loop o Shorthand Assignment Operators o The do-while

Conditionals and Loops

Conditionals and Loops Conditionals and Loops Now we will examine programming statements that allow us to: make decisions repeat processing steps in a loop Chapter 5 focuses on: boolean expressions conditional

CSE101-lec#12. Designing Structured Programs Introduction to Functions. Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU

CSE101-lec#12 Designing Structured Programs Introduction to Functions Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU Outline Designing structured programs in C: Counter-controlled repetition

Control Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003

Control Flow COMS W1007 Introduction to Computer Science Christopher Conway 3 June 2003 Overflow from Last Time: Why Types? Assembly code is typeless. You can take any 32 bits in memory, say this is an

Chapter 5 Control Statements: Part 2 Section 5.2 Essentials of Counter-Controlled Repetition

Chapter 5 Control Statements: Part 2 Section 5.2 Essentials of Counter-Controlled Repetition 5.2 Q1: Counter-controlled repetition requires a. A control variable and initial value. b. A control variable

5. Selection: If and Switch Controls

Computer Science I CS 135 5. Selection: If and Switch Controls René Doursat Department of Computer Science & Engineering University of Nevada, Reno Fall 2005 Computer Science I CS 135 0. Course Presentation

Iosif Ignat, Marius Joldoș Laboratory Guide 4. Statements. STATEMENTS in C

STATEMENTS in C 1. Overview The learning objective of this lab is: To understand and proper use statements of C/C++ language, both the simple and structured ones: the expression statement, the empty statement,

Week 2. Relational Operators. Block or compound statement. if/else. Branching & Looping. Gaddis: Chapters 4 & 5. CS 5301 Spring 2018.

Week 2 Branching & Looping Gaddis: Chapters 4 & 5 CS 5301 Spring 2018 Jill Seaman 1 Relational Operators l relational operators (result is bool): == Equal to (do not use =)!= Not equal to > Greater than

The Arithmetic Operators. Unary Operators. Relational Operators. Examples of use of ++ and

The Arithmetic Operators The arithmetic operators refer to the standard mathematical operators: addition, subtraction, multiplication, division and modulus. Op. Use Description + x + y adds x and y x y

The Arithmetic Operators

The Arithmetic Operators The arithmetic operators refer to the standard mathematical operators: addition, subtraction, multiplication, division and modulus. Examples: Op. Use Description + x + y adds x

Java Programming: Guided Learning with Early Objects Chapter 5 Control Structures II: Repetition

Java Programming: Guided Learning with Early Objects Chapter 5 Control Structures II: Repetition Learn about repetition (looping) control structures Explore how to construct and use: o Counter-controlled

Topics. Chapter 5. Equality Operators

Topics Chapter 5 Flow of Control Part 1: Selection Forming Conditions if/ Statements Comparing Floating-Point Numbers Comparing Objects The equals Method String Comparison Methods The Conditional Operator

A complex expression to evaluate we need to reduce it to a series of simple expressions. E.g * 7 =>2+ 35 => 37. E.g.

1.3a Expressions Expressions An Expression is a sequence of operands and operators that reduces to a single value. An operator is a syntactical token that requires an action be taken An operand is an object

15 FUNCTIONS IN C 15.1 INTRODUCTION

15 FUNCTIONS IN C 15.1 INTRODUCTION In the earlier lessons we have already seen that C supports the use of library functions, which are used to carry out a number of commonly used operations or calculations.

Main Program. C Programming Notes. #include <stdio.h> main() { printf( Hello ); } Comments: /* comment */ //comment. Dr. Karne Towson University

C Programming Notes Dr. Karne Towson University Reference for C http://www.cplusplus.com/reference/ Main Program #include main() printf( Hello ); Comments: /* comment */ //comment 1 Data Types

Loops! Loops! Loops! Lecture 5 COP 3014 Fall September 25, 2017

Loops! Loops! Loops! Lecture 5 COP 3014 Fall 2017 September 25, 2017 Repetition Statements Repetition statements are called loops, and are used to repeat the same code mulitple times in succession. The

Lecture 9. Assignment. Logical Operations. Logical Operations - Motivation 2/8/18

Assignment Lecture 9 Logical Operations Formatted Print Printf Increment and decrement Read through 3.9, 3.10 Read 4.1. 4.2, 4.3 Go through checkpoint exercise 4.1 Logical Operations - Motivation Logical