BRANCHING if-else statements

Similar documents
Conditionals and Loops

Introduction to C Programming

Chapter 3. Selections

Algorithms and Conditionals

Control Structures in Java if-else and switch

Control Structures in Java if-else and switch

Review Chapters 1 to 4. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Java Flow of Control

Conditional Programming

Decision Making -Branching. Class Incharge: S. Sasirekha

Selection Statements. Pseudocode

Program Development. Java Program Statements. Design. Requirements. Testing. Implementation

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

More Programming Constructs -- Introduction

Expressions. Arithmetic expressions. Logical expressions. Assignment expression. n Variables and constants linked with operators

What we have learned so far

Program Development. Chapter 3: Program Statements. Program Statements. Requirements. Java Software Solutions for AP* Computer Science A 2nd Edition

Chapter 3: Program Statements

Chapter 3 Structured Program Development

CS313D: ADVANCED PROGRAMMING LANGUAGE

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

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

Chapter 4: Conditionals and Loops

Topics. Chapter 5. Equality Operators

2.1. Chapter 2: Parts of a C++ Program. Parts of a C++ Program. Introduction to C++ Parts of a C++ Program

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

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

Basic operators, Arithmetic, Relational, Bitwise, Logical, Assignment, Conditional operators. JAVA Standard Edition

ECE 122. Engineering Problem Solving with Java

ECE 122. Engineering Problem Solving with Java

Module 3 SELECTION STRUCTURES 2/15/19 CSE 1321 MODULE 3 1

CHAPTER 2.1 CONTROL STRUCTURES (SELECTION) Dr. Shady Yehia Elmashad

Chapter 3: Decision Structures

1.4 Control Structures: Selection. Department of CSE

5. Selection: If and Switch Controls

Lab 8: IF statement. Conditionals and Loops. Copyright 2012 Pearson Education, Inc.

Lecture 9. Monday, January 31 CS 205 Programming for the Sciences - Lecture 9 1

Java I/O and Control Structures

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

Slide 1 CS 170 Java Programming 1 The Switch Duration: 00:00:46 Advance mode: Auto

Chapter 7. Expressions and Assignment Statements ISBN

Comparing Data. Comparing Floating Point Values. Comparing Float Values. CS257 Computer Science I Kevin Sahr, PhD

Introduction to Object-Oriented Programming

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

CP FAQS Q-1) Define flowchart and explain Various symbols of flowchart Q-2) Explain basic structure of c language Documentation section :

Chapter 3 Selection Statements

Java I/O and Control Structures Algorithms in everyday life

Computers Programming Course 6. Iulian Năstac

Review. Relational Operators. The if Statement. CS 151 Review #4

Chapter 4: Conditionals and Loops

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

Computer Programming. Decision Making (2) Loops

CS313D: ADVANCED PROGRAMMING LANGUAGE

C: How to Program. Week /Mar/05

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

(4-2) Selection Structures in C H&K Chapter 4. Instructor - Andrew S. O Fallon CptS 121 (September 12, 2018) Washington State University

Chapter 3: Decision Structures

Fundamentals of Programming

Handout 4 Conditionals. Boolean Expressions.

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

Fundamental of Programming (C)

COMP Primitive and Class Types. Yi Hong May 14, 2015

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

Guide for The C Programming Language Chapter 1. Q1. Explain the structure of a C program Answer: Structure of the C program is shown below:

Structured Program Development

Expressions and Assignment Statements

Chapter 2: Introduction to C++

Midterm Exam. CSCI 2132: Software Development. March 4, Marks. Question 1 (10) Question 2 (10) Question 3 (10) Question 4 (10) Question 5 (5)

Chapter 7. Expressions and Assignment Statements

Chapter 4: Conditionals and Loops

Chapter Overview. C++ Basics. Variables and Assignments. Variables and Assignments. Keywords. Identifiers. 2.1 Variables and Assignments

Lecture 5 Tao Wang 1

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

Chapter 4 C Program Control

Basic computer skills such as using Windows, Internet Explorer, and Microsoft Word. and Java. Chapter 2 Primitive Data Types and Operations

Computer System and programming in C

ISA 563 : Fundamentals of Systems Programming

1.3b Type Conversion

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

Programming for Engineers Iteration

CS1004: Intro to CS in Java, Spring 2005

Program Elements -- Introduction

Chapter 2: Special Characters. Parts of a C++ Program. Introduction to C++ Displays output on the computer screen

Chapter 2, Part III Arithmetic Operators and Decision Making

Flow of Control. Selection. if statement. True and False in C False is represented by any zero value. switch

These are reserved words of the C language. For example int, float, if, else, for, while etc.

Introduction. C provides two styles of flow control:

Operators. Java operators are classified into three categories:

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

Chapter 4: Basic C Operators

In Fig. 3.5 and Fig. 3.7, we include some completely blank lines in the pseudocode for readability. programs into their various phases.

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

MODULE 2: Branching and Looping

Flow Control. CSC215 Lecture

CMPT 125: Lecture 4 Conditionals and Loops

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

Chapter 3: Operators, Expressions and Type Conversion

Fundamentals of Programming Session 7

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

Computer Science & Engineering 150A Problem Solving Using Computers

Transcription:

BRANCHING if-else statements

Conditional Statements A conditional statement lets us choose which statement t t will be executed next Therefore they are sometimes called selection statements Conditional statements give us the power to make basic decisions The C conditional statements are the: if statement if-else statement if-else if-else if-else ladder switch statement 2004 Pearson Addison-Wesley. All rights reserved

The if Statement The if statement has the following syntax: if is a C reserved word The condition must be a boolean expression. It must evaluate to either true or false. if ( condition ) statement; If the condition is true, the statement is executed. If it is false, the statement is skipped. 2004 Pearson Addison-Wesley. All rights reserved

The if Statement (Example) Selection structure: Used to choose among alternative courses of action Pseudocode: If stude nt s m ark is gre ate r th an or e qualto 60 Print Passed Pseudocode statement in C: #include <stdio.h> main() { float marks; printf( Enter your marks: ); scanf( %f, &marks); if ( marks >= 60 ) printf( "Passed\n" ); }

Logic of an if statement condition evaluated true false statement 2004 Pearson Addison-Wesley. All rights reserved

Relational Operators A condition often uses one of C's equality operators or relational l operators == equal lto!= not equal to < less than > greater than <= less than or equal to >= greater than or equal to Note the difference between the equality operator (==) and the assignment operator (=) 2004 Pearson Addison-Wesley. All rights reserved

Indentation The statement controlled by the if statement is indented d to indicate that t relationship The use of a consistent indentation style makes a program easier to read and understand Although it makes no difference to the compiler, proper indentation is crucial 2004 Pearson Addison-Wesley. All rights reserved

The if-else Statement An else clause can be added to an if statement to make an if-else statement t t if ( condition ) statement1; else statement2; If the condition is true, statement1 is executed; if the condition is false, statement2 is executed One or the other will be executed, but not both

Logic of an if-else statement condition evaluated true false statement1 statement2

The if-else Statement (Example) Selection structure: Pseudocode: If stude nt s m ark is gre ate r th an or e qualto 60 Print Passed Otherwise Pseudocode statement in C: #include <stdio.h> Print Failed main() { float marks; printf( Enter your marks: ); scanf( %f, &marks); if ( marks >= 60 ) printf( "Passed\n" ); else printf( Failed\n ); }

Logic of previous example printf( Enter your marks: ); scanf( %f, &marks); marks >= 60 true false printf ( Passed ); printf ( Failed );

Another example Write down a program that will take two integers as input and will print the maximum of two.

Another example Write down a program that will take three integers as input and will print the maximum of three.

The if-else if-else if else ladder If-else if- else if else can be used to select from multiple choices: if ( condition1 ) statement1; else if ( condition2 ) statement2; else if ( conditionk ) statementk; else statement; If the condition1 is true, statement1 is executed; if condition2 is true, statement2 t t2 is executed; and so on One or the other will be executed (i.e. those are mutually exclusive)

Logic of an if-else if-else statement condition 1 evaluated true condition 2 evaluated statement1 true statement2 condition k evaluated true statement false statement3

Example The following chart will be used for a quick grade conversion in C programming language course: 90-100 A 80-89 B 70-79 C 60-69 D 0-59 F Write down a program that will take a student s mark as input and will convert it to the corresponding letter grade.

Combining multiple conditions: Logical Operators C defines the following logical operators:! Logical NOT && Logical AND Logical OR Logical NOT is a unary operator (it operates on one operand) Logical AND and logical OR are binary operators (each operates on two operands) 2004 Pearson Addison-Wesley. All rights reserved

Logical NOT The logical NOT operation is also called logical negation or logical complement If some condition a is true, then!a is false; if a is false, then!a is true Logical expressions can be shown using a truth table a!a true false false true 2004 Pearson Addison-Wesley. All rights reserved

Selection structure: Example Used to choose among alternative courses of action Pseudocode: If stude nt s m ark is gre ate r th an or e qualto 60 Print Passed Pseudocode statement in C: #include <stdio.h> main() { float marks; printf( Enter your marks: ); scanf( %f, &marks); if ( marks >= 60 ) printf( "Passed\n" ); }

Selection structure: Example Used to choose among alternative courses of action Pseudocode: If stude nt s m ark is gre ate r th an or e qualto 60 Print Passed Pseudocode statement in C: #include <stdio.h> main() { float marks; printf( Enter your marks: ); scanf( %f, &marks); if!( marks < 60 ) printf( "Passed\n" ); }

Logical AND and Logical OR The logical AND expression a && b is true if both a and b are true, and false otherwise The logical OR expression a b is true if a or b or both are true, and false otherwise 2004 Pearson Addison-Wesley. All rights reserved

Logical Operators A truth table shows all possible true-false combinations of the terms Since && and each have two operands, there are four possible combinations of conditions a and b a b a && b a b true true true true true false false true false true false true false false false false 2004 Pearson Addison-Wesley. All rights reserved

Example The following chart will be used for a quick grade conversion in C programming language course: 90-100 A 80-89 B 70-79 C 60-69 D 0-59 F Write down a program that will take a student s mark as input and will convert it to the corresponding letter grade.

Boolean Expressions in C C does not have a boolean data type. Therefore, C compares the values of variables and expressions against 0 (zero) to determine if they are true or false. If the value is 0 then the result is implicitly assumed to be false. If the value is different from 0 then the result is implicitly assumed to be true. C++ and Java have boolean data types.

Short-Circuited Operators The processing of logical AND and logical OR is short-circuited it If the left operand is sufficient to determine the result, the right operand is not evaluated if (count!= 0 && total/count > MAX) printf ("Testing "); This type of processing must be used carefully 2004 Pearson Addison-Wesley. All rights reserved

Example: Write a C program that calculates weekly wages for hourly employees. Regular hours 0-40 are paid at $10/hours. Overtime (> 40 hours per week) is paid at 150%

Block Statements Several statements can be grouped together into a block statement t t delimited it d by braces A block statement can be used wherever a statement is called for in the C syntax rules if (b == 0) { printf ( divide by zero!!\n"); errorcount++; }

Block Statements In an if-else statement, the if portion, or the else portion, or both, could be block statements t t if (b == 0) { printf( divide by zero!!"); errorcount++; } else { result = a/b; printf ( Result of division: %d, result); }

The Conditional Operator C has a conditional operator that uses a boolean condition to determine which h of two expressions is evaluated Its syntax is: condition? expression1 : expression2 If the condition is true, expression1 is evaluated; if it is false, expression2 is evaluated The value of the entire conditional operator is the The value of the entire conditional operator is the value of the selected expression

The Conditional Operator The conditional operator is similar to an if-else statement, t t except tthat titi is an expression that t returns a value For example: larger = ((num1 > num2)? num1 : num2); If num1 is greater than num2, then num1 is assigned to larger; otherwise, num2 is assigned to larger The conditional operator is ternary because it requires three operands

Example: Write a C program that will find the absolute value of a number. You can use only the ternary operator.

The switch Statement The switch statement provides another way to decide which h statement t t to execute next The switch statement evaluates an expression, then attempts to match the result to one of several possible cases Each case contains a value and a list of statements The flow of control transfers to statement associated with the first case value that t matches

The switch Statement Often a break statement is used as the last statement t t in each case's statement t t list A break statement causes control to transfer to the end of the switch statement If a break statement is not used, the flow of control will continue into the next case Sometimes this may be appropriate, but often we want to execute only the statements associated with one case

The switch Statement An example of a switch statement: switch (option) { case 'A': acount++; break; case 'B': bcount++; break; case 'C': ccount++; break; default: othercount++; break; }

The switch Statement A switch statement can have an optional default case The default case has no associated value and simply uses the reserved word default If the default case is present, control will transfer to it if no other case value matches If there is no default case, and no other value matches, control falls through to the statement after the switch

The switch Statement The expression of a switch statement must result in an integral type, meaning an integer (byte, short, int,) or a char It cannot be a floating point value (float or double) The implicit test condition in a switch statement is equality You cannot perform relational checks with a switch statement

The switch Statement The general syntax of a switch statement is: switch switch ( expression ) and { case case value1 : are statement-list1 reserved case value2 : words statement-list2 case value3 : statement-list3 case... } to here If expression matches value2, control jumps

Example: Write down a program using switch structure that will take an integer as input and will determine whether the number is odd or even.

THE END