Lecture-2 Operators and Conditionals. Variables(again???) Type: Representation of “bits” in memory Variables: Name for a memory object. Starts with letters,

Slides:



Advertisements
Similar presentations
Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 2 Simple C Programs.
Advertisements

Computer Programming w/ Eng. Applications
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
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.
C Lecture Notes 1 Program Control (Cont...). C Lecture Notes 2 4.8The do / while Repetition Structure The do / while repetition structure –Similar to.
Basic C Programming Data Types and Arithmetic Operations 01/30/15.
CS1061 C Programming Lecture 5: Building Blocks of Simple Programs A. O’Riordan, 2004.
1 Introduction to Computers and Programming Class 3 Introduction to C Professor Avi Rosenfeld.
1 Arithmetic in C. 2 Type Casting: STOPPED You can change the data type of the variable in an expression by: (data_Type) Variable_Name Ex: int a = 15;
ICS 103 Lab 2-Arithmetic Expressions. Lab Objectives Learn different arithmetic operators Learn different arithmetic operators Learn how to use arithmetic.
1 Lab Session-III CSIT-120 Spring 2001 Revising Previous session Data input and output While loop Exercise Limits and Bounds GOTO SLIDE 13 Lab session.
Programming with MATLAB. Relational Operators The arithmetic operators has precedence over relational operators.
Admin Office hours 2:45-3:15 today due to department meeting if you change addresses during the semester, please unsubscribe the old one from the.
1  Ex: Declare a variable to store user’s age: int age; Prompt the user to enter his/her age: printf (“ How old are you? “); Read / scan the entered value.
12-2 Know how if and switch C statements control the sequence of execution of statements. Be able to use relational and logical operators in the conditional.
EG280 - CS for Engineers Chapter 2, Introduction to C Part I Topics: Program structure Constants and variables Assignment Statements Standard input and.
C Programming Lecture 3. The Three Stages of Compiling a Program b The preprocessor is invoked The source code is modified b The compiler itself is invoked.
1 Lab Session-III CSIT-120 Fall 2000 Revising Previous session Data input and output While loop Exercise Limits and Bounds Session III-B (starts on slide.
 Input and Output Functions Input and Output Functions  OperatorsOperators Arithmetic Operators Assignment Operators Relational Operators Logical Operators.
2440: 211 Interactive Web Programming Expressions & Operators.
Agenda  Commenting  Inputting Data from Keyboard (scanf)  Arithmetic Operators  ( ) * / + - %  Order of Operations  Mixing Different Numeric Data.
Lecture 8: Choosing the Correct Loop. do … while Repetition Statement Similar to the while statement Condition for repetition only tested after the body.
C STRUCTURES. A FIRST C PROGRAM  #include  void main ( void )  { float height, width, area, wood_length ;  scanf ( "%f", &height ) ;  scanf ( "%f",
CSE1222: Lecture 2The Ohio State University1. mathExample2.cpp // math example #include using namespace std; int main() { cout
A First C Program /* Print a Message */ #include main() { printf("This is a test!\n"); } The program is compiled and run as cc pg32.c  a.out  This is.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
NA2204.1jcmt CSE 1320 Intermediate Programming C Program Basics Structure of a program and a function type name (parameters) { /* declarations */ statement;
Chapter 3. Outline Relational Operators Loops Decisions Logical Operators Precedence Summary.
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.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X1 Chapter 3 Control Statements.
Topic:  Reading Input  identifier,  if statement,  Arithmetic Operation CSE 102 – Programming Fundamentals.
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
CSCI 171 Presentation 3. Operators Instructs C to perform some operation Assignment = Mathematical Relational Logical.
Gator Engineering Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 3 Formatted Input/Output.
Lecture 7: Menus and getting input. switch Multiple-selection Statement switch Useful when a variable or expression is tested for all the values it can.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 2 September 3, 2009.
Java Basics. Tokens: 1.Keywords int test12 = 10, i; int TEst12 = 20; Int keyword is used to declare integer variables All Key words are lower case java.
Operators A binary operator combines two values to get one result: x OP y where OP is any binary operators such as +, -, *, /, ==, !=, >, &&, or even =.
Dr. Sajib Datta Jan 21,  Declare a variable ◦ int height; [note that no value is still assigned]  Assign a variable a value ◦ height =
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
CC213 Programming Applications Week #2 2 Control Structures Control structures –control the flow of execution in a program or function. Three basic control.
OPERATORS IN C CHAPTER 3. Expressions can be built up from literals, variables and operators. The operators define how the variables and literals in the.
Simple C Programs.
Decision making If.. else statement.
Chapter 3 Control Statements
ECE Application Programming
BASIC ELEMENTS OF A COMPUTER PROGRAM
INC 161 , CPE 100 Computer Programming
BIL 104E Introduction to Scientific and Engineering Computing
Lecture 7: Repeating a Known Number of Times
Intro to C Tutorial 4: Arithmetic and Logical expressions
Chapter 3 Control Statements Lecturer: Mrs Rohani Hassan
Introduction to C Programming
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Chapter 4 - Program Control
Introduction to C Programming
Structured Program Development in C
Chapter 2 - Introduction to C Programming
Conditionals.
Conversion Check your class notes and given examples at class.
Relational, Logical, and Equality Operators
SELECTIONS STATEMENTS
Chapter 2 - Introduction to C Programming
EECE.2160 ECE Application Programming
Introduction to C Programming
Problem 1 Given n, calculate 2n
Presentation transcript:

Lecture-2 Operators and Conditionals

Variables(again???) Type: Representation of “bits” in memory Variables: Name for a memory object. Starts with letters, and may contain letters, numerals and underscores Declaration: Start of program block. Combination of type and variable Assignment: Value for the variable

Example int a, b; /* Declaration */ int a = 9; /* Decl. with Initial Value */ b = 2; /* Assignment/Definition */ type name

scanf() Function Input to the program can be done through scanf() function Format is same as printf(), except for variable used is attached an “&” For e.g. scanf(“%d”, &i);

Arithmetic Operators +Addition -Subtraction *Multiplication /Division %Remainder unary + unary -Negate

test1.c Follow this link to test1.c on the classtest1.c web page

Math Library(math.h) cos(x)cos x sin(x)sin x tan(x)tan x pow(x, y)xyxy sqrt(x)Square root of x exp(x) xx log(x)ln x Note: For Trigonometric functions, angles are in radians

hypot.c Follow this link to hypot.c on the classhypot.c web page

In class Exercise -1 Write a program to calculate the Area of a triangle. Prompt the user to enter base and height Use “float” variables

Conditional Statements(if-else) if (expression) statement: Execute the statement if the expression is true if (expression) statement-1 else statement-2 Execute statement-1 if expression is true, else execute statement-2

Relational Operators Relational operators return 1 if the condition is true, and 0 if the condition is false. They are used in expressions for conditionals ==Equals !=Not equal to >Greater than <Less than >=Greater than or equal <=Less than or equal

Logical Operators ! operator: !(expression) inverts the value of the expression. For example: !0=1, !2.0=0 && operator: expression1 && expression2 is true if and only if both expressions are true || operator: expression1 || expression2 is true if any one expression is true

grader.c Follow this link to grader.c on the classgrader.c web page

Switch-case statement Switch (expression) { case value1: statements1; break; case value2: statements2; break; default: default statements; break; } Expression is an integer expression, and are matched against Case values which also must be integers!

calculator.c Follow this link to calculator.c on the classcalculator.c webpage

In class Exercise - 2 Modify the calculator.c program for floating point variables. You should support all the operations as the original program. Your program should print out an error message incase of “divide by zero” error, and exit gracefully.

Homework-2 Follow this link to Homework-2 on the class webpageHomework-2