Looping
Looping
Don’t forget:
★ ***** You have to study the previous session before the next session *****
★ It’s better to show your face and open camera.
★ Raise your hand if you want to ask a questions.
★ Stick with discussion time and don’t interrupt the session there is a time for Q / A after every topic
★ Attending sessions is not optional and it’s a part of your graduation
★ You have to attend with PC or Laptop, Phones are not allowed anymore.
SESSION 1 OUTLINE
• Codeforces https://codeforces.com/group/MWSDmqGsZm/contests
• Switch
• Looping (For, While)
• Exercises
NICE TO SEARCH NEXT SESSION
• Nested If
• Ternary operator
• switch case
Advanced
• Write program to sum number from 1 to N without for loop.
• Write Program to find remainder between two numbers without using % operator.
NESTED IF
• If expression 1 is true then get inside the first if :
• Then if expression 2 is true execute statement 1
• And if expression 2 is false execute statement 2
• If expression 1 is false then skip the first and second if .
TERNARY OPERATOR
• A ternary operator evaluates the test condition and executes a block of code based on the result of the
condition.
• The ternary operator takes 3 operands (condition, expression1 and expression2).
• If expression1 is true then expression2 is executed, else expression3 is executed .
EXERCISES
• Write program to take number print positive or negative.
• Write program to take two number and print max between them.
• Write program to take number then print even or odd.
SWITCH
SWITCH
• The switch statement allows us to execute a block of code among many alternatives.
• The expression is evaluated once and compared with the values of each case label.
• If there is a match, the corresponding code after the matching label is executed. For example, if the value of
the variable is equal to constant2, the code after case constant2: is executed until the break statement is
encountered.
• If there is no match, the code after default: is executed.
• Can not use more than one expression like if .
• Can not use relational operators like if .
• Only integer and chars value is allowed to use in switch .
• Statement 1 is executed (one time) before the execution of the code block.
• Statement 2 This statement gets evaluated ahead of each execution
of the loop body, and abort the execution if the given condition get false.
• Statement 3 is executed (every time) after the code block has been executed
and it gets executed after the loop body, ahead of the next condition evaluated.
EXERCISES
• Print even numbers from 1 to 100
• While Loop
• Nestet Loop
• do-while