CS115 FALL 2008-2009 Senem KUMOVA-METİN1 CHAPTER 4 FLOW OF CONTROL-1 Operators, If and switch statements.

Slides:



Advertisements
Similar presentations
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Advertisements

1 Conditional Statement. 2 Conditional Statements Allow different sets of instructions to be executed depending on truth or falsity of a logical condition.
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.
BBS514 Structured Programming (Yapısal Programlama)1 Selective Structures.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT3: Conditional Statements CS2311 Computer Programming.
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 4- 1.
1 Chapter 3 Flow of Control. 2 Outline  How to specify conditions?  Relational, Equality and Logical Operators  Statements  Statements: compound statement.
If Statements. COMP104 If / Slide 2 Three Program Structures * Sequence - executable statements which the computer processes in the given order * Choice.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Chapter 4 Making Decisions
Programming Control Flow. Sequential Program S1 S2 S5 S4 S3 int main() { Statement1; Statement2; … StatementN; } Start End.
1 Lecture 5  More flow control structures  for  do  continue  break  switch  Structured programming  Common programming errors and tips  Readings:
ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 2: Control Flow.
1 Chapter 3 Flow of Control. 2 Review of Class on Sep 23.
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
C Programming Lecture 12. The Compound Statement b A compound statement is a series of declarations and statements surrounded by braces. b A compound.
Algorithms and Computing Lecture 3 Control Statements By Dr. M. Tahir Khaleeq.
Conditional Statement
Control Structures – Selection Chapter 4 2 Chapter Topics  Control Structures  Relational Operators  Logical (Boolean) Operators  Logical Expressions.
1 Flow of Control. 2 Outline Relational, Equality, and Logical Operators Relational Operators and Expressions Equality Operators and Expressions Logical.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT3: Conditional Statements CS2311 Computer Programming.
Chapter 3 Control Flow Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Making Decisions. 4.1 Relational Operators Used to compare numbers to determine relative order Operators: > Greater than < Less than >= Greater than.
Chapter 3. Outline Relational Operators Loops Decisions Logical Operators Precedence Summary.
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.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4: Making Decisions.
Copyright 2003 Scott/Jones Publishing Making Decisions.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 4: Making Decisions.
Chapter Making Decisions 4. Relational Operators 4.1.
Control statements Mostafa Abdallah
CSCI 171 Presentation 3. Operators Instructs C to perform some operation Assignment = Mathematical Relational Logical.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 4: Making Decisions 1.
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.
Making Decisions in c. 1.if statement Imagine that you could translate a statement such as “If it is not raining, then I will go swimming” into the C.
ECE122 Feb 10, Unary Operator An operator that takes only a single operand Plus: + Minus: – Cast: (type). E.g. (double)
Lecture 3.1 Operators and Expressions Structured Programming Instructor: Prof. K. T. Tsang 1.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Dr. Sajib Datta Sep 3,  A new operator used in C is modulus operator: %  % only used for integers, not floating-point  Gives the integer.
BY ILTAF MEHDI(MCS,MCSE, CCNA) 1. INSTRUCTOR: ILTAF MEHDI (MCS, MCSE, CCNA, Web Developer) BY ILTAF MEHDI(MCS,MCSE, CCNA) 2 Programming Fundamentals Chapter.
Lesson #4 Logical Operators and Selection Statements.
Lesson #4 Logical Operators and Selection Statements.
ECE Application Programming
Operators And Expressions
Week 3 - Friday CS222.
Chapter 4: Making Decisions.
Lecture 3- Decision Structures
Chapter 4: Making Decisions.
CSE1320 INTERMEDIATE PROGRAMMING Operators+Conditionals+Loop
Flow of Control.
Introduction to Programming
Flow of Control.
CS1100 Computational Engineering
Exam 1 Date: Feb. 2nd, 2015 during class time (50 minutes) Coverage
Flow of Control.
CSC215 Lecture Flow Control.
CSC215 Lecture Control Flow.
Chapter 4: Control Structures I (Selection)
The System.exit() Method
Decision making and control functions
CprE 185: Intro to Problem Solving (using C)
Flow of Control.
CSC215 Lecture Control Flow.
Control Structure.
Presentation transcript:

CS115 FALL Senem KUMOVA-METİN1 CHAPTER 4 FLOW OF CONTROL-1 Operators, If and switch statements

CS115 FALL Senem KUMOVA-METİN2 Operators used for flow of control: REVIEW  Relational  Less than:<  Greater than:>  Less than or equal:<=  Greater than or equal:>=  Equality  Equal:==  Not equal:!=  Logical  Negation:!  Logical and:&&  Logical or:||

CS115 FALL Senem KUMOVA-METİN3 Operator Precedence : REVIEW Operator Associativity () ++(postfix) - - (postfix) Left to right +(unary) -(unary) ++(prefix) - -(prefix) ! Right to left * / % Left to right + - Left to right >= Left to right == != Left to right && Left to right || Left to right ?: Right to left = += -= *= /= etc. Right to left, Left to right

CS115 FALL Senem KUMOVA-METİN4 Relational Operators : REVIEW, =  EXAMPLE 1: int x=1, y=0; int z=x>y; //???? printf(“%d %d”, z, x<y);  EXAMPLE 2: int x=10,y=4; int z= x-y<5; // which one has // higher precedence? printf(“%d”,z);

CS115 FALL Senem KUMOVA-METİN5 Relational Operators : REVIEW, =  EXAMPLE 3: int x=7; int z=3<x<5; //Remember //associativity!! //LEFT TO RIGHT ((3<x)<5) printf(“%d %d”, z, 3<x<5);

CS115 FALL Senem KUMOVA-METİN6 Equality Operators: REVIEW  Equal:== expr1==expr2  TRUE or FALSE  Not equal:!= expr1!=expr2  TRUE or FALSE  EXAMPLE : int x=0; printf(“%d %d”, x==1, x!=0);

CS115 FALL Senem KUMOVA-METİN7 Logical Operators: REVIEW  Negation:!  Logical and:&&  Logical or:||

CS115 FALL Senem KUMOVA-METİN8 Logical Operators: REVIEW  EXAMPLE 1: int x=1; int z= !x-1; int t= !!x; printf(“%d %d”, z, t);  EXAMPLE 2: int x=5; printf("%d %d", !x, !!x);

CS115 FALL Senem KUMOVA-METİN9 Logical Operators: REVIEW  EXAMPLE 3: int x=1, y=0; printf(“%d %d”, x&&y, x&&y&&y); printf(“%d %d”, x||y, x||y||y);  EXAMPLE 4: int x=4, y=3; printf(“%d %d”, x&&y, x&&y&&y); //1 1 printf(“%d %d”, x||y, x||y||y); //1 1

CS115 FALL Senem KUMOVA-METİN10 Short Circuit Evaluation (With operands &&, ||)  expression1 && expression2 If expression1 is FALSE, No need to check the other, RESULT will be FALSE  expression1 || expression2 If expression1 is TRUE, No need to check the other, RESULT will be TRUE SHORTCIRCUITSHORTCIRCUIT

CS115 FALL Senem KUMOVA-METİN11 Short Circuit Evaluation (With operands &&, ||) EXAMPLE: cnt =3; if(cnt>4 && (c=getchar())!=EOF) {…… } /* cnt>4 will return FALSE so c=getchar())!=EOF expression will never be evaluated */

CS115 FALL Senem KUMOVA-METİN12 Compund Statements  Series of declarations and statements surrounded by braces EXAMPLE 1: int a =1,b=1,c=1; { a+=b+=c; // b=b+c; a=a+b; printf(“%d %d %d”, a,b,c); } EXAMPLE 2: { a=1; { b=2; c=3; } }

CS115 FALL Senem KUMOVA-METİN13 The if statements 1 if(expression) statement; 2 if(expression) statement_1; else statement_2; 3 if(expression_1) statement_1; else if (expression _2) statement_2; else if (expression_3) statement_3;. else statement_n;

CS115 FALL Senem KUMOVA-METİN14 if without else /* DOES NOT CARE ON THE ELSE CONDITION */ #include #include // for exit() function main() { int x; printf(“Type a number or Press 0 to exit “); scanf(“%d”, &x); if(x==0) { printf(“ eXiting.....\n ”); exit(0); }

CS115 FALL Senem KUMOVA-METİN15 if with else main() { int x; printf(“Type 0 for addition or 1 for subtraction\n“); scanf(“%d”, &x); if(x==0) { printf( “ PERFORM ADDITION...”\n) ;} else { printf( “ PERFORM SUBTRACTION...”\n);} }

CS115 FALL Senem KUMOVA-METİN16 if- else if - else /* IF YOU HAVE MORE THAN 2 CASES */ scanf(“%d”,&x); if(x==0) { printf("x is 0\n"); } else if(x==1) { printf("x is 1\n"); } else if(x==2) { printf("x is 2\n"); } else if(x==3) { printf("x is 3\n"); } else {printf(“ wrong number !!”)}......

CS115 FALL Senem KUMOVA-METİN17 EXAMPLE : if- else if – else Calculator’s Menu int a=9,b=5,c; char x; scanf(“%c”,&x); if(x==‘A’) { printf(“ADDITION\n"); c=a+b; } else if(x==‘S’) { printf(“SUBTRACTION\n"); c=a-b; } else if(x==‘M’) { printf(“MULTIPLICATION\n“); } else if(x==‘D’) { printf(“DIVISION\n"); } else {printf(“ wrong number !!”); }......

CS115 FALL Senem KUMOVA-METİN18 Nested if Statement EXAMPLE 1: if(expression1) { if(expression2) { statement2 } statement1 } /* if expression1 is TRUE, statement1 will be evaluated whether expression 2 is TRUE or FALSE */ /* if expression1 and expression 2 are TRUE, statement2 will be evaluated */ EXAMPLE 2: if(expression1) { if(expression2){ statement1 } else { statement2 } } else { statement3 }

CS115 FALL Senem KUMOVA-METİN19 Example 1: Find if x is a multiple of 2 and 5 #include int main() { int a; printf("Input an integer and push return:\n"); scanf("%d", &a); if (a%2==0) { if(a%5==0) printf("%d is a multiple of 2 and 5\n", a); else printf("%d is only a multiple of 2\n",a); } else {if(a%5==0) printf("%d is only a multiple of 5\n",a); else printf("%d is not a multiple of 2 or 5\n",a); } return 0; }

CS115 FALL Senem KUMOVA-METİN20 Example 2: Find if x is a multiple of 2 and 5 #include int main() { int a; printf("Input an integer and push return:\n"); scanf("%d", &a); if (a%2==0 && a%5==0) { /* Start of if block */ printf("%d is a multiple of 2 and 5\n", a); } else { /* This is the else branch */ printf("%d is not a multiple of both 2&5\n", a); } return 0; }

CS115 FALL Senem KUMOVA-METİN21 CONDITIONAL OPERATOR : REVIEW expression1?expression2:expression3 same as if(expression1) expression2 else expression3

CS115 FALL Senem KUMOVA-METİN22 CONDITIONAL OPERATOR : EXAMPLE y = (x < 5) ? 5 : 10; if (x < 5) y = 5; else y = 10;

CS115 FALL Senem KUMOVA-METİN23 The switch Statement A multiway conditional statement generalizing the if else statement switch (expression) { case expr1: /*one or more statements*/ case expr2: /*one or more statements*/ case expr3: /*one or more statements*/ /*...more cases if necessary */ default: /* do this if all other cases fail */ }

CS115 FALL Senem KUMOVA-METİN24 The switch Statement : Example1 int x; if(x==1) printf(“x equals to 1”); else if(x==3) printf(“x equals to 3”); else if(x==4) printf(“x equals to 4”); else printf(“x does not equal to 1,3 or 4”); switch (x) // switch and check if x equals to any case { case 1 : printf(“x equals to 1”); case 3 : printf(“x equals to 3”); case 4 : printf(“x equals to 4”); default: printf(“x does not equal to 1,3 or 4”); }

CS115 FALL Senem KUMOVA-METİN25 The switch Statement: Example2 char x; if(x==‘A’) printf(“x equals to A”); else if(x==‘B’) printf(“x equals to B”); else if(x==‘C’) printf(“x equals to C”); else printf(“x does not equal to A,B or C”); switch(x) // switch and check if x equals to any case {case ‘A’: printf(“x equals to A”); case ‘B’: printf(“x equals to B”); case ‘C’: printf(“x equals to C”); default : prinf(“x does not equal to A,B or C”); }

CS115 FALL Senem KUMOVA-METİN26 Example: switch Statement  #include main() { int i; printf ("Enter a positive integer from 1 to 4: "); scanf("%d", &i); switch(i) { case 1: printf(“You have entered 1 \n"); case 2: printf(“You have entered 2 \n"); case 3: printf(“You have entered 3 \n"); case 4: printf(“You have entered 4 \n"); default: printf(“not 1,2,3 or 4\n"); } OUTPUT ???

CS115 FALL Senem KUMOVA-METİN27 Example: switch Statement  You'll notice that the program will select the correct case but will also run through all the cases below it (including the default) until the switch block's closing bracket is reached.  To prevent this from happening, we'll need to insert another statement into our cases...  BREAK

CS115 FALL Senem KUMOVA-METİN28 Example: switch Statement #include main() { int i; printf ("Enter a positive integer from 1 to 4: "); scanf("%d", &i); switch(i) {case 1: printf(“You have entered 1 \n"); break; case 2: printf(“You have entered 2 \n"); break; case 3: printf(“You have entered 3 \n"); break; case 4: printf(“You have entered 4 \n"); break; default: printf(“not 1,2,3 or 4\n"); }

CS115 FALL Senem KUMOVA-METİN29 The break statement /* BREAK CAUSES AN EXIT FROM THE INNERMOST ENCLOSING LOOP OR SWITCH STATEMENT */ double x; while(1) // an infinite loop { scanf(“%lf”,&x); if(x<0.0) break; /* exit loop if x is negative */ printf(“%f\n”,fsqrt(x)); } // break jumps to here