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

Fundamentals of Programming - 1 02 Class Notes DECODE DSA With C 2.0 64e49f97f4cb5500180ad522

The document is a lecture on control statements in programming, focusing on if-else, ternary, and switch statements. It includes code examples, output predictions, and exercises for students to practice these concepts. The lecture also covers practical applications such as determining pass/fail based on marks and creating a simple calculator using switch cases.
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)
28 views22 pages

Fundamentals of Programming - 1 02 Class Notes DECODE DSA With C 2.0 64e49f97f4cb5500180ad522

The document is a lecture on control statements in programming, focusing on if-else, ternary, and switch statements. It includes code examples, output predictions, and exercises for students to practice these concepts. The lecture also covers practical applications such as determining pass/fail based on marks and creating a simple calculator using switch cases.
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
compact
~Cool s
if-else

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


a
~#include<iostream>
. value of X:

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