Lecture 7 Computer Programming -1-. Conditional Statements 1- if Statement. 2- if ….. else Statement. 3- switch.

Slides:



Advertisements
Similar presentations
Fundamental of C programming
Advertisements

If Statements & Relational Operators Programming.
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.
Conditional Operator (?:) Conditional operator (?:) takes three arguments (ternary) Syntax for using the conditional operator:
1 10/16/06CS150 Introduction to Computer Science 1 switch Selection Structure.
Switch Switch case statements are a substitute for long if statements that compare a variable to several "integral" values ("integral" values are simply.
1 9/26/08CS150 Introduction to Computer Science 1 Logical Operators and if/else statement.
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.
Function tax and buy_one double tax (double price, double tr) { return price*tr; } double buy_one() { double p; cout > p;
The If/Else Statement, Boolean Flags, and Menus Page 180
1 9/28/07CS150 Introduction to Computer Science 1 Logical Operators and if/else statement.
Administrative MUST GO TO CORRECT LAB SECTION! Homework due 11:59pm on Tuesday. 25 points off if late (up to 24 hours) Cannot submit after 11:59pm on Wednesday.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
Quiz 1 Exam 1 Next Week. Nested if Statements if (myGrade >= 80) if (myGrade >= 90) cout
CHAPTER#3 STRUCTURED PROGRAM DEVELOPMENT IN C++ 1 st semester King Saud University College of Applied studies and Community Service Csc 1101.
Programming in C++ Lecture Notes 2 – Choice Statements Andreas Savva.
© Copyright 2013 by Pearson Education, Inc. All Rights Reserved.1 Chapter 3 Selections.
Selection Statements in C++ If Statement in C++ Semantics: These statements have the same meaning as in the algorithmic language. 2- Two way selection:
More on Input Output Input Stream : A sequence of characters from an input device (like the keyboard) to the computer (the program running). Output Stream.
Selection Structures (if & switch statements) (CS1123)
1 CS 1430: Programming in C++. 2 Literal Values Literal values of int Literal values of float
Basic Of Computer Science
1 CS 1430: Programming in C++. 2 IF Statement if (cond) statement //Next statement if (cond) { statement1 statement2 … } //Next statement.
Programming Fundamentals Lecture 4. In the Previous Lecture Basic structure of C program Variables and Data types Operators ‘cout’ and ‘cin’ for output.
Functions in C Programming Dr. Ahmed Telba. If else // if #include using namespace std; int main() { unsigned short dnum ; cout
CHAPTER 8 CONTROL STRUCTURES Prepared by: Lec. Ghader R. Kurdi.
Previously Repetition Structures While, Do-While, For.
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.
Perform Calculations and Control Flow Telerik Software Academy Learning & Development Team.
Chapter 05 (Part III) Control Statements: Part II.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
Lecture #7 CONTROL STRUCTURE & FLOW CHARTS
PROGRAM FLOW CHAPTER3 PART1. Objectives By the end of this section you should be able to: Differentiate between sequence, selection, and repetition structure.
Control structures Algorithm Development Conditional Expressions Selection Statements 1.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Compound Statements If you want to do more than one statement if an if- else case, you can form a block of statements, or compound statement, by enclosing.
111/18/2015CS150 Introduction to Computer Science 1 Announcements  I have not graded your exam yet.
CS Class 08 Today  Exercises  Nested loops  for statement  Built-in functions Announcements  Homework #3, group solution to in-class.
CSC1201: Programming Language 2 Lecture 1 Level 2 Course Nouf Aljaffan (C) CSC 1201 Course at KSU1.
Lecture 7: Making Decisions Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
C++ Programming Lecture 7 Control Structure I (Selection) – Part II The Hashemite University Computer Engineering Department.
Statements
Simple Data Types Chapter Constants Revisited t Three reasons to use constants –Constant is recognizable –Compiler prevents changes in value.
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,
1 CS 101 Fall 2001 Lecture 3. 2 Boolean expressions The expressions that are allowed to be in the parentheses following an if can be of the form x>y,
Selection Executing Statements Selectively. Review We’ve seen that the C++ if statement permits a Statement to be executed selectively: if (Expression)
LESSON 4 Decision Control Structure. Decision Control Structures 1. if statement 2. if-else statement 3. If-else-if statement 4. nested if statement 5.
Think First, Code Second Understand the problem Work out step by step procedure for solving the problem (algorithm) top down design and stepwise refinement.
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Conditions - compare the values of variables, constants and literals using one or more relational.
STRUCTURED PROGRAMMING Selection Statements. Content 2  Control structures  Types of selection statements  if single-selection statement  if..else.
Control Structures Selection: else / if and switch Dr. Ahmed Telba.
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Condition – any expression that evaluates to true/false value Relational operators are BINARY.
LECTURE # 7 : STRUCTURED PROGRAMMING Selection Statements Tr.Hadeel.
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)
Asif Nawaz University Institute of Information Technology, PMAS-AAUR Lecture 05: Object Oriented Programming:2014 Object-Oriented Programming in C++ Exception.
Introduction to Computer Programming
MT262A Review.
LESSON 4 Decision Control Structure
Programming Fundamentals
Decision Making.
Compound Assignment Operators in C++
Summary Two basic concepts: variables and assignments Basic types:
By Hector M Lugo-Cordero September 3, 2008
Branching statements Kingdom of Saudi Arabia
C++ Programming Lecture 7 Control Structure I (Selection) – Part II
Presentation transcript:

Lecture 7 Computer Programming -1-

Conditional Statements 1- if Statement. 2- if ….. else Statement. 3- switch.

A- If …… statement - The if statement is used to check a condition and if the condition is true, we run a block of statements Syntax : 1\ If ( condition ) { statement } example: if (x>y) cout<<"X is greater than Y“ ;

- program to enter number, and print message if the number >=100 #include int main( ) { int x; cout<<"enter x:"; cin>>x; if ( x>=100) cout<<"large value"; return 0; }

2\ if (condition) { Statement 1; Statement 2; …..………… Statement n; } example: if (x>y) { cout<<"big number is:" <<x; cout<<"\n small number is:" <<y; }

- This program to increase the salary of the employee 10% if its salary less than or equal 300 or its rank greater than or equal 4 #include int main() { int rank; float salary; cout<<"Enter the salary and rank:"; cin>>salary>>rank; if ((salary =4)) { salary=salary*0.010; cout<<"salary after increasing \n"; cout<<salary<<endl; } return 0; }

B- if…….. else Statement used to check a condition and if the condition is true, we run a block of statements, else we process another block of statements. Syntax : if (condition) statement; else statement; example: if(x==100) cout<<"x is 100"; else cout <<"x is not 100";

This program determine if the entered number is even or odd.. #include int main() { int a; cout<<"Enter the number:"; cin>>a; if (a%2= =0) cout<<" the number is even"; else cout<<"the number is odd"; return 0; }

- Write a program read tow numbers and determine which number is the greatest.. #include int main() { int a,b; cout<<"Enter tow different number:"; cin>>a>>b; if (a>b) cout<<a<<" number is greater "<<b; else cout<<b<<" number is greater "<<a; return 0; }

(Nested if) complex if statement تعود else الى if(number>5) if (number > 0) { if (number > 5) cout<<"the number is greater than 5"; else out<<" the number is less than 5"; } if ( (number > 0) && if (number > 5) ) { cout<<"the number is greater than 5"; else cout<<" the number is less than 5"; }

- This program determine if the entered number is even or odd.. #include int main() { int number; cin>> number; if (number>0) if (number%2==0) cout<<"the number is positive even"; else cout<<" the number is positive Odd"; else cout<<"the number is less than zero"; } تعود else الأخيرة الى if(number>0)

if – else – if Syntax : if (condition1) statement1; else if(condition2) statement2; else if(condition3) statement3; …… else if(condition n) statement n; else statement;

Example #include int main() { int n; cout << " Enter the number: "; cin >> n; if (n== 1) cout << " Sunday : Programming(1) " <<endl ; else if (n== 2) cout << " Monday : English " << endl ; else if (n== 3) cout << " Tuesday : Introduction" << endl; else if (n== 4) cout << " Wednesda y : Math " << endl; else if (n== 5) cout << " Thursday : Islamic " << endl; else cout << " nothing " <<endl ; return 0; }

Write a C++ program by using if statement To simulate the calculator such that when you enter tow numbers and the mathematical operation(+,-,*,%,\) you get a result ? #include int main() { double a,b ; char x ; cout << "enter the operation : " ; cin >> a >>x >> b ; if (x=='+') cout << " a+b = " << a+b << endl ; else if (x=='-') cout<< "a-b = " << a-b << endl ; else if (x=='*') cout << "a*b = " << a*b << endl ; else if (x=='/') cout << "a/b = " << a/b << endl ; else cout << "Unknown operation" << endl ; return 0; }

Write a C++ program by using if statement to calculate the grades of students ? #include int main() { float score ; cout << "Enter your score : "; cin >> score ; if (score >= 90) cout << "Your Grade is A " << endl; else if (score >= 80) cout << "Your Grade is B " << endl ; else if (score >= 70) cout << "Your Grade is C " << endl ; else if (score >= 60) cout << "Your Grade is D " << endl ; else if (score >= 0) cout << "Your Grade is F " << endl ; else cout << "Error: Score can't be less than 0 " << endl ; return 0; }

Switch statement A switch statement allows a variable to be tested for equality against a list of values. Each value is called a case, and the variable being switched on is checked for each case.

Switch statement SyntaxExample Switch (Expression or variable) { case constant 1 : statement 1; break; case constant 2 : statement 2; break; ……. case constant x : statement x; break; default : statement; } switch (x) { case 0 : cout << "Zero !" << endl; break; case 1 : cout << "One !" << endl; break; case 2 : cout << "Two !" << endl; break; default : cout << "Error !" << endl; }

- Write a C++ program by using switch statement To simulate the calculator such that when you enter tow numbers and the mathematical operation (+,-,*,\) you get a result ? #include int main() { float x, y ; char op ; cout<< " enter first number and math operator and second number : " ; cin >> x >> op >> y ; switch (op) { case '+' : cout<< "x+y = " << x+y << endl ; break ; case '-' : cout<< "x-y = " << x-y << endl ; break ; case '*' : cout<< "x*y = " << x*y << endl ; break ; case '/ ' : cout<< "x/y = " << x/y << endl ; break ; default : cout << " incorrect operation " << endl ; } return 0 ; }

- Write a C++ program by using switch statement to print the name of the color depending on the character input. #include int main() { char c ; cout<< " enter character : " ; cin >> c ; switch (c) { case 'b' : cout << "blue" << endl ; break ; case 'g' : cout << "green" << endl ; break ; case 'r' : cout << "red" << endl ; break ; case 'w' : cout << "white" << endl ; break ; default : cout << " unknown char " << endl ; } return 0 ; }

- Write a C++ program by using switch statement to print the days of the week. #include int main() { int weekday ; cout<< " enter number : " ; cin >> weekday ; switch (weekday) { case 1 : cout<< "Saturday" << endl ; break ; case 2 : cout<< "Sunday" << endl ; break ; case 3 : cout<< "Monday" << endl ; break ; case 4 : cout<< "Tuesday" << endl ; break ; case 5 : cout<< "Wednesday" << endl ; break ; case 6 : cout<< "Thursday" << endl ; break ; case 7 : cout<< "Friday" << endl ; break ; default : cout << " unknown char " << endl ; } return 0 ; }