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

Looping

This document provides an overview of an upcoming programming with C++ session. It outlines session rules, topics to be covered, and exercises. The session will cover codeforces, switches, looping (for and while loops), nested if statements, the ternary operator, and switch cases. Exercises are provided for each topic and include programs to check positive/negative numbers, find the max of two numbers, check vowels/consonants, and print weekdays from numbers. The next session topics may include nested loops, while loops, and do-while loops.

Uploaded by

Yousef Aldrisi
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views

Looping

This document provides an overview of an upcoming programming with C++ session. It outlines session rules, topics to be covered, and exercises. The session will cover codeforces, switches, looping (for and while loops), nested if statements, the ternary operator, and switch cases. Exercises are provided for each topic and include programs to check positive/negative numbers, find the max of two numbers, check vowels/consonants, and print weekdays from numbers. The next session topics may include nested loops, while loops, and do-while loops.

Uploaded by

Yousef Aldrisi
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 15

PROGRAMMING WITH C++

BY/ MOHAMED HASHEM


SESSION RULES

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 .

• Break; is used to stop the statement after each expression .


• Default case is looked like else in if statement.
EXERCISES
• Take 2 integers and char like(+,-,*,/) then print the result.
• Logic to check vowel or consonant.
• read weekday number and print weekday name.

• read gender (M/F) and print corresponding gender.


In Programming, sometimes there is a need to perform some operation more than once or (say) n number of
times, Loops come into use when we need to repeatedly execute a block of statements. 

For example: Suppose we want to print “Hello World” 10 times ???


LOOP
C++ programming language provides the following type of loops to handle looping requirements.
• for loop
Execute a sequence of statements multiple times and abbreviates the code that manages the loop variable.
• while loop
Repeats a statement or group of statements while a given condition is true. It tests the condition before
executing the loop body.
• do...while
loop Like a ‘while’ statement, except that it tests the condition at the end of the loop body.
• nested loops
You can use one or more loop inside any another ‘while’, ‘for’ or ‘do..while’ loop.
FOR - LOOP

• 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

• Print all numbers that can be divided by 3 or 4 from 1 up to 100


• Print all small letters
• Print factorial of an integer
• Take an integer then print all divisor of this number
• example1 user insert 8 then print 1 2 4 8
• example2 user insert 9 then print 1 3 9
• Let the user insert infinity numbers then print all numbers except 50 and stop when the user inserts negative
numbers.
• Print the max of 10 integers.

• Check if the number is prime or not.


• Print the digits of an integer.
• Reverse an integer.
NICE TO SEARCH NEXT SESSION

• While Loop
• Nestet Loop
• do-while

You might also like