Intro to Programming Week # 3 If-else Statement Lecture # 6

Slides:



Advertisements
Similar presentations
1 CSC103: Introduction to Computer and Programming Lecture No 8.
Advertisements

1 Engineering Problem Solving With C++ An Object Based Approach Chapter 3 Control Structures.
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
Week 4 Selections This week shows how to use selection statements for more flexible programs. It also describes the various integral types that are available.
1 10/20/08CS150 Introduction to Computer Science 1 do/while and Nested Loops Section 5.5 & 5.11.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
If Statements. COMP104 If / Slide 2 Three Program Structures * Sequence - executable statements which the computer processes in the given order * Choice.
Quiz 1 Exam 1 Next Week. Nested if Statements if (myGrade >= 80) if (myGrade >= 90) cout
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
PRINCIPLES OF PROGRAMMING Revision. A Computer  A useful tool for solving a great variety of problems.  To make a computer do anything (i.e. solve.
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 1: Selection statements: if, if…else, switch.
Outlines Chapter 3 –Chapter 3 – Loops & Revision –Loops while do … while – revision 1.
Computer Science Department Relational Operators And Decisions.
CPS120: Introduction to Computer Science Decision Making in Programs.
CS102 Introduction to Computer Programming Chapter 4 Making Decisions.
1 CSC103: Introduction to Computer and Programming Lecture No 7.
Lecture 2: Logical Problems with Choices. Problem Solving Before writing a program Have a thorough understanding of the problem Carefully plan an approach.
Programming Fundamentals Lecture 4. In the Previous Lecture Basic structure of C program Variables and Data types Operators ‘cout’ and ‘cin’ for output.
CHAPTER 8 CONTROL STRUCTURES Prepared by: Lec. Ghader R. Kurdi.
Previously Repetition Structures While, Do-While, For.
Chapter 3. Outline Relational Operators Loops Decisions Logical Operators Precedence Summary.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 21, 2005 Lecture Number: 10.
Chapter 05 (Part III) Control Statements: Part II.
Lecture #7 CONTROL STRUCTURE & FLOW CHARTS
Conditional Structures UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) ADNAN BABAR MT14028 CR
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
CHAPTER#3 PART1 STRUCTURED PROGRAM DEVELOPMENT IN C++ 2 nd semester King Saud University College of Applied studies and Community Service Csc.
Chapter 6.  Control Structures are statements that are used to perform repetitive tasks and to make decisions within a program. Loop repeats a sequence.
Chapter#3 Part1 Structured Program Development in C++
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,
CPS120: Introduction to Computer Science Decision Making in Programs.
Unary Not operator !  !true = false  !false = true.
CPS120: Introduction to Computer Science Decision Making in Programs.
Lecture 7 Computer Programming -1-. Conditional Statements 1- if Statement. 2- if ….. else Statement. 3- switch.
Programming Language C++ Lecture 3. Control Structures  C++ provides control structures that serve to specify what has to be done to perform our program.
 2003 Prentice Hall, Inc. All rights reserved. 1 Basic C++ Programming.
Looping Increment/Decrement Switch. Flow of Control Iteration/Switch Statements.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
 By the end of this section you should be able to: ◦ Differentiate between sequence, selection, and repetition structure. ◦ Differentiae between single,
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
Lecture #8 SWITCH STATEMENT By Shahid Naseem (Lecturer)
Computer Programming -1-
THE DECISIONS CONTROL STRUCTURE! CHAPTER 2. Transfer of control statement: o The statement of computer program are executed one after the other in the.
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.
Branching statements.
Chapter 3 Selection Statements
Chapter 3 Control Statements
REPETITION CONTROL STRUCTURE
Bill Tucker Austin Community College COSC 1315
Decisions Chapter 4.
Intro to Programming Week # 6 Repetition Structure Lecture # 10
Engineering Problem Solving with C++, Etter/Ingber
Chapter 2.1 Control Structures (Selection)
CSC113: Computer Programming (Theory = 03, Lab = 01)
Intro to Programming Week # 4 Switch Statement Lecture # 7
Chapter 2.2 Control Structures (Iteration)
Intro to Programming Week # 5 Repetition Structure Lecture # 9
Control Statement Examples
Lecture 2: Logical Problems with Choices
3 Control Statements:.
Intro to Programming Week # 2 Variables/Data type Lecture # 4 & 5
Chapter 2.2 Control Structures (Iteration)
Summary Two basic concepts: variables and assignments Basic types:
Chapter 4: Control Structures I (Selection)
Control Statements Paritosh Srivastava.
HNDIT11034 More Operators.
Selection Control Structure
Programming Fundamental
Presentation transcript:

Intro to Programming Week # 3 If-else Statement Lecture # 6 Department of Computer Science & Engineering Air University Intro to Programming Week # 3 If-else Statement Lecture # 6 By: Saqib Rasheed

Programs executed sequentially by default Control Structures 3 control structures Sequence structure Programs executed sequentially by default Selection structures (conditional instructions) if, if/else, switch Repetition structures (loop/jump instructions) while, do/while, for

Flow Chart for if statement Connectors will come at the end By: Saqib Rasheed Air University

if Statement The if statement allows conditional execution if (condition) { statement ; - } The if statement allows conditional execution By: Saqib Rasheed Air University

Practice Example # 1 main () { int num; cout<<“Enter a number less than 10:”; cin>>num; If (num <10) cout<<“What a obedient students” } By: Saqib Rasheed Air University

Relational Operators The relational operators allow to compare two values Whether they are equal to each other. Unequal Whether one is greater than the other. By: Saqib Rasheed Air University

Relational Operators By: Saqib Rasheed Air University

We can even use arithmetic expressions in the if statement We can even use arithmetic expressions in the if statement. if ( 3 + 2 % 5 ) cout<<"This works"; if ( a = 10 ) cout<<"Even this works"; if ( -5 ) cout<<"Surprisingly even this works"; By: Saqib Rasheed Air University

Practice Example # 2 int var1, var2; cout<<"Input the first number:"; cin>>var1; cout<<"Input the second number:"; cin>>var2; if(var1 == var2) { cout<<"var1 is equal to var2"; } By: Saqib Rasheed Air University

Practice Example # 3 int main() { int n,d; cout << "Enter two positive integers: "; cin >> n >> d; if (n%d) cout << n << " is not divisible by " << d ; } By: Saqib Rasheed Air University

if-else Entry point for if-else block Exit point for if-else block Connectors at the end Exit point for if-else block By: Saqib Rasheed Air University

if-else if (condition) { statement ; - } else By: Saqib Rasheed Air University

Practice Example # 4 cout<<“Enter a number less than Equal to 10:”; cin>>num; If (num <=10) { cout<<“What a obedient students” } else cout<<“You are not obedient Student”; By: Saqib Rasheed Air University

Practice Example # 5 int var1, var2; cout<<"Input the first number:"; cin>>var1; cout<<"Input the second number:"; cin>>var2; if(var1 == var2) { cout<<"var1 is equal to var2"; } else cout<<"var1 is not equal to var2"; By: Saqib Rasheed Air University

Example Nested if int var1,var2,var3,var4; var1=12; var2=12; var3=10; if(var1 == var2) { cout<<"\nOuter Loop Statement"; if (var3 == var4) cout<<"\nVar1 & Var2 are Equal and Var3 & var4 are Equal"; } cout<<"\nMain Statement"; By: Saqib Rasheed Air University

Largest Number among 3 Write a program in C++ that take input Que No 1 Write a program in C++ that take input of three integers numbers from user. Find the largest number among three of them. By: Saqib Rasheed Air University

Largest Number among 3 (Code) #include<iostream.h> void main () { int a,b,c, larg; cout<<"Enter First Integer="; cin>>a; cout<<"Enter Second Integer="; cin>>b; cout<<"Enter Third Integer="; cin>>c; By: Saqib Rasheed Air University

Largest Number among 3 (Code) if (a > b) larg = a; else larg = b; if (larg > c ) cout<<"Largest is ="<<larg<<endl; cout<<"Largest is ="<<c<<endl; } By: Saqib Rasheed Air University

Largest Number among 3 (Out Put) Enter First Integer=10 Enter Second Integer=25 Enter Third Integer=38 Largest is =38 Press any key to continue By: Saqib Rasheed Air University

Nested if else Que No 2 Write a program in C++ using if/else operator with nested statements to find the grade of a student . marks >= 90  Grade A marks >= 80  Grade B marks >=70  Grade C marks >=60  Grade D By: Saqib Rasheed Air University

Nested if else (Code) #include<iostream.h> void main () { int marks; cout<<"Enter the grade of student="; cin>>marks; if ( marks >= 90 ) cout<< " Grade A \n"; else if (marks >= 80 ) cout<<" Grade B \n"; By: Saqib Rasheed Air University

Nested if else (Code) else if ( marks >=70 ) cout<<" Grade C \n"; else if (marks >=60) cout<<" Grade D \n"; else { cout<<" Grade F \n"; cout<<" You have to take the classes again\n"; cout<<" Work Hard To Get Good Grade\n"; } By: Saqib Rasheed Air University

Nested if - else int n1,n2,n3; cout << "Enter three integers: "; cin >> n1 >> n2 >> n3;` if (n1 < n2) if (n1 < n3) ` cout << "Their minimum is " << n1 << endl; else cout << "Their minimum is " << n3 << endl; else if (n2 < n3) cout << "Their minimum is " << n2 << endl; By: Saqib Rasheed Air University

Nested if-else main( ) { int i ; cout<< "Enter either 1 or 2 "; cin>>i ; if ( i == 1 ) cout<<"You would go to heaven !"; By: Saqib Rasheed Air University

Nested if-else else { if ( i == 2 ) cout<<"Hell was created with you in mind"; cout<<"How about mother earth !"; } By: Saqib Rasheed Air University

Forms of if The if statement can take any of the following forms: (a) if ( condition ) do this ; (b) if ( condition ) { and this ; } By: Saqib Rasheed Air University

Forms of if -else (c) if ( condition ) do this ; else By: Saqib Rasheed Air University

Forms of if -else (d) if ( condition ) { do this ; and this ; } else By: Saqib Rasheed Air University

Forms of if -else (e) if ( condition ) do this ; else { if ( condition ) and this ; } By: Saqib Rasheed Air University

Forms of if -else (f) if ( condition ) { if ( condition ) do this ; else and this ; } By: Saqib Rasheed Air University

Logical Operators AND && OR || Not ! Bitwise Operator | & By: Saqib Rasheed Air University

Logical Operators If a is greater than b AND c is greater than d In C++ if(a > b && c> d) if(age > 18 || height > 5) By: Saqib Rasheed Air University

The Conditional Operators The conditional operators ? and : are sometimes called ternary operators since they take three arguments expression 1 ? expression 2 : expression 3 By: Saqib Rasheed Air University

Example of Condition Operator int x, y ; cin>>x; y = ( x > 5 ? 3 : 4 ) ; This statement will store 3 in y if x is greater than 5, otherwise it will store 4 in y. By: Saqib Rasheed Air University

Hierarchy of Operators By: Saqib Rasheed Air University

Vowel / Consonant Que No 3 Write a program in C++ to input a single character and print a message“ It is vowel" if it is vowel otherwise print message "It is a "consonant“ Use if-else structure and OR (||) operator only By: Saqib Rasheed Air University

Vowel / Consonant (Code) #include<iostream.h> void main() { char input; cout<<"Input a single character-->"; cin>>input; if (input == 'a' || input == 'e'|| input == 'i' ||input == 'o'|| input == 'u‘) cout<<"Its a VOWEL\n“; else cout<<"Its a CONSONANT\n“; } By: Saqib Rasheed Air University

Odd/ Even Que No 4 Write a Program in C++ that take an Integer value’s from the user and tell that the number Is EVEN or ODD By: Saqib Rasheed Air University

Odd/ Even (Code) int value; cout<<"Enter an Interger value "; cin>>value; if (value % 2 == 0) cout<<"Your number is Even\n"; else cout<<"Your number is Odd\n“; By: Saqib Rasheed Air University

Small / Capital Letter Que No 5 Write a program in C++ that take a single character from the user, and tells it's a Small Letter or it's a CAPITAL letter using nested if statement only By: Saqib Rasheed Air University

Small / Capital Letter (Code) char letter; cout<<"Enter the letter in \"CAPITAL\" or in \"SMALL\"-->"; cin>>letter; if (letter >='A' && letter <= 'Z') { cout<<"\n\nYou Entered a Capital Letter\n"; if (letter == 'a' || letter == 'e'|| letter == 'i' || letter == 'o'|| letter == 'u' || letter == 'A' || letter == 'E'|| letter == 'I' || letter == 'O'|| letter == 'U') cout<<"\n\nIts a VOWEL\n"; else cout<<"\n\nIts a CONSONANT\n“; } By: Saqib Rasheed Air University

Small / Capital Letter (Code) else if (letter >= 'a' && letter <= 'z') { cout<<"\n\nYou Entered a Small Letter\n"; If (letter == 'a' || letter == 'e'|| letter == 'i' || letter == 'o'|| letter =='u' || letter == 'A' || letter == 'E'|| letter == 'I' || letter == 'O'|| letter == 'U') cout<<"\n\nIts a VOWEL\n"; else cout<<"\n\nIts a CONSONANT\n“; } else cout<<"\n\nIts Not a letter"; cout<<endl; return 0; } By: Saqib Rasheed Air University

Condition Operator (?:) Que No 6 Write a Program in C++ to input a single letter in a char variable. If "m" is input print "You are Male” otherwise "You are Female" by Using Condition Operator (?:) By: Saqib Rasheed Air University

Condition Operator (?:) #include<iostream.h> void main () { char gender; cout<<"Enter the Gender of the Person="; cin>>gender; /*(?:) First Value is Printed if the condition is true and if it is false second condition is printed*/ cout<<( gender>= 'm' ? "You are Male\n" : "You are Female\n"); } By: Saqib Rasheed Air University

Temperature Program Que No 7 Make a program in C ++ that tells the form of Water whether it is Ice, Water or Steam. Display the menu also as under. Temperature Less than 0 = ICE Temperature Greater than 0 & Less than 100 = Water Temperature Greater than 100 = STEAM By: Saqib Rasheed Air University

Temperature Program int t; cout<<"Temperature Less than 0 = ICE \n" <<"Temperature Greater than 0 & " <<"Temperature Less than 100 = Water\n" <<"Temperature Greater than 100 = STEAM\n"; cout<<"\n\n\t\tPlease enter the Temperature="; cin>>t; By: Saqib Rasheed Air University

Temperature Program if ( t <= 0 ) cout<<"Form of water is \"ICE\""<<endl; else if( t > 0 && t < 100 ) cout<<"Form is \"WATER\"\n"; if ( t >= 100 ) cout<<"Form of water is \"steam\"\n"; By: Saqib Rasheed Air University

Assignment Write a program to calculate the salary as per the following table By: Saqib Rasheed Air University