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

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

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

Computational Expression

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

Information Science 1

Information Science 1

Conditional Statement

Introduction to C Programming

Kingdom of Saudi Arabia Princes Nora bint Abdul Rahman University College of Computer Since and Information System CS240 BRANCHING STATEMENTS

Chapter 4 - Notes Control Structures I (Selection)

Conditional Statement

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

Programming for Engineers Iteration

Chapter Overview. More Flow of Control. Flow Of Control. Using Boolean Expressions. Using Boolean Expressions. Evaluating Boolean Expressions

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

Flow Control. CSC215 Lecture

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

5. Selection: If and Switch Controls

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

The Arithmetic Operators

Lecture 5 Tao Wang 1

If Control Construct

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

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

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

Computer Science & Engineering 150A Problem Solving Using Computers

Introduction to Programming Using Java (98-388)

Objectives. Chapter 4: Control Structures I (Selection) Objectives (cont d.) Control Structures. Control Structures (cont d.) Relational Operators

Chapter 4: Control Structures I (Selection) Objectives. Objectives (cont d.) Control Structures. Control Structures (cont d.

Chapter 3. More Flow of Control. Copyright 2008 Pearson Addison-Wesley. All rights reserved.

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

Chapter 4: Making Decisions

Chapter 4: Making Decisions

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

Chapter 3. More Flow of Control

BITG 1223: Selection Control Structure by: ZARITA (FTMK) LECTURE 4 (Sem 1, 16/17)

Maltepe University Computer Engineering Department. Algorithms and Programming. Chapter 4: Conditionals - If statement - Switch statement

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

Topics. Chapter 5. Equality Operators

Chapter 4 C Program Control

BRANCHING if-else statements

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

Following is the general form of a typical decision making structure found in most of the programming languages:

In Java, data type boolean is used to represent Boolean data. Each boolean constant or variable can contain one of two values: true or false.

Problem Solving through Programming In C Prof. Anupam Basu Department of Computer Science & Engineering Indian Institute of Technology, Kharagpur

Chapter 4: Making Decisions. Copyright 2012 Pearson Education, Inc. Sunday, September 7, 14

CS 105 Lecture 5 Logical Operators; Switch Statement. Wed, Feb 16, 2011, 5:11 pm

DECISION MAKING STATEMENTS

Chapter 4. Flow of Control

CS 199 Computer Programming. Spring 2018 Lecture 5 Control Statements

5. Control Statements

Decision Making in C

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

if Statement Numeric Rela5onal Operators The if Statement Flow of Control: Branching (Savitch, Chapter 3)

if Statement Numeric Rela7onal Operators The if Statement Flow of Control: Branching (Savitch, Chapter 3)

School of Computer Science CPS109 Course Notes 5 Alexander Ferworn Updated Fall 15

Control Structures. Code can be purely arithmetic assignments. At some point we will need some kind of control or decision making process to occur

CT 229 Java Syntax Continued

Control Structures in Java if-else and switch

Lecture 5. Review from last week. Selection Statements. cin and cout directives escape sequences

ARG! Language Reference Manual

CS313D: ADVANCED PROGRAMMING LANGUAGE

Control Structures in Java if-else and switch

Chapter 3: Decision Structures

Conditionals and Loops

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Chapter 3: Decision Structures

Algorithms and Conditionals

Chapter 3: Decision Structures

Boolean Algebra Boolean Algebra

Chapter 3 Selection Statements

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Lecture 3 Tao Wang 1

LECTURE 04 MAKING DECISIONS

Logical Operators and switch

Selection Statements. Pseudocode

Loops and Conditionals. HORT Lecture 11 Instructor: Kranthi Varala

Introduction. Following are the types of operators: Unary requires a single operand Binary requires two operands Ternary requires three operands

C++ Programming: From Problem Analysis to Program Design, Fourth Edition. Chapter 4: Control Structures I (Selection)

Control Structures. Control Structures Conditional Statements COMPUTER PROGRAMMING. Electrical-Electronics Engineering Dept.

More Programming Constructs -- Introduction

Chapter 2, Part III Arithmetic Operators and Decision Making

Decision Making -Branching. Class Incharge: S. Sasirekha

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

Flow Control. So Far: Writing simple statements that get executed one after another.

Linear execution of statements. CISC 1600/1610 Computer Science I. Alternatives to linear execution. Linear execution of statements

Operators. Java operators are classified into three categories:

Conditionals. CSE / ENGR 142 Programming I. Chapter 4. Conditional Execution. Conditional ("if ") Statement. Conditional Expressions

Chapter 4 C Program Control

C Course. IIT Kanpur. Lecture 3 Aug 31, Rishi Kumar <rishik>, Final year BT-MT, CSE

C Programming Basics

Chapter 4: Control Structures I

Chapter 4: Control Structures I (Selection)

x = 3 * y + 1; // x becomes 3 * y + 1 a = b = 0; // multiple assignment: a and b both get the value 0

COP 2000 Introduction to Computer Programming Mid-Term Exam Review

Relational Expressions. Boolean Expressions. Boolean Expressions. ICOM 4036 Programming Languages. Boolean Expressions

In this lab, you will learn more about selection statements. You will get familiar to

Type Conversion. and. Statements

Objectives. Chapter 4: Control Structures I (Selection) Objectives (cont d.) Control Structures

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

Transcription:

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 structures combination of individual statements into a logical unit that regulates the flow of execution in a program or function Sequence Selection (Making Decisions) Repetition (Looping) 3 111 Ch 06 1

Boolean Expressions Evaluate to true or false Forms Relational expression: <expr> <relational operator> <expr> Examples: 7 < 5 a + b > 6 Logical expression: <Boolean expr> <logical operator> <Boolean expr> Examples: (x < 7) && (y > 3) 4 Relational Operators Standard Algebraic Relational Operator C Relational Operator C Condition Example Meaning of C Condition Inequality < < x < y x is less than y <= x <= y x is less than or equal to y > > x > y x is greater than y >= x >= y x is greater than or equal to y Equality = == x == y x is equal to y!= x!= y x is not equal to y Ch 5 p. 46 5 Logical Operators (Compound Relationals) && (logical AND) Returns true if both conditions are true (logical OR) Returns true if either of its conditions is true Ch 6 p. 72! (logical NOT, logical negation) Is a unary operator, only takes one operand following Reverses the truth/falsity of its condition Returns true when its condition is false 6 111 Ch 06 2

Logical Operators Truth Table P Q P && Q P Q!P true true true true false true false false true false false true false true true false false false false true 7 Precedence of Operators 1. Unary +, unary -,!, ++, -- 2. Type casting 3. *, /, % 4. +, - 5. <, <=, >, >= 6. ==,!= 7. && 8. 9. = 8 The if Selection Structure Selection structure used when we want the computer to choose between two alternative courses of action 9 111 Ch 06 3

The if Selection Structure if Statement Boolean Expression true false true block 10 The if Selection Structure General form of if: if (Boolean Expression) { statement1; statement2;... } 11 The if-else Selection Structure if Only performs an action if the condition is true if-else A different action is performed when condition is true and when condition is false 12 111 Ch 06 4

if-else Selection Structure if-else statement false Boolean Expression true false block true block 13 The if-else Selection Structure 14 General form of if-else: if (expression) { statement1a; statement2a;... } else { statement1b; statement2b;... } The if-else Selection Structure Nested if-else structures Test for multiple cases by placing if-else selection structures inside if-else selection structures. 15 111 Ch 06 5

Nested if-else Structures 16 The if-else-if Construct 17 Once a condition is met, the rest of the statements are skipped The if-else-if Construct The standard way to indent the previous code is 18 111 Ch 06 6

The if-else Selection Structure Compound statement: Set of statements within a pair of braces Example: 19 The if-else Selection Structure Without the braces, only one statement is executed. e.g. given the following code: The statement, will be executed independent of the value of grade. The statement, 20 will execute only if grade is greater than or equal to 90. The dangling else Note: the compiler matches an else with the closest unmatched if The above will be treated as 21 111 Ch 06 7

The dangling else If the else is to match the outer if, use braces. 22 if-else Construct To avoid confusion, and possible errors, it is best to use braces even for single statements. However, code will be longer 23 Conditionals C uses an integer to represent Boolean values Zero is interpreted as false Any other integer value is interpreted as true 24 111 Ch 06 8

Conditionals is not a syntax error in C. The expression, n = 0, assigns zero to n and the value of the expression is 0. Zero is interpreted as false, and the false branch of the if statement will be taken. is not a syntax error in C. The expression assigns 5 to n. 5 is interpreted as true, and the true branch of the if statement will be taken. 25 Conditionals Remember to use the == operator to test for equality. To help catch the error when the equality check involves a constant, put the constant on the left hand side of the ==. For example, use instead of Since is not a valid assignment in C, the compiler will detect this error when == is intended. 26 The switch Multiple-Selection Structure switch Useful when variable or expression is tested for multiple values Consists of a series of case labels and an optional default case 27 111 Ch 06 9

The switch Multiple-Selection Structure With Breaks case a case a action(s) break case b case b action(s) break case z case z action(s) break default action(s) 28 The switch Multiple-Selection Structure Without Breaks case a case a action(s) case b case b action(s) case z case z action(s) default action(s) 29 switch Statement Syntax switch (switch_expression) { case constant1: statementsequence1 break; case constant2: statementsequence2 break; case constantn: statementsequencen break; default: defaultstmtsequence } 30 111 Ch 06 10

switch Statement The switch_expression is compared against the values constant1, constant2,, constantn constant1, constant2,, constantn must be simple constants or constant expressions. Can be a char or an int 31 switch Statement Reminder The switch statement ends break statement end of the switch statement When executing the statements after a case label, it continues to execute until it reaches a break statement or the end of the switch. If you omit the break statements, then after executing the code for one case, the computer will continue to execute the code for the next case. 32 Example of switch 33 111 Ch 06 11

Programming in C T H E E N D 34 111 Ch 06 12