Control structures in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile 9866245898

Slides:



Advertisements
Similar presentations
1 Chapter 3: Program Statements Lian Yu Department of Computer Science and Engineering Arizona State University Tempe, AZ
Advertisements

Copyright © 2012 Pearson Education, Inc. Chapter 6 More Conditionals and Loops Java Software Solutions Foundations of Program Design Seventh Edition John.
Chapter 5 Repetition and Loop Statements Instructor: Alkar & Demirer.
5-1 Flow of Control Recitation-01/25/2008  CS 180  Department of Computer Science  Purdue University.
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Loops – While, Do, For Repetition Statements Introduction to Arrays
Aalborg Media Lab 23-Jun-15 Software Design Lecture 6 “Conditionals and Loops”
ECE122 L7: Conditional Statements February 20, 2007 ECE 122 Engineering Problem Solving with Java Lecture 7 Conditional Statements.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
ECE122 L8: More Conditional Statements February 7, 2007 ECE 122 Engineering Problem Solving with Java Lecture 8 More Conditional Statements.
Logical Operators and Conditional statements
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Boolean Expressions and If Flow of Control / Conditional Statements The if Statement Logical Operators The else Clause Block statements Nested if statements.
COMP 110 Introduction to Programming Mr. Joshua Stough September 19, 2007.
Chapter 5 Repetition and Loop Statements Instructor: Kun-Mao Chao ( 台大資工 趙坤茂 )
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
CSM-Java Programming-I Spring,2005 Control Flow Lesson - 3.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
Switch Statements Comparing Exact Values. 2 The Switch Statement The switch statement provides another way to decide which statement to execute next The.
Chapter 5 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved2/33 Conditionals and Loops Now we will examine programming statements.
Programming in Java (COP 2250) Lecture 11 Chengyong Yang Fall, 2005.
Chapter 3: Program Statements
Chapter 5: Conditionals and loops. 2 Conditionals and Loops Now we will examine programming statements that allow us to: make decisions repeat processing.
1 Data Comparisons and Switch Data Comparisons Switch Reading for this class: L&L 5.3,
© 2004 Pearson Addison-Wesley. All rights reserved February 17, 2006 The ‘while’ Statement ComS 207: Programming I (in Java) Iowa State University, SPRING.
5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Copyright © 2012 Pearson Education, Inc. Chapter 6 More Conditionals and Loops Java Software Solutions Foundations of Program Design Seventh Edition John.
Switch Statements Comparing Exact Values. The Switch Statement: Syntax The switch statement provides another way to decide which statement to execute.
1 Chapter 3 Selections. 2 Outline 1. Flow of Control 2. Conditional Statements 3. The if Statement 4. The if-else Statement 5. The Conditional operator.
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.
Introduction to Java Java Translation Program Structure
Chapter 6. else-if & switch Copyright © 2012 Pearson Education, Inc.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Chapter 5 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved5-2 The switch Statement The switch statement provides another way.
Chapter 5 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved2/29 The switch Statement The switch statement provides another way.
Topics Logical Operators (Chapter 5) Comparing Data (Chapter 5) The conditional operator The switch Statement The for loop Nested Loops.
Chapter 5 Conditionals and Loops 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All rights.
Control Flow. Data Conversion Promotion happens automatically when operators in expressions convert their operands For example, if sum is a float and.
Flow of Control Unless indicated otherwise, the order of statement execution through a method is linear: one after the other in the order they are written.
Control statements Mostafa Abdallah
1 b Boolean expressions b truth tables b conditional operator b switch statement b repetition statements: whilewhile do/whiledo/while forfor Lecture 3.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
1 Flow of Control Chapter 5. 2 Objectives You will be able to: Use the Java "if" statement to control flow of control within your program.  Use the Java.
Design A software design specifies how a program will accomplish its requirements A design includes one or more algorithms to accomplish its goal.
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Operator Precedence Operators Precedence Parentheses () unary
Boolean Expressions and If
Programming Fundamentals
The order in which statements are executed is called the flow of control. Most of the time, a running program starts at the first programming statement,
The switch Statement The switch statement provides another way to decide which statement to execute next The switch statement evaluates an expression,
Control Structures.
The ‘while’ Statement September 27, 2006
Chapter 3: Program Statements
CS139 October 11, 2004.
Repetition and Loop Statements
CprE 185: Intro to Problem Solving (using C)
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
Comparing Data & the ‘switch’ Statement
Comparing Data & the ‘switch’ Statement
CprE 185: Intro to Problem Solving (using C)
Conditionals and Loops
CprE 185: Intro to Problem Solving (using C)
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:

Control structures in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile

Flow of Control Unless specified otherwise, the order of statement execution in a C-function is sequential: one statement after another in sequence Some programming statements allow us to: – decide whether or not to execute a particular statement or set of statements – execute a statement (s)over and over, repetitively

These decisions are based on expressions (more commonly conditional expressions) that evaluate to true (non 0)or false(0) The order of statement execution is called the flow of control Note: In C there is no data type as Boolean 5-3

Conditional Statements A conditional statement lets us choose which statement will be executed next Therefore they are sometimes called selection statements Conditional statements give us the power to make basic decisions The C conditional statements are : – if statement – if-else statement – switch statement

Logic of an if statement expression evaluated statement True(non 0) False (0)

The if Statement The if statement has the following syntax: if ( expression) statement; if is a C reserved word The expression should be a valid c expression If the expression i s evaluated to non 0 (true), the statement is executed.If it evaluates to 0( false), the statement is skipped.

Logic of an if-else statement expression evaluated statement1 True(non 0) False(0) statement2

The if-else Statement An else clause can be added to an if statement to make an if-else statement if ( expression ) statement1; else statement2; If the expression evaluates to true(non 0), statement1 is executed; if the expression is false(0), statement2 is executed One or the other will be executed, but not both

Boolean Expressions A condition often uses one of C's equality operators or relational operators, which all return boolean results: == equal to != not equal to < less than > greater than <= less than or equal to >= greater than or equal to Note the difference between the equality operator ( == ) and the assignment operator ( = )

Boolean Expressions in C C does not have a boolean data type. Therefore, C compares the values of variables and expressions against 0 (zero) to determine if they are true or false. If the value is 0 then the result is implicitly assumed to be false. If the value is different from 0 then the result is implicitly assumed to be true. Java langauge has Boolean data types.

Block Statements Several statements can be grouped together into a block statement delimited by braces A block statement can be used wherever a statement is called for in the C syntax rules if (total > MAX) { printf ("Error!!\n"); errorCount++; }

Block Statements In an if-else statement, the if portion, or the else portion, or both, could be block statements if (total > MAX) { printf("Error!!"); errorCount++; } else { printf ("Total: %d“, total); current = total*2; }

Nested if Statements The statement executed as a result of an if statement or else clause could be another if statement These are called nested if statements An else clause is matched to the last unmatched if (no matter what the indentation implies) Braces can be used to specify the if statement to which an else clause belongs

The switch Statement The switch statement provides another way to decide which statement (s)to execute next The switch statement evaluates an expression, then attempts to match the result to one of several possible cases Each case contains a value and a list of statements The flow of control transfers to statement associated with the first case value that matches

The switch Statement Often a break statement is used as the last statement in each case's statement list A break statement causes control to transfer to the end of the switch statement If a break statement is not used, the flow of control will continue into the next case Sometimes this may be appropriate, but often we want to execute only the statements associated with one case

The switch Statement switch (option) { case 'A': aCount++; break; case 'B': bCount++; break; case 'C': cCount++; break; default: otherCount++; break; } An example of a switch statement:

The switch Statement A switch statement can have an optional default case The default case has no associated value and simply uses the reserved word default If the default case is present, control will transfer to it if no other case value matches If there is no default case, and no other value matches, control falls through to the statement after the switch

The switch Statement The expression of a switch statement must result in an integral type, meaning an integer ( byte, short, int,) or a char It cannot be a floating point value ( float or double ) The implicit test condition in a switch statement is equality You cannot perform relational checks with a switch statement

The switch Statement The general syntax of a switch statement is: switch ( expression ) { case value1 : statement-list1 case value2 : statement-list2 case value3 : statement-list3 case... } switch and case are reserved words If expression matches value2, control jumps to here

Repetition in Programs In most software, the statements in the program may need to repeat for many times. – e.g., calculate the value of n!. – If n = 10000, it’s not elegant to write the code as 1*2*3*…* Loop Loop is a control structure that repeats a group of steps in a program. – Loop body – Loop body stands for the repeated statements. There are three C loop control statements: – whilefordo-while – while, for, and do-while. 5-20

Flow Diagram of Loop Choice Process Copyright ©2004 Pearson Addison-Wesley. All rights reserved e.g., calculate the value of n! e.g., read the content in a file

The while Statement in C The syntax of while statement in C: loop repetition condition while (loop repetition condition) statement Loop repetition condition Loop repetition condition is the condition which controls the loop. true The statement is repeated as long as the loop repetition condition is true. infinite loop A loop is called an infinite loop if the loop repetition condition is always true. 5-22

An Example of a while Loop 5-23 Statement Loop repetition condition Loop control variable Loop control variable is the variable whose value controls loop repetition. In this example, count_emp is the loop control variable.

Flowchart for a while Loop 5-24 Loop repetition condition Statement

The for Statement in C The syntax of for statement in C: initialization expression for (initialization expression; loop repetition condition loop repetition condition; update expression update expression) statement initialization expression The initialization expression set the initial value of the loop control variable. loop repetition condition The loop repetition condition test the value of the loop control variable. update expression The update expression update the loop control variable. 5-25

An Example of the for Loop 5-26 Loop repetition condition Initialization Expression Update Expression count_emp is set to 0 initially. count_emp should not exceed the value of number_emp. count_emp is increased by one after each iteration.

Nested Loops outer loop inner loops Nested loops consist of an outer loop with one or more inner loops. e.g., for (i=1;i<=100;i++){ for(j=1;j<=50;j++){ … } The above loop will run for 100*50 iterations Inner loop Outer loop

The do-while Statement in C The syntax of do-while statement in C: loop repetition condition do statement while (loop repetition condition); The statement is first executed. loop repetition condition If the loop repetition condition is true, the statement is repeated. Otherwise, the loop is exited. 5-28

An Example of the do-while Loop /* Find even number input */ do{ printf(“Enter a value: ”); scanf(“%d”, &num); }while (num % 2 !=0) 5-29 This loop will repeat if the user inputs odd number.