Increment Operator To increment a variable X by 1. X+1;X+=;++X; X = 10 Y = X + 1;Y = 11 X += 1;X = 11 Y = ++X + 4;Y = 15.

Slides:



Advertisements
Similar presentations
INTEC CS160 - Jeanine Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 3 Control Structures and Data Files.
Advertisements

Java Control Statements
Switch code for Lab 4.2 switch (input) { /* input is a variable that we will test. */ case 'M': printf("The prefix is equal to 1E6.\n"); break; case 'k':
Introduction to C Programming
Repetition Statements Perform the same task repeatedly Allow the computer to do the tedious, boring things.
Conditionals – if - else Dr. Sajib Datta
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
IntroductionIntroduction  Computer program: an ordered sequence of statements whose objective is to accomplish a task.  Programming: process of planning.
True or false A variable of type char can hold the value 301. ( F )
1 Engineering Problem Solving With C++ An Object Based Approach Chapter 3 Control Structures.
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
1 Objectives You should be able to describe: Relational Expressions The if-else Statement Nested if Statements The switch Statement Common Programming.
CS150 Introduction to Computer Science 1
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
Lecture 1 The Basics (Review of Familiar Topics).
1 Midterm Review COMP 102. Tips l Eat a light meal before the exam l NO electronic devices (including calculators, dictionaries, phones, pagers, etc.)
Switch Statements. Switch Statement Often you want to do a series of tests –if i==0 … else if i==1 …. else if i==2 … else if i==3 …. C++ provides the.
C++ Operators CS242 COMPUTER PROGRAMMING T.Banan Al-Hadlaq.
Chapter 4: Basic C Operators
4. Python - Basic Operators
Chapter 4: Operators Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills /1436.
2 Objectives You should be able to describe: Relational Expressions Relational Expressions The if-else Statement The if-else Statement Nested if Statements.
Introduction to C++ // Program description #include directives int main() { constant declarations variable declarations executable statements return.
CONTROLLING PROGRAM FLOW
C Programming Lecture 6 : Operators Lecture notes : courtesy of Ohio Supercomputing Center, and Prof. Woo and Prof. Chang.
CSE 332: C++ execution control statements Overview of C++ Execution Control Expressions vs. statements Arithmetic operators and expressions * / % + - Relational.
Chapter 3. Outline Relational Operators Loops Decisions Logical Operators Precedence Summary.
CSIS 113A Lecture 3 Conditional & Switch Glenn Stevenson CSIS 113A MSJC.
Review the following: if-else One branch if Conditional operators Logical operators Switch statement Conditional expression operator Nested ifs if –else.
Chapter 05 (Part III) Control Statements: Part II.
1 Compound Assignment C++ has a large set of operators for applying an operation to an object and then storing the result back into the object Examples.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
C++ Lecture 1 Friday, 4 July History of C++ l Built on top of C l C was developed in early 70s from B and BCPL l Object oriented programming paradigm.
VARIABLES, CONSTANTS, OPERATORS ANS EXPRESSION
Chapter 3 More Flow of Control Goals: To analyze the use of Boolean expressions To analyze the use of Boolean expressions To introduce the notion of enumerated.
Operators in JAVA. Operator An operator is a symbol that operates on one or more arguments to produce a result. Java provides a rich set of operators.
1 CS161 Introduction to Computer Science Topic #8.
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,
Expressions and Operators in C. Expressions and Operators Examples: 3 + 5; x; x=0; x=x+1; printf("%d",x); Two types: – Function calls – The expressions.
Python Mini-Course University of Oklahoma Department of Psychology Day 3 – Lesson 10 Iteration: Loops 05/02/09 Python Mini-Course: Day 3 - Lesson 10 1.
Chapter 4 October 22, The If Statement Programs make decisions If(condition){ Statement(s); } Condition  boolean expression Evaluates to either.
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
Repetition Statements (Loops). 2 Introduction to Loops We all know that much of the work a computer does is repeated many times. When a program repeats.
C Language 1 Program Looping. C Language2 Topics Program looping Program looping Relational operators / expressions Relational operators / expressions.
Chad’s C++ Tutorial Demo Outline. 1. What is C++? C++ is an object-oriented programming (OOP) language that is viewed by many as the best language for.
Structured Programming Structured Programming is writing a program in terms of only 3 basic control structures: sequence selection repetition We have already.
Dr. Sajib Datta Jan 23,  A precedence for each operator ◦ Multiplication and division have a higher precedence than addition and subtraction.
Inside Class Methods Chapter 4. 4 What are variables? Variables store values within methods and may change value as the method processes data.
Lecture 3.1 Operators and Expressions Structured Programming Instructor: Prof. K. T. Tsang 1.
1 Chapter 4 - Control Statements: Part 1 Outline 4.1 Introduction 4.4 Control Structures 4.5 if Selection Structure 4.6 if/else Selection Structure 4.7.
University of Central Florida COP 3330 Object Oriented Programming
Computing Fundamentals
A mechanism for deciding whether an action should be taken
Chapter 2 Assignment and Interactive Input
University of Central Florida COP 3330 Object Oriented Programming
Engineering Problem Solving with C++, Etter/Ingber
Chapter 5: Loops and Files.
Basic Notions Review what is a variable? value? address? memory location? what is an identifier? variable name? keyword? what is legal identifier? what.
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
Unary Operators ++ and --
3 Control Statements:.
If Statements.
Chapter 4: Control Structures I (Selection)
Introduction to Programming – 4 Operators
2.6 The if/else Selection Structure
Chap 7. Advanced Control Statements in Java
HNDIT11034 More Operators.
Presentation transcript:

Increment Operator To increment a variable X by 1. X+1;X+=;++X; X = 10 Y = X + 1;Y = 11 X += 1;X = 11 Y = ++X + 4;Y = 15

Increment Operator To increment a variable X by 1. X+1;X+=;++X; Operator before the variable are called prefix. X = 10 X = X + 1;X = 11 X += 1;X = 12 Y = ++X + 4;Y = 17

Example 6 #include int main() { int x, y; x=10;//initial value for x x=x+1;//increment x by 1 cout <<"x=" <<x <<endl; x+=1;//increment x by 1 cout <<"x=" <<x <<endl; y=++x+4;//increment x by 1 and add 4 cout <<"y=" <<y <<"\t and \t x=" <<x <<endl; return 0; }

Increment Operator The operator can also be written after the variable to which it applies. Operator before the variable are called postfix. Effect of postfix in operation is slightly different. The incrementing of the variable to which it applies occurs after its value is used in context.

Example 7 #include int main() { int x, y; x=10;//initial value for x x=x+1;//increment x by 1 cout <<"x=" <<x <<endl; x+=1;//increment x by 1 cout <<"x=" <<x <<endl; y=4 + x++ ;//add 4 to x and increment x by 1 cout <<"y=" <<y <<"\t and \t x=" <<x <<endl; return 0; }

Decrement Operator To decrement a variable X by 1. X = X X X-- -=X

Example 8 #include int main() { int x, y, z; x=10;//initial value for x x=x-1;//decrement x by 1 cout <<"x=" <<x <<endl; x-=1;//decrement x by 1 cout <<"x=" <<x <<endl; y=--x +4;//decrement x by 1 and add 4 cout <<"y=" <<y <<"\t and \t x=" <<x <<endl; z=4 + x--;//add 4 to x and decrement x by 1 cout <<"z=" <<z <<"\t and \t x=" <<x <<endl; return 0; }

Relational Operators <less than <=less than equal >greater than >=greater than equal ==equal to !=not_eqnot equal to

Logical Operators !notlogical NOT &&andlogical AND ||orlogical OR

Bitwise Operators Bitwise operators treat their operands as a series of individual bits rather than a numerical value. They work with integer or constants.

Bitwise Operators ~comp1one’s complement <<left shift >>right shift &bitandbitwise And ^xorbitwise Exclusive-OR |bitorbitwise OR

Example 9 #include main() { long letter1 =0x41, letter2 =0x5A, x=0, y=0, z=0; cout <<"letter1=" <<letter1 <<"\t\t letter2=" <<letter2; cout <<endl; x= ~letter1; y=letter1 & letter2; z=letter1 | letter2; cout <<"x= " <<x <<"\t\t y=" <<y <<"\t\t z=" <<z; cout <<endl; return 0; }

Assignment Operators =assignment +=addition update -=subtraction update *=multiplication update /=division update

Assignment Operators %=modulus update <<=left shift update >>=right shift update &=and_eqbitwise AND update |=or_eqbitwise OR update ^=xor_eqbitwise Exclusive-OR

If Statement The if statement enables the programmer to test for a condition and branch to different parts of the code depending on the result. If (expression) statement;

Example 10 #include main() { int w, x, y, z; cout <<"enter a value for w" << endl <<"w="; cin>>w; cout <<"enter a value for x" << endl <<"x="; cin>>x; if (w>x) { y=w+x; cout <<"w is greater than x" <<endl <<"y=w+x=" <<y <<endl; } if (w<x) { z=w-x; cout <<"x is greater than w" <<endl <<"z=w-x=" <<z <<endl; } if (w==x) cout <<"A tie" <<endl; cout <<"Thanks for telling me. \n\n"; return 0; }

The else keyword A program should take one branch if a condition is true, and another branch if the condition is false. if (expression) statement; else statement;

Example 11 #include main() { int w, x, y, z; cout <<"enter a value for w" << endl <<"w="; cin>>w; cout <<"enter a value for x" << endl <<"x="; cin>>x; if (w>x) { y=w+x; cout <<"w is greater than x" <<endl <<"y=w+x=" <<y <<endl; } else { z=w-x; cout <<"x is greater than w" <<endl <<"z=w-x=" <<z <<endl; } cout <<"Thanks for telling me. \n\n"; return 0; }

Advanced if statement Any statement can be used in an if or else clause. if (expression1) { if (expression2) statement1; else { if (expression3) statement2; else statement3; } else statement4;

Switch Statement The switch statement enables you to select from multiple choice on a set of fixed values for a given expression.

Example 12 #include main() { int x=0; cout <<"Please select one of the following delicious dishes:" << endl <<"1 \t Hamburger" <<endl <<"2 \t Hamburger and coke" <<endl <<"3 \t Coke" <<endl; cout <<"Enter your choice \t"; cin>>x; switch(x) { case 1: cout << endl <<"Hamburger" <<endl; break; case 2: cout << endl << "Hamburger and coke" <<endl; break; case 3: cout << endl << "Coke" <<endl; break; default: cout <<"You entered a wrong number" <<endl; } return 0; }

Loop A loop executes a sequence of statements until a particular condition is true (or false). loop: statements if ( ) goto loop;

Example 13 #include int main() { int i=1,y=20, x; loop: x=y+i; cout <<"x=" <<x <<"\t i=" <<i <<"\t y=" <<y <<endl; if (++i <= y) goto loop; cout <<"I love C++" <<endl; return 0; }

for Loop General form of the for loop is: for (initializing; test; increment) statements;

Example 14 #include int main() { int i=1,y=20, x; for(i;i<=y;i++) { x=y+i; cout <<"x=" <<x <<"\t i=" <<i <<"\t y=" <<y <<endl; } cout <<"I love C++" <<endl; return 0; }

Example 15 #include int main() { int y=20, x; for(int i=1;i<=y;i++) { x=y+i; cout <<"x=" <<x <<"\t i=" <<i <<"\t y=" <<y <<endl; } cout <<"I love C++" <<endl; return 0; }

Example 16 #include int main() { int i=1, y=20, x; for(; i<=y; i++) { x=y+i; cout <<"x=" <<x <<"\t i=" <<i <<"\t y=" <<y <<endl; } cout <<"I love C++" <<endl; return 0; }

Example 17 #include int main() { int i=1, y=20, x; for(;i<=y;x=y+i++, cout <<"x=" <<x <<"\t i=" <<i <<"\t y=" <<y <<endl); cout <<"I love C++" <<endl; return 0; }