44
FLOW CONTROL-1

Fekra c++ Course #2

Embed Size (px)

DESCRIPTION

Fekra Course c++

Citation preview

Page 1: Fekra c++ Course #2

FLOW CONTROL-1

Page 2: Fekra c++ Course #2

More Operators

• Check this operator equivalence:x = x + 5 ---> x+=5 x = x + y ---> x+=y x = x – z ---> x-=y x = x*y ---> x*=y x = x/y ---> x/=y x = x%y ---> x%=y

Page 3: Fekra c++ Course #2

2 important operators

• Instead of x= x+1 or x+=1, c++ has ++ operator.

• Ex: int x= 5;x++; ----> now x = 6or ++x ----> now x = 6

Page 4: Fekra c++ Course #2

++x , x++ ?

• x++ execute the statement then increment x

• Ex: int x = 6cout<<x++; ----> print 6 on screencout<<x; ----> print 7 on screen

Page 5: Fekra c++ Course #2

++x , x++ ? (cont)

• ++x increment x, then execute the statement.

• Ex: int x = 6;cout<<++x; ---> print 7 on screencout<<x; ----> print 7 on screen

Page 6: Fekra c++ Course #2

Example

• Write this program output on a paperint x =4,y=5;cout<<x++<<endl;cout<<y++<<endl;cout<<++x<<endl;cout<<++y<<endl;cout<<++x – y++<<endl;cout<<x<<endl;cout<<y<<endl;

Page 7: Fekra c++ Course #2

solution

int x =4,y=5;cout<<x++<<endl; --> 4cout<<y++<<endl; --> 5cout<<++x<<endl; --> 6cout<<++y<<endl; --> 7cout<<++x – y++<<endl; --> 0cout<<x<<endl; --> 7cout<<y<<endl; --> 8

Page 8: Fekra c++ Course #2

Flow Charts

• Standard way to express algorithms with drawings.

• Easy to make, easy to understand.

Page 9: Fekra c++ Course #2

Flow Charts (cont)

• The beginning of the algorithm starts with:

• And ends with:

Start

End

Page 10: Fekra c++ Course #2

Flow Charts (cont)

• Parallelogram are used for input/ output operations:

• Rectangles are used for processing:

Take input X

X = X + 2

Page 11: Fekra c++ Course #2

Flow Charts (cont)

• Arrows are used for expressing flow, e.g. moving from step to another:

Start

Print “Hello World!!”

End

Page 12: Fekra c++ Course #2

Practice

• Make a flow chart for a program takes 2 numbers and computes average.

Page 13: Fekra c++ Course #2

SolutionStart

End

average = (num1+num2)/2

Take input num2

Take input num1

Print “average is” average

Page 14: Fekra c++ Course #2

Let’s calculate the average faster

• Translate the previous flow chart into C++ code.

Page 15: Fekra c++ Course #2

Conditional Statements

• All conditional statements use the (True or False) of a condition, to determine the path of the program.

Page 16: Fekra c++ Course #2

Conditional in flow chart

• Rhombus are used to express conditional statements:

• Conditional statements has output 2 arrows one for YES and one for NO

Is X > 60 ?

Page 17: Fekra c++ Course #2

Example

Is (grade>60)

?

Print “Passed”

Print “Failed”

If greater than 60

If less than 60

Page 18: Fekra c++ Course #2

if-else Statement

if (expression)statement

-----------------------------------------------------------------------//The expression decides whether to implement the statement or notif (expression) {

//block of statements }else {

//block of statements }

True / False

Page 19: Fekra c++ Course #2

Equality and Relational Operators

C++ Operator Sample C++ example Meaning

> x > y X is greater than y

< x < y X is less than y

>= x >=y X is greater than or equal y<= x<=y X is less than or equal y

== x == y X equal to y

!= X != y X not equal to y

Notes: • “A syntax error will occur if any of the operators ==, !=, >= and <=

appears with spaces between its pair of symbols.”• In c++:

• False sometimes expressed by an integer zero• True sometimes expressed by any integer other than zero

Page 20: Fekra c++ Course #2

if-else Statement// If then.cpp : Defines the sample conditional expressions.#include <iostream>using namespace std;

int main(){

int grade;cout<<"Enter your grade: "<<endl;cin>>grade;if (grade>=50)

cout<<"Congrats, You passed ;)"<<endl;else

cout<<"See you next semester :( "<<endl;}

Page 21: Fekra c++ Course #2

Nested if else// If then.cpp : Defines the sample conditional expressions.#include <iostream>using namespace std;

int main(){

int grade;cout<<"Enter your grade: "<<endl;cin>>grade;if (grade>=50) if (grade >= 90)cout<<"Congrats, You are excellent ;)"<<endl;elsecout<<"Congrats, You passed ;)"<<endl;elsecout<<"See you next semester :( "<<endl;

}

Page 22: Fekra c++ Course #2

Else ifint main (){ int a; cin >> a; if( a == 10 ) { cout << "Value of a is 10" << endl; } else if( a == 20 ) { cout << "Value of a is 20" << endl; } else { cout << "Value of a is not matching" << endl; } cout << "Exact value of a is : " << a << endl; return 0;}

Page 23: Fekra c++ Course #2

if – else example

Int x = 4, y = 6;if ( x > 5 ) if ( y > 5 ) cout << "x and y are > 5";else cout << "x is <= 5";

Page 24: Fekra c++ Course #2

Dangling - Else

if ( x > 5 ) if ( y > 5 ) cout << "x and y are > 5";else cout << "x is <= 5";

if ( x > 5 ) if ( y > 5 ) cout << "x and y are > 5"; else cout << "x is <= 5";

These code fragments are not the same logically. Beware of dangling-else, so it’s recommended to use braces to identify the scope of the (if-else) block.

Page 25: Fekra c++ Course #2

Training 1

• Let’s help our faculty and make a program that takes your grade, and tells you whether you got A, B, C, D, F.

Page 26: Fekra c++ Course #2

Training 1 ans.

if ( studentGrade >= 90 ) // 90 and above gets "A" cout << "A";else if ( studentGrade >= 80 ) // 80-89 gets "B" cout << "B";else if ( studentGrade >= 70 ) // 70-79 gets "C" cout << "C";else if ( studentGrade >= 60 ) // 60-69 gets "D" cout << "D";else // less than 60 gets "F" cout << "F";

Page 27: Fekra c++ Course #2

Training 2

• Try to do a program that :– Take a number from user– The program see if this number is even or

odd– Then type a message on the screen says that

if the number is even or odd

Page 28: Fekra c++ Course #2

Training 2 answer

#include<iostream>Using namespace std;Int main(){

int x;cin>>x;If(x % 2 == 0 )

cout<<“number is even”<<endl;else

cout<<“number is odd”<<endl;}

Page 29: Fekra c++ Course #2

Break

Page 30: Fekra c++ Course #2

Logical Operators

• Logical operators that are used to form more complex conditions by combining simple conditions. The logical operators are && (logical AND), || (logical OR) and ! (logical NOT, also called logical negation).

Page 31: Fekra c++ Course #2

AND (&&) Operator

• Suppose that we wish to ensure that two conditions are both True before we choose a certain path of execution. In this case, we can use the && (logical AND) operator, as follows:if (isCar == true && speed >= 100 )

speedFine=400;

• To test whether x is greatest number from (x, y, z), you would write if ( (x > y) && (x > z))

cout<<“x is the largest number”;

Page 32: Fekra c++ Course #2

OR (||) Operator

• We use it when we have two paths, and if either one is true or both of them, a certain path of action happen.

if ( ( semesterAverage >= 90 ) || ( finalExam >= 90 ) ) cout << "Student grade is A" << endl;

• The && operator has a higher precedence than the || operator. Both operators associate from left to right.

Page 33: Fekra c++ Course #2

NOT (!) Operator

• Not Operator enables a programmer to "reverse" the meaning of a condition.

• The unary logical negation operator is placed before a condition when we are interested in choosing a path of execution if the original condition is false.

If(! (Age>=18) ) cout<<“you can’t get a driving license”;

Page 34: Fekra c++ Course #2

Training 3

• What is the final result of each statement, decide whether it’s (True – False):

• !( 1 || 0 ) • ( 1 || 1 && 0 ))• !( ( 1 || 0 ) && 0 )• !(1 == 0)

Page 35: Fekra c++ Course #2

Training 4

make a program that asks a BeDev Trainee if he skipped a session, and if he skipped an assignment, and about how many bonuses did he solve.• if he skipped an assignment and a session and:

– if he solved 6 bonus questions his score won’t change.– if he did not solve any bonus he will be kicked out.

• if he skipped only one of them (assignment, session):– If he solved 3 bonus question, his score won’t change.

• You are required to tell if his score will decrease, not decrease(stay as it is, or increase), or that he will be out!.

Page 36: Fekra c++ Course #2

Solution

bool skippedAssignment, skippedSession;int bonuses;

cout << "Did you skip an assignment? (1 for yes, 0 for no)"<< endl;cin >> skippedAssignment;

cout << "Did you skip a session? (1 for yes, 0 for no)"<< endl;cin >> skippedSession;

cout << "How many bonuses did you have?" << endl;cin >> bonuses;

Taking input from user

Page 37: Fekra c++ Course #2

Solution cont.

if(skippedAssignment && skippedSession){

if(bonuses == 0)cout << "You'll be kicked out of the course" << endl;

else if(bonuses < 6)cout << "Your score will decrease" << endl;

elsecout << "your score will not decrease" << endl;

}else if(skippedAssignment || skippedSession){

if(bonuses < 3)cout << "Your score will decrease" << endl;

elsecout << "your score will not decrease" << endl;

}else cout << "your score will not decrease" << endl;

Page 38: Fekra c++ Course #2

Let’s think about this..

Page 39: Fekra c++ Course #2

Switch Statement• The switch provides multiple-selection statement to

perform many different actions based on the possible values of a variable or expression.

Page 40: Fekra c++ Course #2

Switch statement cont.

• The previous code was not readable and hard to understand. There is an easier way to write this using the switch statement.

• Switch statement written as :switch(variable){case value_1:statement;break;case value_2:statement;break;default :statement;break;}

• what if we don’t write a break statement?

Page 41: Fekra c++ Course #2

• The preceding chunk of code could be written as follows:

Page 42: Fekra c++ Course #2

Training 5

• imagine now, you are making a software for a restaurant, and the user enters the code of the ordered food, Then prints the price of sold item.

• For example:• 1 -> Sandwich,• 2 -> Juice, • 3 -> water ,• 4 -> chocolate.

• How you will make this using a switch statement ?

Page 43: Fekra c++ Course #2

Training 6

• Let’s write our simple calculator 1. Take a number from user2. Take an operator (+, - , * , / )3. Take another number from the user4. Print out the result of the operation• Now check the division by zero

Page 44: Fekra c++ Course #2

Any Questions ?!