fekra c++ course #2
DESCRIPTION
Fekra Course c++TRANSCRIPT
FLOW CONTROL-1
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
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
++x , x++ ?
• x++ execute the statement then increment x
• Ex: int x = 6cout<<x++; ----> print 6 on screencout<<x; ----> print 7 on screen
++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
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;
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
Flow Charts
• Standard way to express algorithms with drawings.
• Easy to make, easy to understand.
Flow Charts (cont)
• The beginning of the algorithm starts with:
• And ends with:
Start
End
Flow Charts (cont)
• Parallelogram are used for input/ output operations:
• Rectangles are used for processing:
Take input X
X = X + 2
Flow Charts (cont)
• Arrows are used for expressing flow, e.g. moving from step to another:
Start
Print “Hello World!!”
End
Practice
• Make a flow chart for a program takes 2 numbers and computes average.
SolutionStart
End
average = (num1+num2)/2
Take input num2
Take input num1
Print “average is” average
Let’s calculate the average faster
• Translate the previous flow chart into C++ code.
Conditional Statements
• All conditional statements use the (True or False) of a condition, to determine the path of the program.
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 ?
Example
Is (grade>60)
?
Print “Passed”
Print “Failed”
If greater than 60
If less than 60
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
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
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;}
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;
}
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;}
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";
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.
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.
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";
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
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;}
Break
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).
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”;
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.
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”;
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)
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!.
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
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;
Let’s think about this..
Switch Statement• The switch provides multiple-selection statement to
perform many different actions based on the possible values of a variable or expression.
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?
• The preceding chunk of code could be written as follows:
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 ?
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
Any Questions ?!