0% found this document useful (0 votes)
4 views23 pages

Lecture-6 (26-09-2023)

The document discusses control structures in C++, focusing on arithmetic and comparison operators, as well as if-else statements. It explains operator precedence, the use of parentheses in expressions, and provides examples of if-else statements to find the maximum of three numbers. The content is aimed at teaching fundamental programming concepts in a structured manner.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views23 pages

Lecture-6 (26-09-2023)

The document discusses control structures in C++, focusing on arithmetic and comparison operators, as well as if-else statements. It explains operator precedence, the use of parentheses in expressions, and provides examples of if-else statements to find the maximum of three numbers. The content is aimed at teaching fundamental programming concepts in a structured manner.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

Problem Solving and

MA144:
Computer Programming

Lecture-6

Control Structures
Arithmetic Operators
Binary operators : + - * / %
 The % operator can not be applied to floating-point type
• C++ allows only one kind of parentheses ( ) in arithmetic
expressions.
• The other varieties are reserved for other purposes.
• If you omit parentheses, the computer will follow some
rules called precedence rules to evaluate an expression.

Highest precedence

Lowest precedence

Observe: 𝑥 3 = 𝑥 ∗ 𝑥 ∗ 𝑥
 Arithmetic operators are executed left to right
when operators have the same precedence

What is the output of the above program?


Combining Assignment Operator with
Arithmetic Operators
Variable Op= Expression
is equivalent to
Variable = Variable Op (Expression)
Comparison Operators

< <= > >= Highest precedence


Left-right associative
== != Lowest precedence
Control Structures
if if-else
if (Boolean expression)
if (Boolean expression) {
{ statement1;
statement1; statement2;
statement2; }
} else
{
Statement3; statement3;
Statement4; statement4;
}
Statement5;
Statement6;

A Boolean expression is any expression


that is either true or false.
Find a maximum number from the given three numbers.
Read three numbers from the keyboard using cin object.
Use this in arrays
Some observations on if-else statement
#include<iostream>
using namespace std;
int main()
{
int a,b;
cout<<"enter two numbers: ";
cin>>a>>b;
if (a<b);
else
cout<<"i am in else block";
return 0;
}
#include<iostream>
using namespace std;
int main()
{
int a,b;
cout<<"enter two numbers: ";
cin>>a>>b;
if (a<b)
{
}
else
cout<<"i am in else block";
return 0;
}
#include<iostream>
using namespace std;
int main()
{
int a,b;
cout<<"enter two numbers: ";
cin>>a>>b;
if (a<b);
cout<<"i am in if block";
else
cout<<"i am in else block";
return 0;
}
#include<iostream>
using namespace std;
int main()
{
int a,b;
cout<<"enter two numbers: ";
cin>>a>>b;
if (a<b)
cout<<"i am in if block";
else;

return 0;
}
#include<iostream>
using namespace std;
int main()
{
int a,b;
cout<<"enter two numbers: ";
cin>>a>>b;
if (a<b)
cout<<"i am in if block\n";
else;
cout<<"How crazy you are";
return 0;
}
Thank You

You might also like