Lectures on Numerical Methods1 Statements çExpressions, when terminated by a semicolon, become statements. çExamples X = 5; I++; IsPrime(c); c = 5 * (

Slides:



Advertisements
Similar presentations
Chapter 3: Control Flow S. M. Farhad. Statements and Blocks An expression becomes a statement when it is followed by a semicolon Braces { and } are used.
Advertisements

Control Statements. Define the way of flow in which the program statements should take place. Control Statements Implement decisions and repetitions.
Decision Making EE2372 Software Design I Dr. Gerardo Rosiles.
Programming Languages and Paradigms The C Programming Language.
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
1 Conditional Statement. 2 Conditional Statements Allow different sets of instructions to be executed depending on truth or falsity of a logical condition.
1 CS101 Introduction to Computing Lecture 23 Flow Control & Loops (Web Development Lecture 8)
0 Chap. 3 Control Flow 3.1 Statements and Blocks Imperative Programming, B. Hirsbrunner, diuf.unifr.ch/pai/ip Session 4, 3 April if, if … else.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 4 – C Program Control Outline 4.1Introduction.
Control Flow C and Data Structures Baojian Hua
1 Lecture 8:Control Structures I (Selection) (cont.) Introduction to Computer Science Spring 2006.
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.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
true (any other value but zero) false (zero) expression Statement 2
 2000 Prentice Hall, Inc. All rights reserved. Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
Tutorial 4 Decision Making with Control Structures and Statements Section A - Decision Making JavaScript Tutorial 4 -Decision Making with Control.
1 Lecture 5  More flow control structures  for  do  continue  break  switch  Structured programming  Common programming errors and tips  Readings:
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved The switch Multiple-Selection Statement switch.
Arrays, Conditionals & Loops in Java. Arrays in Java Arrays in Java, are a way to store collections of items into a single unit. The array has some number.
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
CSM-Java Programming-I Spring,2005 Control Flow Lesson - 3.
 Decision making statements Decision making statements if statement if...else statement Nested if...else statement (if...elseif....else Statement) 
Algorithms and Computing Lecture 3 Control Statements By Dr. M. Tahir Khaleeq.
Programming C for Engineers An exercise is posted on the web site! Due in one week Single submission.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
Lecture 4 C Program Control Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee C Language Part 2.
COMPUTER PROGRAMMING. Control Structures A program is usually not limited to a linear sequence of instructions. During its process it may repeat code.
Control Statements Spring Semester 2013Programming and Data Structure1.
Program Looping Making Decisions Copyright © 2012 by Yong-Gu Lee
Chapter 3 Control Flow Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University.
Lecture 8: Choosing the Correct Loop. do … while Repetition Statement Similar to the while statement Condition for repetition only tested after the body.
5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,
PHP Conditional Statements Conditional statements in PHP are used to perform different actions based on different conditions. Conditional Statements Very.
Chapter 7 Additional Control Structures. 2 2 void GetYesOrNo (/* out */ char& response) // Inputs a character from the user // Postcondition: response.
Expressions An expression is a series of variables, operators, and method calls (constructed according to the syntax of the language) that evaluates to.
1 Conditionals In many cases we want our program to make a decision about whether a piece of code should be executed or not, based on the truth of a condition.
Chapter 8 Repetition Statements. Introduction Iteration - process of looping or the repetition of one or more statements Loop body - the statement, or.
Chapter 05 (Part III) Control Statements: Part II.
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.
Control Statements in C 1.Decision making statements 2.Looping statements 3.Branching statements
CCSA 221 Programming in C CHAPTER 6 MAKING DECISIONS 1.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
IT CS 200: C ONDITION Lect. Napat Amphaiphan. T HE ABILITY TO CONTROL THE FLOW OF YOUR PROGRAM, LETTING IT MAKE DECISIONS ON WHAT CODE TO EXECUTE 2.
1 CS428 Web Engineering Lecture 13 Flow Control & Loops (JavaScript - III)
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Control structures in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
CC213 Programming Applications Week #2 2 Control Structures Control structures –control the flow of execution in a program or function. Three basic control.
Lecture 3.1 Operators and Expressions Structured Programming Instructor: Prof. K. T. Tsang 1.
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Condition – any expression that evaluates to true/false value Relational operators are BINARY.
 Real numbers representation - Floating Point Notation  First C Program  Variables Declaration  Data Types in C ◦ char, short, int, long, float, double,
 Very often when you write code, you want to perform different actions for different decisions. You can use conditional statements in your code to do.
Chapter 4 – C Program Control
Chapter 6: Loops.
Programmation impérative - Prof. Béat Hirsbrunner
Control Structures.
Flow of Control.
Flow of Control.
- Additional C Statements
CS1100 Computational Engineering
Chapter 6 Decision Making and Looping
Flow of Control.
CSC215 Lecture Flow Control.
CSC215 Lecture Control Flow.
REPETITION STATEMENTS
Program Flow.
Dale Roberts, Lecturer IUPUI
Flow of Control.
CSC215 Lecture Control Flow.
Presentation transcript:

Lectures on Numerical Methods1 Statements çExpressions, when terminated by a semicolon, become statements. çExamples X = 5; I++; IsPrime(c); c = 5 * ( f – 32 ) / 9 çOne can construct compound statements by putting statements and declarations in Braces { and }. Also called blocks. çExample if ( rad > 0.0 ) { float area = pi * rad * rad; float peri = 2 * pi * rad; printf( “Area = %f\n”, area ); printf( “Peri = %f\n”, peri ); } else printf( “Negative radius\n”);

Lectures on Numerical Methods2 Labeled statements çOne can give a label to any statement. The form is çIdentifier : statement çThere are other labeled statements like case and default, to be used with switch statement. çExample

Lectures on Numerical Methods3 If – else  The if-else statement expresses simplest decision making. The syntax is if (expression) statement 1 else opt Statement 2  The expression is evaluated and if it is nonzero (true) then the statement 1 is executed. Otherwise, if else is present statement 2 is executed.  Expression can just be numeric and need not be conditional expression only.  Statement 1 and statement 2 may be compound statements or blocks.

Lectures on Numerical Methods4 If – else if ( n > 0 ) if ( isPrime(n) ) { printf(“%d is prime\n”,n); return; } else printf(“Input must be positive\n”,n); printf(“%d is non-prime\n”,n);  Each else is associated with closest previous else-less if. Using this principle multi-way decisions can be constructed.

Lectures on Numerical Methods5 Switch çThe syntax of switch statement is switch (expression) { case const-expression 1 : statement 1 case const-expression 2 : statement 2 : default : statement n } çAll case expressions must be different. Expression must evaluate to an integer. çFirst the expression is evaluated. Then the value of expression is compared with the case expressions. The execution begins at the case statement, whose case expression matches. All the statements below are executed. çIf default is present and if no other case matches then default statement is executed.

Lectures on Numerical Methods6 Switch switch ( marks / 10 ) { case 3 : grade = “DD”; break; case 4 : grade = “CD”; break; case 5 : grade = “CC”; break; case 6 : grade = “BC”; break; case 7 : grade = “BB”; break; case 8 : grade = “AB”; break; case 9 : case 10 : grade = “AA”; break; default : grade = “FF”; break; }

Lectures on Numerical Methods7 Iterations çThe three types of loops are given below. while (expression) statement for (expression1 opt ; expression2 opt ; expression3 opt ) statement do statement while(expression); çIn while loop the expression (which must be arithmetic) is evaluated and if the value is nonzero, then the statement is executed. The process is continued till the expression becomes zero. The expression is evaluated before the iteration. çFor statement is equivalent to expression1; while ( expression2 ) { statement expression3; }

Lectures on Numerical Methods8 Iterations  In the do loop, the statement is executed and then the expression is evaluated. If the expression is nonzero the process is repeated. Thus the condition is evaluated at the end of each iteration. çExample x1 = 1; do { x0 = x1; x1 = x0 – f(x0) / derf(x0); } while ( fabs(x1 – x0) > FLT_EPSILON ) ; çWe needed to evaluate x1 before we could apply the convergence criterion.

Lectures on Numerical Methods9 Break and Continue çThe loops have one expression that decides whether the iterative process should be terminated. It is sometimes convenient to be able to exit from the loop.  break statement provides an early exit from the for, while and do loops. çIt also provides an exit from switch statement.  continue statement causes the jump to the end of the loop body, skipping the statements only once. The loop may continue. while (...) {... continue;... cont: ; } do {... continue;... cont: ; } for (...) {... continue;... cont: ; }

Lectures on Numerical Methods10 Break and continue çExample for ( i = 0; i < n; i++ ) { if ( a[i] < 0 ) continue; /* Process only non-negative elements of the array */... } çExample for ( i = 2; i <= (int)sqrt(n); i++ ) { if ( n % i == 0 ) break; if ( n % i ) printf(“Prime\n”); else printf(“Not prime\n”);