Programming In C++ Spring Semester 2013 Lecture 4 Programming In C++, Lecture 3 By Umer Rana.

Slides:



Advertisements
Similar presentations
Selection Feature of C: Decision making statements: It allow us to take decisions as to which code is to be executed next. Since these statements control.
Advertisements

Control Structures.
Introduction to C Programming
The if-else Statements
Control Flow Statements: Repetition/Looping
Pemrograman Dasar Control Flow Statements: Decision Making or Selection PTIIK - UB 1.
Programming In C++ Spring Semester 2013 Lecture 3 Programming In C++, Lecture 3 By Umer Rana.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 10P. 1Winter Quarter Repetition Structures.
CS 101 Introductory Programming - Lecture 7: Loops In C & Good Coding Practices Presenter: Ankur Chattopadhyay.
Fundamental of C programming
Spring Semester 2013 Lecture 5
Lecture 3: Control Structures - Selection BJ Furman 10SEP2012.
Decision Making EE2372 Software Design I Dr. Gerardo Rosiles.
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.
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 4 – C Program Control Outline 4.1Introduction.
Basic Elements of Programming A VB program is built from statements, statements from expressions, expressions from operators and operands, and operands.
C Lecture Notes 1 Program Control (Cont...). C Lecture Notes 2 4.8The do / while Repetition Structure The do / while repetition structure –Similar to.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
true (any other value but zero) false (zero) expression Statement 2
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Logical and Relational Expressions Nested if statements.
1 Lecture 7:Control Structures I (Selection) Introduction to Computer Science Spring 2006.
Tutorial 4 Decision Making with Control Structures and Statements Section A - Decision Making JavaScript Tutorial 4 -Decision Making with Control.
C++ for Engineers and Scientists Third Edition
Visual C++ Programming: Concepts and Projects
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 8P. 1Winter Quarter Control Statements Lecture.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved The switch Multiple-Selection Statement switch.
EGR 2261 Unit 4 Control Structures I: Selection  Read Malik, Chapter 4.  Homework #4 and Lab #4 due next week.  Quiz next week.
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
Lecture 4 C Program Control Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
1 4.8The do/while Repetition Structure The do/while repetition structure –Similar to the while structure –Condition for repetition tested after the body.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Flow Control (Switch, do-while, break) Outline 4.7The.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 21, 2005 Lecture Number: 10.
Review the following: if-else One branch if Conditional operators Logical operators Switch statement Conditional expression operator Nested ifs if –else.
Introduction to Computing Lecture 05: Selection (continued) Introduction to Computing Lecture 05: Selection (continued) Assist.Prof.Dr. Nükhet ÖZBEK Ege.
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.
Conditional Structures UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) ADNAN BABAR MT14028 CR
Lecture 2 Control Structure. Relational Operators -- From the previous lecture Relational Operator Meaning == is equal to < is less than > is greater.
Control Statements in C 1.Decision making statements 2.Looping statements 3.Branching statements
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.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Chapter 4 Control Structures I. Chapter Objectives Learn about control structures Examine relational and logical operators Explore how to form and evaluate.
Sections © Copyright by Pearson Education, Inc. All Rights Reserved.
Programming Language C++ Lecture 3. Control Structures  C++ provides control structures that serve to specify what has to be done to perform our program.
Control Statements: Part1  if, if…else, switch 1.
CSE202: Lecture 5The Ohio State University1 Selection Structures.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Control Structures (Selection & Repetition)
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)
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
 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.
THE DECISIONS CONTROL STRUCTURE! CHAPTER 2. Transfer of control statement: o The statement of computer program are executed one after the other in the.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 10P. 1Winter Quarter Repetition Structures Lecture 10.
Lesson #4 Logical Operators and Selection Statements.
Lesson #4 Logical Operators and Selection Statements.
Chapter 4 – C Program Control
CNG 140 C Programming (Lecture set 3)
Decisions Chapter 4.
A mechanism for deciding whether an action should be taken
Expressions and Control Flow in JavaScript
IF if (condition) { Process… }
Professor Jodi Neely-Ritz CGS3460
CSC215 Lecture Control Flow.
Relational, Logical, and Equality Operators
Chapter 4: Control Structures I (Selection)
Chap 7. Advanced Control Statements in Java
CSC215 Lecture Control Flow.
Lecture 9: Implementing Complex Logic
Presentation transcript:

Programming In C++ Spring Semester 2013 Lecture 4 Programming In C++, Lecture 3 By Umer Rana

Programming In C++, Lecture 3 By Umer Rana

Decisions n If Statement n If-else Statement n Else-if Statement n Switch Statement n Conditional Operator Programming In C++, Lecture 3 By Umer Rana

Decisions If Statement The if statement controls conditional branching. The body of an if statement is executed if the value of the expression is true. Syntax: if ( expression ) statement Example: if(a==b) printf(“I study in Preston University”); Programming In C++, Lecture 3 By Umer Rana

Decisions Multiple Statement with if if(a==b) { printf(“My name is Umer Rana”); printf(“I tech in Preston University”); } Programming In C++, Lecture 3 By Umer Rana

Decisions Nested if Statements One if statement is part of the body of another if statement. if(a==b) { if(c==d) { printf(“My name is Umer Rana”); printf(“I tech in Preston University”); } Programming In C++, Lecture 3 By Umer Rana

Decisions if-else Statement This feature permits the programmer to write a single comparison, and then execute one of the two statements depending upon whether the test expression is true or false. Syntax if(expression)(true) statement1 else(false) statement2 Programming In C++, Lecture 3 By Umer Rana

Decisions Example: if(a==b) { printf(“I study in Preston University”); } else { printf(“I study in Iqra University”); } Programming In C++, Lecture 3 By Umer Rana

Decisions if-else if Statement This construct is useful where two or more alternatives are available for selection. if(a==b) { printf(“I study in Preston University”); } else { if(a==c) printf(“I study in Iqra University”); else printf(“I study in Bahria University”); } Programming In C++, Lecture 3 By Umer Rana

Decisions if Statement with Logical Operators || Logical OR && Logical AND ! Logical NOT Programming In C++, Lecture 3 By Umer Rana

Decisions if Statement with Logical Operators || Logical OR The Logical (||) means if either the expression on the right or left side or both sides of the operator it true then the entire expression is true. OR operator represented by the exclamation point (||). Example: if ( (a==b) || (a==c)) printf (“I study in Preston University”); Programming In C++, Lecture 3 By Umer Rana

Decisions if Statement with Logical Operators && Logical AND The Logical (&&) means if both expressions on the right and left sides of the operator is true then the entire expression is true. AND operator represented by the exclamation point (&&). Example: if ( (a==b) && (a==c)) printf (“I study in Preston University”); Programming In C++, Lecture 3 By Umer Rana

Decisions if Statement with Logical Operators Logical NOT(!) This operator reverses the logical value of the expression it operates on, it makes a true expression false and a false expression true. Not operator takes only one operand and represented by the exclamation point (!). Example: if ( !( x < 5 ) ) or if ( ! (c) ) Programming In C++, Lecture 3 By Umer Rana

Decisions Break Statement in if Statement if(a==b) printf(“I study in Preston University”); break; else if(a==c) printf(“I study in Iqra University”); break; else if(a==e) printf(“I study in Bahria University”); break; else printf(“I study in Comsat University”); Programming In C++, Lecture 3 By Umer Rana

Decisions The Switch Statement The switch statement is almost the same as an “if statement”, substitute for long if statements. The switch statement selection is based on the value of a single variable or of a simple expression. If the value of the switch expression or variable equals the value of one of the case value, the statements following that case value are processed. If not, the default label statements are processed switch(expression/veriable) { case constant1: statements 1; break; case constant2: statements 2; break; default: statements n; } Programming In C++, Lecture 3 By Umer Rana

Decisions Example int input; printf( "1. BSCS-1\n" ); printf( "2. BSCS-2\n" ); printf( "3. BSCS-3\n" ); scanf( "%d", &input ); switch ( input ) { case 1: printf(“I study in BSCS-1\n"); break; case 2: printf(" I study in BSCS-2\n"); break; case 3: printf(" I study in BSCS-3\n "); break; default: printf( "Bad input!\n" ); break; } Programming In C++, Lecture 3 By Umer Rana

Decisions The Conditional Operator The conditional operator is also known as ternary operator. It is called ternary operator because it takes three arguments. The conditional operator evaluates an expression returning a value if that expression is true and different one if the expression is evaluated as false. Syntax: condition ? True-case Statement : False-case Statement If the condition is true, True-case Statement is returned else False-case Statement is returned. Programming In C++, Lecture 3 By Umer Rana

Decisions The Conditional Operator input=1 ? printf(“I study in BSCS-1\n") : printf(“I study in BSCS-2\n"); Programming In C++, Lecture 3 By Umer Rana