0% found this document useful (0 votes)
7 views

L3 - Control Flow (if-else & Loops)

Uploaded by

Krishna Bade
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

L3 - Control Flow (if-else & Loops)

Uploaded by

Krishna Bade
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

L3

Control flow (if-else & loops)

Join Discord - https://tinyurl.com/ly-discord


Let’s suppose you want the computer to decide whether someone will
pass or fail in exam based on marks entered.

How do you think we can do it ?

E mar-1
Marks >
- 39
pass

Fail
What are conditionals statements
and their use ?


conditional statement
decision >
-

if (conditions if (marks > =


40)
E E
-

statement
;
-

3
3
If
=

int n 2 14 ;

it (
% = = =
)
S
S 0
.
·
Pen (Number is divisible by 7");
3
If - Else
~ if (condition (
E

3 S
if (n % 2 = =
0) odd
. 0
5 .
In (Even") ;
Vabe
S 3
ebe

3 S .

0 ·
th (6 Odd ") ;
3
If - else - if Ladder

↑ multiple conditi
a ne

if (condition)
S
statement I ;

3
ele if (condition 2)
E
statement 2 ; day
3
ebe
&
statement 3
;
3
Max of 2 numbers

int a 2 S
;

int b 2 6;

if (a >
b)
&
S o .
·
Ph (a - is manimum") ;
3
ebe
S
S . 0 .
Blu (b + "is manimum");
Y
Max of 3 numbers

is
& as = be

if(a = = b22a) =
c)
E
. Pen (a) ;
S 0 .

3
&& b )
if (b)
>
ele = a =

S 0 He
.
·

(b) ;
3
an
Ternary Operator >
- ?:
if (condition(
T
E

S -
Statement;

3 Al
abe boolean a = (n % 2 = = 0 ? tree fase)

Gi
-
-

&
statement 2 ;

Y
Watermelon problem Codeforces

https://codeforces.com/problemset/problem/4/A
What is Switch Case ?
day , .
= 1 2 3
, ,

value I

↓ -
switch (expression
-

7 if-ebe S
value
ch case value 1
-
:

T
statement
3
2
X
case value 2 :

2 -
-
Statement
break ;
Why to use Loops ?

-op
Task

↓De
a

I
point

Loop


-100
For Loop

fa in ↓
test

t

initialization
condition
update
part expression
3

A
1 - 2 -3 - 4 -S
For Loop
-

socintinsi
E

D ↓
fasse
While Loop
initialization

S
initialization statement
test condition
while (rest condition
E update
statement

update expression

3
#ST e
fase

int i 2 1 ; ass
true

(5)
Mi
while

S 0
.
.

Plu (i) ;
EP2
i2 i + 1
% in 2 z ,
3

i
Do-While Loop

I
initialization statement

do
&

statement
update expression

3 While (test-condition) ;
↑ I Hello i =
2 2 <

inti2
,
,
1 ; ,

Hello i 315
iz 2
2 3

do
, ,
,

in 4 4725
i2 3
,
Hello , ,

S 0
.
.
De (Hello") : is 4 Hello , ins ,
sus
,

in 6

se
icie1 ; its Hello , ,
,

3 While (i < 25) ;


STOP
-
-
Homework Questions

1. Program to check whether a number is negative, positive or zero.


2. Program to check whether a number is divisible by 5 and 11 or
not.
3. Program to check whether a number is even or odd.
4. Program to find sum of all even numbers between 1 to n.
5. Program to find sum of all odd numbers between 1 to n.
6. Program to find HCF (GCD) of two numbers.
7. Program to find LCM of two numbers.
Thank You!

Please practice more questions and examples as above !!

You might also like