Upcoming SlideShare
×

# Fekra c++ Course #2

193
-1

Published on

Fekra Course c++

Published in: Education, Technology, Business
1 Like
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
Your message goes here
• Be the first to comment

Views
Total Views
193
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
6
0
Likes
1
Embeds 0
No embeds

No notes for slide
• Syntax error, the uncommented line “statement or not”
• Bonus questions: -what is the type of that error (symantec)  .5 points-find a syntax error (capital ‘I’ of int)  .5
• Imply the increase conditionsinput is taken as 0,1s
• ### Fekra c++ Course #2

1. 1. FLOW CONTROL-1
2. 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 --->
3. 3. 2 important operators • Instead of x= x+1 or x+=1, c++ has ++ operator. • Ex: int x= 5; x++; ----> now x = 6 or ++x ----> now x = 6
4. 4. ++x , x++ ? • x++ execute the statement then increment x • Ex: int x = 6 cout<<x++; ----> print 6 on screen cout<<x; ----> print 7 on screen
5. 5. ++x , x++ ? (cont) • ++x increment x, then execute the statement. • Ex: int x = 6; cout<<++x; ---> print 7 on screen cout<<x; ----> print 7 on screen
6. 6. Example • Write this program output on a paper int 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;
7. 7. solution int x =4,y=5; cout<<x++<<endl; --> 4 cout<<y++<<endl; --> 5 cout<<++x<<endl; --> 6 cout<<++y<<endl; --> 7 cout<<++x – y++<<endl; --> 0 cout<<x<<endl; --> 7 cout<<y<<endl; --> 8
8. 8. Flow Charts • Standard way to express algorithms with drawings. • Easy to make, easy to understand.
9. 9. Flow Charts (cont) • The beginning of the algorithm starts with: • And ends with: Start End
10. 10. Flow Charts (cont) • Parallelogram are used for input/ output operations: • Rectangles are used for processing: Take input X X = X + 2
11. 11. Flow Charts (cont) • Arrows are used for expressing flow, e.g. moving from step to another: Start Print “Hello World!!” End
12. 12. Practice • Make a flow chart for a program takes 2 numbers and computes average.
13. 13. Solution Start End average = (num1+num2)/2 Take input num2 Take input num1 Print “average is” average
14. 14. Let’s calculate the average faster • Translate the previous flow chart into C++ code.
15. 15. Conditional Statements • All conditional statements use the (True or False) of a condition, to determine the path of the program.
16. 16. 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 ?
17. 17. Example Is (grade>60) ? Print “Passed” Print “Failed” If greater than 60 If less than 60
18. 18. if-else Statement if (expression) statement ----------------------------------------------------------------------- //The expression decides whether to implement the statement or not if (expression) { //block of statements } else { //block of statements } True / False
19. 19. 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
20. 20. 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; }
21. 21. 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; else cout<<"Congrats, You passed ;)"<<endl; else cout<<"See you next semester :( "<<endl; }
22. 22. Else if int 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; }
23. 23. 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";
24. 24. 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.
25. 25. 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.
26. 26. 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";
27. 27. 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
28. 28. 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; }
29. 29. Break
30. 30. 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).
31. 31. 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”;
32. 32. 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.
33. 33. 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”;
34. 34. 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)
35. 35. 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!.
36. 36. 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
37. 37. 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; else cout << "your score will not decrease" << endl; } else if(skippedAssignment || skippedSession) { if(bonuses < 3) cout << "Your score will decrease" << endl; else cout << "your score will not decrease" << endl; } else cout << "your score will not decrease" << endl;