Conditionals – if - else Dr. Sajib Datta

Slides:



Advertisements
Similar presentations
Lecture 2 Introduction to C Programming
Advertisements

Introduction to C Programming
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
Introduction to C Programming
True or false A variable of type char can hold the value 301. ( F )
3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 1 Learning the C++ language 3. The Nuts and Bolts of C++ (4) 10 September.
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
CS1061 C Programming Lecture 5: Building Blocks of Simple Programs A. O’Riordan, 2004.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
ECE122 L3: Expression Evaluation February 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 3 Expression Evaluation and Program Interaction.
1 Operators And Expressions. 2 Operators Arithmetic Operators Relational and Logical Operators Special Operators.
Expressions and statements Applications of Computer Programming in Earth Sciences Instructor: Dr. Cheng-Chien LiuCheng-Chien Liu Department of Earth Sciences.
C programming an Introduction. Types There are only a few basic data types in C. char a character int an integer, in the range -32,767 to 32,767 long.
1 Midterm Review COMP 102. Tips l Eat a light meal before the exam l NO electronic devices (including calculators, dictionaries, phones, pagers, etc.)
Introduction to C Programming
1 Expressions, Operators Expressions Operators and Precedence Reading for this class: L&L, 2.4.
Boolean Expressions and If Flow of Control / Conditional Statements The if Statement Logical Operators The else Clause Block statements Nested if statements.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved The switch Multiple-Selection Statement switch.
Performing Computations C provides operators that can be applied to calculate expressions: example: tax is 8.5% of the total sale expression: tax =
Operators and Expressions
CSCI 130 Chapter 4 Statements, Expressions, and Operators.
Chapter 4: Basic C Operators
Expressions, Data Conversion, and Input
Primitive Data Types and Operations Identifiers, Variables, and Constants Primitive Data Types Byte, short, int, long, float, double, char, boolean Casting.
 Input and Output Functions Input and Output Functions  OperatorsOperators Arithmetic Operators Assignment Operators Relational Operators Logical Operators.
2440: 211 Interactive Web Programming Expressions & Operators.
Lecture 4 C Program Control Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
CMSC 104, Version 8/061L11Relational&LogicalOps.ppt Relational and Logical Operators Topics Relational Operators and Expressions The if Statement The if-else.
Basic Concepts – Conditionals Conditionals are used for making decisions. The conditionals available in C are if and if-else statements the switch statement.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
Assignment Statements Operator Precedence. ICS111-Java Programming Blanca Polo 2 Assignment, not Equals  An assignment statement changes the value of.
Introduction to C Programming Angela Chih-Wei Tang ( 唐 之 瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan 2010 Fall.
C Programming Lecture 6 : Operators Lecture notes : courtesy of Ohio Supercomputing Center, and Prof. Woo and Prof. Chang.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
If…else statements. Boolean Expressions Boolean expression - An expression whose value is either true or false true = 1 false = 0 Datatype: boolean.
Lesson - 7. Operators There are three types of operators: Arithmetic Operators Relational and Equality Operators Logical Operators.
C Operators. CONTENTS C OPERATORS TYPES OF OPERATOR UNARY BINARY TERNARY ARITHMATIC RELATIONAL LOGICAL.
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.
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
1 Expressions. 2 Variables and constants linked with operators  Arithmetic expressions Uses arithmetic operators Can evaluate to any value  Logical.
Principles of Programming Chapter 4: Basic C Operators  In this chapter, you will learn about:  Arithmetic operators  Unary operators  Binary operators.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
Gator Engineering Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 3 Formatted Input/Output.
Expressions and Order of Operations Operators – There are the standard operators: add, subtract, divide, multiply – Note that * means multiply? (No times.
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
CSE 1320 Basics Dr. Sajib Datta
C Language 1 Program Looping. C Language2 Topics Program looping Program looping Relational operators / expressions Relational operators / expressions.
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.
Design A software design specifies how a program will accomplish its requirements A design includes one or more algorithms to accomplish its goal.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
Dr. Sajib Datta Sep 3,  A new operator used in C is modulus operator: %  % only used for integers, not floating-point  Gives the integer.
Principles of Programming - NI July Chapter 4: Basic C Operators In this chapter, you will learn about: Assignment operators Arithmetic operators.
Chapter 4 – C Program Control
Operators And Expressions
- Standard C Statements
Lecture 3- Decision Structures
CSE1320 INTERMEDIATE PROGRAMMING Operators+Conditionals+Loop
Arithmetic Operator Operation Example + addition x + y
Structure of a C Program
Chapter 4 - Program Control
C Operators, Operands, Expressions & Statements
Basic Concepts – Conditionals
Relational Operators.
Chapter 4 - Program Control
OPERATORS in C Programming
OPERATORS in C Programming
3.0 - Design A software design specifies how a program will accomplish its requirements A design includes one or more algorithms to accomplish its goal.
Presentation transcript:

Conditionals – if - else Dr. Sajib Datta

Fundamental Operators A new operator used in C is modulus operator: %. % only used for integers, not floating-point Return the integer remainder from integer division. ▫Example: int topnum = 7, bottomnum = 3; int answer; answer = topnum % bottomnum; //answer has a value of 1.

Assignment Operators The following assignment operators are available in C: ▫+= addition ▫-= subtraction ▫*= multiplication ▫/= division num = num + 2 OR num+=2; num = num - 1 OR num-=1;

Relational Operators < : 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.

Expressions 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 Every expression has a value in C “=” – the value of the left side ▫the value of a = 2 is 2 “>” – relational operator ▫has value 1if true, and 0 if false

Difference between Expressions and Statements A statement is a complete instruction to the computer In C, it’s indicated by semicolon A complete instruction is not necessarily a statement ▫E.g., x = 5 + (y = 4); Different from statements ▫y = 4 // expression ▫y = 4; // statement (assignment statement)

Increment and Decrement Operators ++ 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! Case1: ▫a = 3; ▫b = 2* ++a;// the value of the expression ++a is the new value of a, therefore b = 8 Case2: ▫a = 3; ▫b = 2* a++;// the value of the expression a++ is the old value of a, therefore, b = 6

If Statements 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);

Indent Style in C Programming indent style - a convention governing the indentation of blocks of code to convey the program's structure Visual Studio manages indent automatically by default Manually, “Tab” key on the keyboard will help

If Statements cont. 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?

If and else Statements 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;