Dr. Sajib Datta Sep 3, 2014.  A new operator used in C is modulus operator: %  % only used for integers, not floating-point  Gives the integer.

Slides:



Advertisements
Similar presentations
1 Conditional Statement. 2 Conditional Statements Allow different sets of instructions to be executed depending on truth or falsity of a logical condition.
Advertisements

Conditionals – if - else Dr. Sajib Datta
1 Objectives You should be able to describe: Relational Expressions The if-else Statement Nested if Statements The switch Statement Common Programming.
Conditions What if?. Flow of Control The order of statement execution is called the flow of control Unless specified otherwise, the order of statement.
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 (!)
true (any other value but zero) false (zero) expression Statement 2
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.
ECE122 L7: Conditional Statements February 20, 2007 ECE 122 Engineering Problem Solving with Java Lecture 7 Conditional Statements.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Programming Control Flow. Sequential Program S1 S2 S5 S4 S3 int main() { Statement1; Statement2; … StatementN; } Start End.
Boolean Expressions and If Flow of Control / Conditional Statements The if Statement Logical Operators The else Clause Block statements Nested if statements.
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
Algorithms and Computing Lecture 3 Control Statements By Dr. M. Tahir Khaleeq.
CONTROL STATEMENTS IF-ELSE, SWITCH- CASE Introduction to Computer Science I - COMP 1005, 1405 Instructor : Behnam Hajian
Conditional Statement
Lecture 4 C Program Control Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
More on Input Output Input Stream : A sequence of characters from an input device (like the keyboard) to the computer (the program running). Output Stream.
CMSC 104, Version 8/061L11Relational&LogicalOps.ppt Relational and Logical Operators Topics Relational Operators and Expressions The if Statement The if-else.
Programming in Java (COP 2250) Lecture 11 Chengyong Yang Fall, 2005.
2 Objectives You should be able to describe: Relational Expressions Relational Expressions The if-else Statement The if-else Statement Nested if Statements.
1 Lecture 5: Selection Structures. Outline 2  Control Structures  Conditions  Relational Operators  Logical Operators  if statements  Two-Alternatives.
5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,
Basic Concepts – Conditionals Conditionals are used for making decisions. The conditionals available in C are if and if-else statements the switch statement.
Flow of Control Part 1: Selection
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.
Operators Precedence - Operators with the highest precedence will be executed first. Page 54 of the book and Appendix B list C's operator precedence. Parenthesis.
Lesson - 5. Introduction While programming, we usually need to decide the path of the program flow according to the parameters and conditions. Actually.
If Statements Programming. COMP104 Lecture 7 / Slide 2 Review: Rules for Division l C++ treats integers different than doubles. 100 is an int. l 100.0,
Gator Engineering Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 3 Formatted Input/Output.
CSE 1320 Basics Dr. Sajib Datta
CS115 FALL Senem KUMOVA-METİN1 CHAPTER 4 FLOW OF CONTROL-1 Operators, If and switch statements.
Dr. Sajib Datta Jan 21,  Declare a variable ◦ int height; [note that no value is still assigned]  Assign a variable a value ◦ height =
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.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
Dr. Sajib Datta Sep 8,  char type technically is an integer type  Computer uses numeric codes to represent characters, and store characters.
Chapter 4 – C Program Control
Operators And Expressions
Selections Java.
Lecture 3- Decision Structures
EGR 2261 Unit 4 Control Structures I: Selection
Chapter 3 Control Statements Lecturer: Mrs Rohani Hassan
CSE1320 INTERMEDIATE PROGRAMMING Operators+Conditionals+Loop
Conditionals & Boolean Expressions
Relational and Logical Operators
Relational and Logical Operators
Chapter 4 - Program Control
Basic Concepts – Conditionals
CSC215 Lecture Flow Control.
CSC215 Lecture Control Flow.
Associativity and Prescedence
Relational, Logical, and Equality Operators
Chapter 4: Control Structures I (Selection)
Relational and Logical Operators
Week 3 – Program Control Structure
Relational and Logical Operators
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
CSC215 Lecture Control Flow.
Controlling Program Flow
Presentation transcript:

Dr. Sajib Datta Sep 3, 2014

 A new operator used in C is modulus operator: %  % only used for integers, not floating-point  Gives the integer remainder from integer division.  Example: ◦ int a = 7, b = 3; ◦ int answer; ◦ answer = a % b;

The following assignment operators are available in C:  += addition  -= subtraction  *= multiplication  /= division  num = num + 2 same as num+=2;  num = num - 1 same as num-=1;

 What is the output of ‘x’? ◦ int x; ◦ x = 10; ◦ x += x; ◦ printf(“%d”, x);

 We discussed: ◦ Omega ◦ Variable  Declaration, assigning a value, initialization ◦ Statements  Conditional statements can change the flow of the code ◦ Operators  +,-,*,% ◦ Data type  What happens when you assign a float/double to a variable of type int

 A precedence for each operator ◦ Multiplication and division have a higher precedence than addition and subtraction  For operators with the same precedence, if they share an operand, they are executed according to the order they occur in the statement. (In most cases, from the left to the right, except assignment operation)  Examples: ◦ int num, x; ◦ num = 2; ◦ x = 1 + 3*num/4; What is the value of x?

 Want an addition operation to take place before division? ◦ int num, x; ◦ num = 2; ◦ x = (1 + 3)*num/4;  What is x now?  int a = 1, b = 4;  b += a+2; is equivalent to b = b + (a + 2)

 < : is less than  <= : is less than or equal to  == : is equal to  >= : is greater than or equal to  > : is greater than  != : is not equal to  Example: ◦ Relational expression: a > 10  If the relation is true, the value of the expression is 1, otherwise 0.

 A combination of operators and operands, where operands can be constants, variables, or combinations of the two  4  4+21  a = (b+c)/2  q>4

 A statement is a complete instruction to the computer  In C, it is indicated by semicolon  An statement is consists of expressions

 ++ and –-  The operand must be a variable  Two varieties – prefix and postfix  ++ a, --b  a ++, b --  This operation increases(decreases) the value of its operand by just one!  In the prefix form, the increment or decrement takes place before the value is used in expression evaluation ◦ so the value of the expression is different from the value of the operand.  In the postfix form, the increment or decrement takes place after the value is used in expression evaluation ◦ so the value of the expression is the same as the value of the operand

 Case1: ◦ a = 3; ◦ b = 2* ++a;// the value of the expression ++a is the new value of a, therefore b = ?  Case2: ◦ a = 3; ◦ b = 2* a++;// the value of the expression a++ is the old value of a, therefore, b = ?

 Conditionals

 if statement gives you choice of either executing a statement or skipping it  The basic format of the if statement is ◦ if (condition_is_true) do_something; // condition_is_true should be an expression //need the parenthesis for the expression // If expression evaluates to true (nonzero), do something. Otherwise, it is skipped. Normally, expression is relational expression. But in general, any expression will work since it has a value, which can be mapped to true or false.   Examples  int x;  int y = 6;  scanf (“%d”, &x);  if (x >= 2)  y = 10;  printf(“y is %d.\n”, y);

 Examples int x; int y = 6; scanf (“%d”, &x); if (x >= 2) y = 10; printf(“y is %d.\n”, y);

 Handle more than one statements when the condition is true  Create a block of statements by using braces.  Example if (x >= 2) { y = 10; printf("y is now %d", y); } next statement;  with one statement, you can use “{}”, but it’s not necessary.  without the braces what’s going to happen here?

 To do one thing if a condition is true but another if the condition is false –  if-else statement: ◦ Basic format of if-else statement if (expression_is_true) do_something (if statement); else do_something_else (else statement);

 Example: int x = 10; if(x>=2) printf("x>=2\n"); else printf("x<2\n"); your next statement;

 else statement must have an if statement to match  Not allowed to have any statement between if statement and else statement (except the cases of nested if-else)  Example: (a wrong program!) int x = 4; int y; if (x >= 2) y = 10; printf("y is now 10.\n"); else printf("y is not assigned.\n");

 else statement can also be a block of statements, but remember to give “{ }”  Example: if(x >= 2) { y = 10; printf("y is now 10.\n"); } else { y = 20; printf("y is now 20.\n"); }

 Have a second conditional statement in when the first condition is true  if statements can be nested  Example : if ( x < 10 ) if( y < 3 ) z = x + y; your next statement comes here;  To assign a value of (x+y) to z only if x > 10 and y < 3

 /* indentation is used to show correct logic; the else goes with the nearest unmatched f if statement within a block, where block is defined by using braces.*/  Examples: (nested in if statement )  (1 ) if (x == 13)  if (y == 52)  printf("Test1.\n");  else  printf("Test2.\n");  printf("Test3.\n");  (2 ) if (x == 13)  if (y== 52)  printf("Test1.\n");  else  printf("Test2.\n");  printf("Test3.\n"); Let’s say x=13, y=53…what are the outputs in 3 cases  (3) if (x == 13)  if (y== 52)  printf("Test1.\n");  else  printf("Test2.\n");  printf("Test3.\n");

 (4) if (x == 13)  {  if (y == 52)  printf("Test1.\n");  else  printf("Test2.\n");  }  printf("Test3.\n");   (5) if (x == 13)  {  if (y== 52)  printf("Test1.\n");  }  else  printf("Test2.\n");  printf("Test3.\n");  (1)(2)(3)(4) are the same, but not (5)  (2) and (3) are not good program styles, you should use indent to convey your code block, like what (1) does, or use the braces like the (4) example.

 Examples: (nested in else statement)  (1) if (x == 13)  printf(“x == 13.\n”, x);  else if (x < 13)  printf(“x < 13.\n”);  else  printf(“x > 13.\n”);  (2) if (x == 13)  printf(“x == 13.\n”, x);  else  if (x < 13)  printf(“x < 13.\n”);  else  printf(“x > 13.\n”);  (1) and (2) are the same.

 Any expression that evaluates to a nonzero value is considered true.  Examples:  if (-3.5)  printf("non-zero values are true\n"); // this will be printed.  else  printf("this never prints\n");  if (0)  printf("zero is false\n");  else  printf("this is always false\n"); // this will be printed.  WARNING: “=” and “==”  ◦ Example:   if (a = 2)  printf("a is equal to 2 forever.\n"); // this will be printed.  else  printf("This statement will never be executed.\n");

 && and  Exp1 && exp2 is true only if both exp1 and exp2 are true  || or  Exp1 || exp2 is true if either exp1 or exp2 is true or if both are true  ! not  ! Exp1 is true if exp1 is false, and it’s false if exp1 is true

 Examples: ◦ 4<3 && 2<9 true/false? ◦ 5!=5 || 4<19 true/false? ◦ !(x<9) assume x = 8, true/false?  Operator precedence: ◦ relational operation have higher precedence over logical operation, except the logical operation “!”  parentheses have the highest precedence

 Example: if ( x < 10 ) if( y < 3 ) z = x + y; your next statement comes here;  the same as if (x < 10 && y < 3) z = x + y; your next statement comes here;

 To test two condition expressions, you have to use a logical operator to connect them.   #include  int main(void)  {  int x;  scanf("%d",&x);  if(3<x<6) // wrong!!!, to correct ( 3<x && x<6)  printf("if statement\n");  else  printf("else statment\n");  return 0;  }  Test with x = 7

 An if-else statement is used for binary decisions–those with two choices, while switch statement is intended for more than two choices.  switch (expression)  {  case label1: do statements1 // there is a space between case and label  case label2: do statements2  ·  case labeln: do statementsn  default: do default statements (optional)  }  expression should be an integer value (including type char).  Labels must be constants (integer constants or char constants).

 The program scans the list of labels until it finds one matching that value.  If there is no matching, while there is “default” key word, the statements associated with “default” will be executed.  If there is no matching, and there is no “default” either, the program will jump out of switch statement. The statement after switch statement will be executed.

 If a matching is found, the corresponding statement, and all following labels are executed until flow reaches end of switch  int x;  scanf("%d", &x);  switch(x)  {  case 1: printf("freshman\n");  case 2: printf("sophomore\n");  case 3: printf("junior\n");  case 4: printf("senior\n");  default: printf("graduates\n");  }  printf("out of switch now.\n");  What happens when x=1? All statements will be executed  If x=4, it will print senior graduates

 When you see “break”, the program will jump out of the switch statement when reaching the break.  int x;  scanf("%d", &x);  switch(x)  {  case 1: printf("freshman\n");  break;  case 2: printf("sophomore\n");  case 3: printf("junior\n");  break;  case 4: printf("senior\n");  default: printf("graduates\n");  }  printf("out of switch now.\n");  What happens with x=1? x=4?

 int x;  scanf("%d", &x);  switch(x)  {  case 1:  case 2: printf("sophomore\n");  case 3: printf("junior\n");  break;  case 4: printf("senior\n");  default: printf("graduates\n");  }  printf("out of switch now.\n");  What happens with x=1? x=4?

 int x;  scanf("%d", &x);  switch(x)  {  case 1: printf("freshman\n");  printf(“redundant freshman\n");  case 2: printf("sophomore\n");  case 3: printf("junior\n");  break;  case 4: printf("senior\n");  default: printf("graduates\n");  }  printf("out of switch now.\n");  What happens with x=1? x=4?

 if(a == 1) ◦ printf(“a = 1”);  else ◦ {  if(b == 3)  printf(“b = 3”); ◦ }

 if(a == 3 || b == 1) ◦ printf(“if is true”);  else ◦ printf(“else is true”);

 switch(n)  {  case 1:  case -1: printf(“UTA”);  break;  case 50:  case 75:  case 100: printf(“CSE”);  break;  case 120:  switch(m)  {  case 50: printf(“1320”);  break;  default: break;  }  default:  printf(“sec 001”);  break;  }