SlideShare ist ein Scribd-Unternehmen logo
1 von 44
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 --->
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
++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
++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
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;
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
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.
Solution
Start
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 not
if (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;
else
cout<<"Congrats, You passed ;)"<<endl;
else
cout<<"See you next semester :( "<<endl;
}
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;
}
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;
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;
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 user
2. Take an operator (+, - , * , / )
3. Take another number from the user
4. Print out the result of the operation
• Now check the division by zero
Any Questions ?!

Weitere ähnliche Inhalte

Was ist angesagt?

FP 201 Unit 2 - Part 3
FP 201 Unit 2 - Part 3FP 201 Unit 2 - Part 3
FP 201 Unit 2 - Part 3
rohassanie
 
Cs1123 5 selection_if
Cs1123 5 selection_ifCs1123 5 selection_if
Cs1123 5 selection_if
TAlha MAlik
 

Was ist angesagt? (18)

FP 201 Unit 2 - Part 3
FP 201 Unit 2 - Part 3FP 201 Unit 2 - Part 3
FP 201 Unit 2 - Part 3
 
Statement
StatementStatement
Statement
 
Cs1123 5 selection_if
Cs1123 5 selection_ifCs1123 5 selection_if
Cs1123 5 selection_if
 
C tutorial
C tutorialC tutorial
C tutorial
 
Csci101 lect04 advanced_selection
Csci101 lect04 advanced_selectionCsci101 lect04 advanced_selection
Csci101 lect04 advanced_selection
 
SQL-PL
SQL-PLSQL-PL
SQL-PL
 
C/C++ programming language Something Great/tutorialoutletdotcom
C/C++ programming language Something Great/tutorialoutletdotcomC/C++ programming language Something Great/tutorialoutletdotcom
C/C++ programming language Something Great/tutorialoutletdotcom
 
Structure in programming in c or c++ or c# or java
Structure in programming  in c or c++ or c# or javaStructure in programming  in c or c++ or c# or java
Structure in programming in c or c++ or c# or java
 
L3 control
L3 controlL3 control
L3 control
 
Python Programming Essentials - M11 - Comparison and Logical Operators
Python Programming Essentials - M11 - Comparison and Logical OperatorsPython Programming Essentials - M11 - Comparison and Logical Operators
Python Programming Essentials - M11 - Comparison and Logical Operators
 
Decision Making and Branching
Decision Making and BranchingDecision Making and Branching
Decision Making and Branching
 
Coding Style
Coding StyleCoding Style
Coding Style
 
Conditional statement c++
Conditional statement c++Conditional statement c++
Conditional statement c++
 
Ch3
Ch3Ch3
Ch3
 
Control structuresin c
Control structuresin cControl structuresin c
Control structuresin c
 
JavaScript Control Statements I
JavaScript Control Statements IJavaScript Control Statements I
JavaScript Control Statements I
 
Lecture04
Lecture04Lecture04
Lecture04
 
JavaScript Control Statements II
JavaScript Control Statements IIJavaScript Control Statements II
JavaScript Control Statements II
 

Andere mochten auch

Andere mochten auch (14)

Url and protocol
Url and protocolUrl and protocol
Url and protocol
 
Telnet
TelnetTelnet
Telnet
 
Web engineering
Web engineeringWeb engineering
Web engineering
 
Web engineering lecture 5
Web engineering lecture 5Web engineering lecture 5
Web engineering lecture 5
 
Structure of url, uniform resource locator
Structure of url, uniform resource locatorStructure of url, uniform resource locator
Structure of url, uniform resource locator
 
Web Engineering - Web Applications versus Conventional Software
Web Engineering - Web Applications versus Conventional SoftwareWeb Engineering - Web Applications versus Conventional Software
Web Engineering - Web Applications versus Conventional Software
 
Web Engineering
Web EngineeringWeb Engineering
Web Engineering
 
Telnet
TelnetTelnet
Telnet
 
Web Engineering
Web EngineeringWeb Engineering
Web Engineering
 
Telnet
TelnetTelnet
Telnet
 
TELNET Protocol
TELNET ProtocolTELNET Protocol
TELNET Protocol
 
Url Presentation
Url PresentationUrl Presentation
Url Presentation
 
Telnet
TelnetTelnet
Telnet
 
Web Engineering
Web EngineeringWeb Engineering
Web Engineering
 

Ähnlich wie Fekra c++ Course #2

Java Programmin: Selections
Java Programmin: SelectionsJava Programmin: Selections
Java Programmin: Selections
Karwan Mustafa Kareem
 
FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2
rohassanie
 
JPC#8 Introduction to Java Programming
JPC#8 Introduction to Java ProgrammingJPC#8 Introduction to Java Programming
JPC#8 Introduction to Java Programming
Pathomchon Sriwilairit
 

Ähnlich wie Fekra c++ Course #2 (20)

lesson 2.pptx
lesson 2.pptxlesson 2.pptx
lesson 2.pptx
 
Control Structures, If..else, switch..case.pptx
Control Structures, If..else, switch..case.pptxControl Structures, If..else, switch..case.pptx
Control Structures, If..else, switch..case.pptx
 
Control statements in c
Control statements in cControl statements in c
Control statements in c
 
ICP - Lecture 7 and 8
ICP - Lecture 7 and 8ICP - Lecture 7 and 8
ICP - Lecture 7 and 8
 
The Three Basic Selection Structures in C++ Programming Concepts
The Three Basic Selection Structures in C++ Programming ConceptsThe Three Basic Selection Structures in C++ Programming Concepts
The Three Basic Selection Structures in C++ Programming Concepts
 
Control Statement.ppt
Control Statement.pptControl Statement.ppt
Control Statement.ppt
 
control structure
control structurecontrol structure
control structure
 
Acm aleppo cpc training introduction 1
Acm aleppo cpc training introduction 1Acm aleppo cpc training introduction 1
Acm aleppo cpc training introduction 1
 
cpphtp4_PPT_02.ppt
cpphtp4_PPT_02.pptcpphtp4_PPT_02.ppt
cpphtp4_PPT_02.ppt
 
Java Programmin: Selections
Java Programmin: SelectionsJava Programmin: Selections
Java Programmin: Selections
 
Programming Fundamentals Decisions
Programming Fundamentals  Decisions Programming Fundamentals  Decisions
Programming Fundamentals Decisions
 
Topic12Conditional if else Execution.ppt
Topic12Conditional if else Execution.pptTopic12Conditional if else Execution.ppt
Topic12Conditional if else Execution.ppt
 
Topic12ConditionalExecution.ppt
Topic12ConditionalExecution.pptTopic12ConditionalExecution.ppt
Topic12ConditionalExecution.ppt
 
FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2
 
JPC#8 Introduction to Java Programming
JPC#8 Introduction to Java ProgrammingJPC#8 Introduction to Java Programming
JPC#8 Introduction to Java Programming
 
cpphtp4_PPT_02.ppt
cpphtp4_PPT_02.pptcpphtp4_PPT_02.ppt
cpphtp4_PPT_02.ppt
 
Introduction to c part -1
Introduction to c   part -1Introduction to c   part -1
Introduction to c part -1
 
Python for Beginners(v2)
Python for Beginners(v2)Python for Beginners(v2)
Python for Beginners(v2)
 
Lecture 2
Lecture 2Lecture 2
Lecture 2
 
Ch3.1
Ch3.1Ch3.1
Ch3.1
 

Kürzlich hochgeladen

Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
Chris Hunter
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
PECB
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
heathfieldcps1
 

Kürzlich hochgeladen (20)

microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Asian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptxAsian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptx
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 

Fekra c++ Course #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. 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. ++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. ++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. 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. 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. Flow Charts • Standard way to express algorithms with drawings. • Easy to make, easy to understand.
  • 9. Flow Charts (cont) • The beginning of the algorithm starts with: • And ends with: Start End
  • 10. Flow Charts (cont) • Parallelogram are used for input/ output operations: • Rectangles are used for processing: Take input X X = X + 2
  • 11. Flow Charts (cont) • Arrows are used for expressing flow, e.g. moving from step to another: Start Print “Hello World!!” End
  • 12. Practice • Make a flow chart for a program takes 2 numbers and computes average.
  • 13. Solution Start End average = (num1+num2)/2 Take input num2 Take input num1 Print “average is” average
  • 14. Let’s calculate the average faster • Translate the previous flow chart into C++ code.
  • 15. Conditional Statements • All conditional statements use the (True or False) of a condition, to determine the path of the program.
  • 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 ?
  • 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. Break
  • 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. 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. 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. 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. 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. 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. 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. 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;
  • 39. Switch Statement • The switch provides multiple-selection statement to perform many different actions based on the possible values of a variable or expression.
  • 40. 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?
  • 41. • The preceding chunk of code could be written as follows:
  • 42. 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 ?
  • 43. Training 6 • Let’s write our simple calculator 1. Take a number from user 2. Take an operator (+, - , * , / ) 3. Take another number from the user 4. Print out the result of the operation • Now check the division by zero

Hinweis der Redaktion

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