C Programming Lecture 11.

Slides:



Advertisements
Similar presentations
The if-else Statements
Advertisements

1 Chapter Five Selection and Repetition. 2 Objectives How to make decisions using the if statement How to make decisions using the if-else statement How.
Dr. Yang, Qingxiong (with slides borrowed from Dr. Yuen, Joe) LT4: Control Flow - Loop CS2311 Computer Programming.
Conditionals – if - else Dr. Sajib Datta
July 13 th.  If/ Else if / Else  Variable Scope  Nested if/else's  Switch statements  Conditional Operator.
1 Flow of Control Chapter 4 in ABC. 2 Operators and Associativity OperatorAssociativity +(unary) -(unary) !right to left * / % left to right + -left.
Additional control structures. The if-else statement The if-else statement chooses which of two statements to execute The if-else statement has the form:
Selection Statements choice of one among several blocks of code Java supports 3 kinds of selection statements: if statement – selects one block or leaves.
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
true (any other value but zero) false (zero) expression Statement 2
1 Conditionals In many cases we want our program to make a decision about whether a piece of code should be executed or not, based on the truth of a condition.
1 Chapter 3 Flow of Control. 2 Outline  How to specify conditions?  Relational, Equality and Logical Operators  Statements  Statements: compound statement.
Tutorial 4 Decision Making with Control Structures and Statements Section A - Decision Making JavaScript Tutorial 4 -Decision Making with Control.
1 Lecture 5  More flow control structures  for  do  continue  break  switch  Structured programming  Common programming errors and tips  Readings:
Slides prepared by Rose Williams, Binghamton University Chapter 3 Flow of Control if-else and switch statements.
Switch Statements Comparing Exact Values. 2 The Switch Statement The switch statement provides another way to decide which statement to execute next The.
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
COMPUTER PROGRAMMING. Control Structures A program is usually not limited to a linear sequence of instructions. During its process it may repeat code.
1 CSC103: Introduction to Computer and Programming Lecture No 11.
Conditional Statements For computer to make decisions, must be able to test CONDITIONS IF it is raining THEN I will not go outside IF Count is not zero.
Programming in Java (COP 2250) Lecture 11 Chengyong Yang Fall, 2005.
1 Relational Expressions Relational expressions: –Expressions that compare operands –Sometimes called conditions –Evaluated to yield a result –Typically.
5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,
Simple Control Structures IF, IF-ELSE statements in C.
Previously Repetition Structures While, Do-While, For.
Controlling Execution Dong Shao, Nanjing Unviersity.
Chapter 3. Outline Relational Operators Loops Decisions Logical Operators Precedence Summary.
A First Book of ANSI C Fourth Edition Chapter 4 Selection.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
Switch Statements Comparing Exact Values. The Switch Statement: Syntax The switch statement provides another way to decide which statement to execute.
Conditional Statement Chapter 8. Conditional Statements Are statements that check an expression then may or may not execute a statement or group of statement.
Decision making statements. Decision making statements are used to skip or to execute a group of statements based on the result of some condition. The.
SE-1020 Dr. Mark L. Hornick 1 The switch statement int gradeLevel = kbd.nextInt(); // can be byte or short also, but not long switch( gradeLevel ) { //
J AVA P ROGRAMMING 2 C H 03: C ONTROL STATEMENTS if, for loop (review) switch, while, do while break, continue Fall Java Programming.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Introduction to C Programming CE Lecture 3 Control Structures in C.
Control statements Mostafa Abdallah
Control Flow Statements
CSI 3125, Preliminaries, page 1 Control Statements.
Control structures in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
Dr. Sajib Datta Jan 23,  A precedence for each operator ◦ Multiplication and division have a higher precedence than addition and subtraction.
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
 Very often when you write code, you want to perform different actions for different decisions. You can use conditional statements in your code to do.
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
Dr. Sajib Datta Sep 3,  A new operator used in C is modulus operator: %  % only used for integers, not floating-point  Gives the integer.
CNG 140 C Programming (Lecture set 3)
Decision Making in C.
Control Structures Combine individual statements into a single logical unit with one entry point and one exit point. Used to regulate the flow of execution.
Chapter 5 Control Statements: switch Statement
Flow of Control.
Chapter 4: Making Decisions.
Control Structures.
Flow of Control.
2.0 FUNDAMENTALS OF JAVA PROGRAMMING LANGUAGE
Selection CSCE 121 J. Michael Moore.
Bools & Ifs.
3 Control Statements:.
CSC215 Lecture Flow Control.
CSC215 Lecture Control Flow.
Relational, Logical, and Equality Operators
Control Structures Part 3
Control Statements Paritosh Srivastava.
Comparing Data & the ‘switch’ Statement
Comparing Data & the ‘switch’ Statement
CprE 185: Intro to Problem Solving (using C)
Chap 7. Advanced Control Statements in Java
Flow of Control.
CSC215 Lecture Control Flow.
Presentation transcript:

C Programming Lecture 11

The switch Statement The switch statement is a multiway conditional statement generalizing the if-else statement.

switch switch (expression) { case constant1: statement . . . statement case constant_n: statement default : statement } /* end switch */

The break Statement The break statement causes a jump out of a switch statement. Execution continues after the closing brace of the switch statement.

Example Using the switch Statement switch (val) { case 1: ++a_cnt; break; case 2: case 3: ++b_cnt; default: ++other_cnt; } The val expression must be of an integer type. The constant integral expressions following the case labels must all be unique.

The Effect of a switch Statement Evaluate the switch expression. Go to the case label having a constant value that matches the value of the expression found in step 1. If a match is not found, go to the default label. If there is no default label, terminate the switch. Terminate the switch when a break statement is encountered, or by “falling off the end”.

Bell Labs Industrial Programming Style Follow normal rules of English, where possible. e.g., put a space after a comma Put one space on either side of a binary operator. Indent code in a consistent fashion to indicate flow of control. Put braces as indicated in the following example: for (i = 0; i < n; ++i) { . . . /* indent 3 spaces */ }

Student Style Place beginning and ending braces in the same column. while (i < 10) { . . . } Either style for braces is acceptable in our class.

Misuse of Relational Operators int k = 8; if (2 < k < 7) printf(“true”); /*true is printed*/ else printf(“false”); if (2 < k && k < 7) printf(“true); printf(“false”); /*false is printed*/