0% found this document useful (0 votes)
27 views22 pages

C++ Lecture 4 PPT PDF

The document discusses control statements in C++, focusing on if-else, ternary operators, and switch statements. It includes code examples to predict outputs and exercises for implementing basic functionalities like checking student marks, determining days of the week, and creating a simple calculator. The content is structured as a lecture by Raghav Garg, providing both theoretical explanations and practical coding scenarios.

Uploaded by

dopana9776
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)
27 views22 pages

C++ Lecture 4 PPT PDF

The document discusses control statements in C++, focusing on if-else, ternary operators, and switch statements. It includes code examples to predict outputs and exercises for implementing basic functionalities like checking student marks, determining days of the week, and creating a simple calculator. The content is structured as a lecture by Raghav Garg, providing both theoretical explanations and practical coding scenarios.

Uploaded by

dopana9776
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/ 22

If else ,ternary & switch

Lecture- 4

Raghav Garg
Control Statements
(continued)
Predict the output Output

1.
#include<iostream>
using namespace std;
int main() {
int x = 10, y = 20 ;
if (x == y) ;
cout << x << ” “ << y ;
return 0;
}
Predict the output Output
3
int main() {
·

int x = 3, y = 5 ; ·
S

if (x == 3) d

cout << x << endl;


else ;
cout << y << endl;
return 0;
}
Unary

Binary banner
~Cool if-else compact
s

Ternary Operator
Ternary

expression 1 ? expression 2 : expression 3


condition 3true:
false
Ques : Given the marks of the student. If the marks
are greater than 33 print the result as pass
otherwise fail without using if-else statement.
int marks;

cin marks;

if (marke (33) cont<< "Pass";

else cont"Fail";
A

Predict the output Output


X:a
~#include<iostream>
. value of
w using namespace std; ·
Value of a: 2

Ca
w int main(){ 2

Wchar x; X
a

~int a = 2;
Wx = (a > 0) ? 'a' : 'S';
~cout << "Value of x : " << I
x << endl;
Wcout << "Value of a : "<< a;

return 0;
}
int x;

int y 3, =

if (y > 4)

X = 1;

else
X = 2,
**
Predict the output 11
24
1! 2
=
< 5 =


1!=1
int main(){ ⑯ I -S
↓ L

int x;
X 1
- 30

~x = 5 > 8 ? 10 : 1 != 2 < 5 ? 20 : 30;


-cout<<"Value of x : "<<x;
return 0;
} Output
Value of X:30
Predict the output
Output
int main(){
o
test
M O
f
int test = 0;
float f = 3.111;
cout << (test > 0 ? f : 0) << endl;
return 0;
}
Switch Statement
alternative of
if-dee statement
S
Intax
switch ( integer expression ) { Switch (X)[
case constant 1 :

I
do this ; case I:

cont"Ok";
case constant 2 :
do this ; case 2:

case constant 3 : cout"Hello";


do this ; case 3:
default : cont<< "Bye";
do this ;
S
}
Ques : Write a program to input week number(1-7)
and print day of week name using switch case.
I -Monday
2 -
Tues

3 - Wed

y, Thurs

3 - Fri

6 -
Sat

1 esun
Ques : Write a program to input month number and
print total number of days in month using switch
case.
1 s Jane 31 Switch (condition) [
case 1:

2 - Feb 28 do this

March 31 3
3 -
-

30
4 - April

31
3 -
May -

6 - June 30

31
7
-
July -
*

Ques : Write a program to create a calculator that


performs basic arithmetic operations (add,
subtract, multiply and divide) using switch case
and functions. The calculator should input two
numbers and an operator from user. +, 7, I
-,

int a, bi

in a bi

charch;
inch;
Predict the output output
102 0
10y
= =

x =

int main() {
~int x = 3, y, z ;
Wy = x = 10 ;
-> O
-z = x < 10 ;
~cout << "x = “ << x << “y = “ << y << ”z = " << z ;

return 0;
}
Output
Predict the output

I %
#include<iostream>

using namespace std;


As o

int main( ) {
~int k = 35 ;

~cout <<( k == 35) <<endl << (k == 50) <<endl << (k > 40 ) ;

return 0;

}
Output

I
Predict the output
#include<iostream>

using namespace std;


S
K
..
·30

int main( ) {
vint k = 35 ;

Wcout <<( k == 35) <<endl << (k == 50) <<endl << (k > 40 ) ;


-

return 0;

}
Predict the output
amaw
to i

I
#include<iostream>
using namespace std;
int main( ) {
int i = 65 ;
char j = 'A' ;
if ( i == j )
cout << "P stands for PhysicsWallah" ;
else
cout<<"P stands for pwskills" ;
return 0;
}
The Real Thing: Output

I
·
This works
if ( 3 + 2 % 5 )
Even this works
cout << "This works" ; endl;
·

if ( a = 10 ) a
surprisingly even

cout << "Even this works" ; endl; this works

if ( -5 )
endl
cout << "Surprisingly even this works" 46
;
The Real Thing:

if ( condition )

statement ;

if ( expression )

statement ;
THANK YOU
Looks, coppe, PP-1, PP-2

Problems

You might also like