C++ Statements represent the lowest-level building blocks of a program and it may be like:. A simple statement is a computation terminated by a semicolon.

Slides:



Advertisements
Similar presentations
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 8: Arrays Starting Out with C++ Early Objects Seventh Edition by.
Advertisements

CS 240 Computer Programming 1
Spring Semester 2013 Lecture 5
1 Chapter 3:Operators and Expressions| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2006 | Last Updated: July 2006 Slide 1 Operators and Expressions.
What is the Result and Type of the Following Expressions? int x=2, y=15;double u=2.0,v=15.0; -xx+yx-y x*vy / xx/yy%xx%y u*vu/vv/uu%v x * u(x+y)*uu /(x-x)
1 Lecture 16:User-Definded function I Introduction to Computer Science Spring 2006.
ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne
Chapter 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
© Janice Regan, CMPT 128, February CMPT 128: Introduction to Computing Science for Engineering Students Pointers.
C++ Basics March 10th. A C++ program //if necessary include headers //#include void main() { //variable declaration //read values input from user //computation.
C++ Classes & Data Abstraction
Local Variables and Scope Benjamin Fein. Variable Scope A variable’s scope consists of all code blocks in which it is visible. A variable is considered.
True or false A variable of type char can hold the value 301. ( F )
1 9/17/07CS150 Introduction to Computer Science 1 Type Casting.
1 Objectives You should be able to describe: Relational Expressions The if-else Statement Nested if Statements The switch Statement Common Programming.
Iteration This week we will learn how to use iteration in C++ Iteration is the repetition of a statement or block of statements in a program. C++ has three.
1 CS150 Introduction to Computer Science 1 Arithmetic Operators.
Chapter 5: Control Structures II (Repetition)
CS150 Introduction to Computer Science 1
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.
C++ for Engineers and Scientists Third Edition
Chapter 3 Getting Started with C++
CSci 125 Lecture 10 Martin van Bommel. Simple Statements Expression followed by semicolon Assignments total = n1 + n2; Function calls printf(”Hello.\n”);
C++ Basics Structure of a Program. C++ Source Code Plain text file Typical file extension .CPP Must compile the C++ source code without errors before.
1 Chapter 4: Selection Structures. In this chapter, you will learn about: – Selection criteria – The if-else statement – Nested if statements – The switch.
2 Objectives You should be able to describe: Relational Expressions Relational Expressions The if-else Statement The if-else Statement Nested if Statements.
/* Documentations */ Pre process / Linking statements Global declarations; main( ) { Local Declarations; Program statements / Executable statements; }
COMPUTER PROGRAMMING. Functions What is a function? A function is a group of statements that is executed when it is called from some point of the program.
Principles of programming languages 5: An operational semantics of a small subset of C Department of Information Science and Engineering Isao Sasano.
COMPUTER PROGRAMMING. A Typical C++ Environment Phases of C++ Programs: 1- Edit 2- Preprocess 3- Compile 4- Link 5- Load 6- Execute Loader Primary Memory.
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 7 Clicker Questions September 22, 2009.
COMPUTER PROGRAMMING. Iteration structures (loops) There may be a situation when you need to execute a block of code several number of times. In general,
Code Grammar. Syntax A set of rules that defines the combination of symbols and expressions.
Chapter 05 (Part III) Control Statements: Part II.
Basic Program Construction
Syntax and Semantics, and the Program Development Process ROBERT REAVES.
CSCI 125 & 161 / ENGR 144 Lecture 8 Martin van Bommel.
Selection-making Decisions Selection allows you to choose between two or more possible program flow --- it lets you make decisions in your program. Examples.
Lecture 10: Modular Programming (functions) B Burlingame 13 April 2015.
Computer Science By: Erica Ligons Compound Statement A compound statement- block A compound statement- is a unit of code consisting of zero or more statement.
CS1201: PROGRAMMING LANGUAGE 2 FUNCTIONS. OVERVIEW What is a Function? Function Prototype Vs Decleration Highlight Some Errors in Function Code Parameters.
A First Book of C++ Chapter 4 Selection. Objectives In this chapter, you will learn about: –Relational Expressions –The if-else Statement –Nested if Statements.
COP 2551 Introduction to Object Oriented Programming with Java Topics –Java Statements –Java Expressions –Postfix Expressions –Prefix Expressions –Evaluating.
Copyright Curt Hill The Assignment Operator and Statement The Most Common Statement you will use.
Dr. Sajib Datta Jan 21,  Declare a variable ◦ int height; [note that no value is still assigned]  Assign a variable a value ◦ height =
Function PrototypetMyn1 Function Prototype We can declare a function before we use or define it by means of a function prototype. A function prototype.
Chapter 2 Creating a C++ Program. Elements of a C++ Program Four basic ways of structuring a program Four basic ways of structuring a program 1.Sequencing.
CS 161 Introduction to Programming and Problem Solving Chapter 12 C++ Statements Herbert G. Mayer, PSU Status 10/8/2014 Initial content copied verbatim.
Computer Programming II Lecture 4. Functions - In C++ we use modules to divide the program into smaller and manageable code. These modules are called.
Copyright © Curt Hill The Assignment Operator and Statement The Most Common Statement you will use.
Classes and Objects C++ Programming Technologies.
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
A First Book of C++ Chapter 4 Selection.
Chapter 3 Control Statements
Introduction to C++ Programming Language
Chapter 2 part #1 C++ Program Structure
Arithmetic Operator Operation Example + addition x + y
Chapter 4 Repetition Structures
3-3 Side Effects A side effect is an action that results from the evaluation of an expression. For example, in an assignment, C first evaluates the expression.
CSC215 Lecture Flow Control.
Local Variables, Global Variables and Variable Scope
CSC215 Lecture Control Flow.
Statements and expressions - java
Lecture Notes - Week 2 Lecture-1. Lecture Notes - Week 2 Lecture-1.
ECE 103 Engineering Programming Chapter 12 More C Statements
Controlling Program Flow
Chapter 4: Expression and Operator
Control Statements Paritosh Srivastava.
CSC215 Lecture Control Flow.
Presentation transcript:

C++ Statements represent the lowest-level building blocks of a program and it may be like:. A simple statement is a computation terminated by a semicolon. Variable definitions and semicolon-terminated. expressions. Examples: Int i; // declaration statement ++i; // this has a side-effect double d = 10.5;// declaration statement d + 5; // useless statement!

Multiple statements can be combined into a compound statement by enclosing them within braces. ;// null statement For example: { int min, i = 10, j = 20; min = (i < j ? i : j); cout << min << '\n'; }

Where condition is the expression that is being evaluated. If this condition is true, statement is executed. If it is false, statement is ignored (not executed) and the program continues

Syntax: Syntax: If (condition) Statement; if Statement statement 1 ; else statement 2 ; if (balance > 0) { interest = balance * creditRate; balance += interest; }

If (condition) statement 1 ; else statement 2 ; if (x == 100) cout << "x is 100"; else cout << "x is not 100";

if (x == 100) { cout << "x is "; cout << x; } if (x == 100) cout << "x is 100"; Else cout << "x is not 100"; if (x > 0) cout << "x is positive"; else if (x < 0) cout << "x is negative"; else cout << "x is 0";

Int main( ) { int score; cout<< “Enter the test score :”; cin>> score ; if (score >100) cout<< “Error :score is out of range”; else if(score >= 90) cout <<‘A’ ; else if(score >= 80) cout <<‘B’ ; else if(score >= 70) cout <<‘C’ ; else if(score >= 60) cout <<‘D’ ; else if(score >= 0) cout <<‘F’ ; else cout<<“Error: Score is out of range”; } Write a program that check an entered score value using If statement.

Writ a program to solves the eqautiona*x*x+ b*x + c=0: # include main( ) { Float a,b,c; Cout<< “Enter coefficients of quadratic eqaation:”; Cin>> a>>b>>c; If (a== 0){ Cout<< “This is not quadratic equation:a==0’’; return 0; }

Cout<< the equation is :<<a<<“x^2+”<<b<<“x+”<<c<<“=0”; Double d,x1,x2; D= b*b-4*a*c; If (d<0) { Cout << this equation has no real solution :d<0\n; return 0; } X1=(-b+sqrt(d)/(2*a); X2=(-b-sqrt(d)/(2*a); Cout <<“The solution are :” x1<<“, “<<x2<< endl; }

Syntax: switch (expression) { case constant1: statements;... case constantn: statements; default: statements; } Its objective is to check several possible constant values for an expression Its objective is to check several possible constant values for an expression.

switch exampleif-else equivalent switch (x) { case 1: cout << "x is 1"; break; case 2: cout << "x is 2"; break; default: cout << "value of x unknown"; } if (x == 1) { cout << "x is 1"; } else if (x == 2) { cout << "x is 2"; } else { cout << "value of x unknown"; }

switch (operator) { case '+':result = operand1 + operand2; break; case '-':result = operand1 - operand2; break; case 'x': case '*':result = operand1 * operand2; break; case '/': result = operand1 / operand2; break; default:cout << "unknown operator: " << ch << '\n'; break; }

Syntax: While (expression) statement #include using namespace std; int main () { int n; cout "; cin >> n; while (n>0) { cout << n << ", "; --n; } cout << "FIRE!\n"; return 0; }

Syntax: do statement while (condition); int main () { unsigned long n; do { cout << "Enter number (0 to end): "; cin >> n; cout << "You entered: " << n << "\n"; } while (n != 0); return 0; }

Syntax: While (expression) statement #include using namespace std; int main () { int n; cout "; cin >> n; while (n>0) { cout << n << ", "; --n; } cout << "FIRE!\n"; return 0; }

Syntax: initialization increase statement for (initialization; condition; increase) statement ; int main () { for (int n=10; n>0; n--) { cout << n << ", "; } cout << "FIRE!\n"; return 0; }

EX: THE SUM OF THE FRIST N SQUARESHint { Int n, sum=0; cout<< “Enter a positive integer:”; cin>> n; for ( int i=1 ; i <=n ; i++) sum+ = i * i ; cout <<“The sum of the frist “<<n<<“squares is ” <<sum << endi ; }

Hint { Int sum=0; for ( int i=1 ; i <=100 ; i++) sum+ = i / (i+1) ;//sum+= (i-1)/i cout <<“The sum of 1/2+2/3+……..89/99+99/100 ” <<sum << endi ; }

{ Int n, sg= 1,sum=0; cout<< “Enter a positive integer:”; for ( int i=1 ; i <=n ; i++) sum+ = sg*i ; sg= -1*sg ; cout <<“The sum of 1/2+2/3+……..98/99+99/100 ” <<sum << endi ; }

An array is a series of elements of the same type placed in contiguous memory locations. An array variable is defined by specifying its dimension and the type of its elements. Int heights [10];

const int size = 3; double Average (int nums[size]) { double average = 0; for (register i = 0; i < size; ++i) average += nums[i]; return average/size; }