< Computer Science Depart.
>
                                < Future Academy >
                                < Fall 2023   “Semester One”>
               {                < Level 1>
                                < Week 4>
                         < Section 04>
                     Relational Operators
                              &
                            Loops
< Eng.Taghreed Salem >
                         ...                                    }
               Relational Operators
•   They are used in Conditions (Decisions / Selection)
•   They are also used in Repetitions (Loops / Iterations)
•   They have Yes / No answers , i.e., either true or
     Relational Operators
>    x   is   greater than y?
<    x   is   less than y?
>=   x   is   greater than or equal to y?
<=   x   is   less than or equal to y?
              Loops(Repetitions/iterations )
         •   A loop is a repetition control structure. It
             causes a single statement or block of statements
{            to be executed repeatedly.
             For example:
             If we wish to read 100 numbers and compute the
             average.
    •   Repetition statements in C++:
                                                                    }
                                   Counter–Controlled Repetition
         For statement
                                   (Counter–Based Repetition)
         While statement
                                   .. or ..
         do..while statement
                                   Sentinel–Controlled Repetition
      Counter –Controlled Repetition
Defined repetition: Number of repetitions is known in advance.
An iteration=1 repetition
       General forms of Repetition
               Statements
              Expression 1 initialization: (k=1)
    We need : Expression 2 Continuation condition: (k<=10)
              Expression 3 increment : (k=k+1)
For(Expression 1 ; Expression 2 ; Expression 3 )
{
   Statement;
}
Expression 1 ;                      Expression1 ;
while (Expression 2 )               do
{                                   {
       Statement;                       statement;
                                         Expression
       Expression 3 ;                    3;
}                               } while (Expression 2) ;
      Repetition Statements
(the for Loop )
   for(initialization ; condition ; incrementation)
Repetition Statements
  #include <iostream>
  using namespace std;
  int main()
  {
     int i;
     for(i = 0; i < 5; i++) {
         cout << " i = "<< i <<endl;
         cout << "After Loop, i = " << i
     <<" ";
     }
  return 0;
  }
    Repetition Statements
(While   vs. Do-While Loops)
       Notes About Expressions
• Off-by-One Error:
If you wrote counter < 10 instead counter <= 10, then the loop will be
executed only 9 times.
• Expressions can contain arithmetic expressions:
For example, If x = 2 and y = 10 ..
for ( j = x + y; j <= 4 * x* y ; j += y / x)
• Increment may be negative > decrement
Notes about the Expressions..
• If the loop-continuation is
 initially false:
       loop body will not be executed.
       but, a do .. while’s body is executed at least once.
Notes about the Expressions..
 An increment expression is exactly similar to a standalone
 statement:
  •   counter = counter + 1;
  •   counter += 1;
  •   ++count;
  •   count++;
       Thus, are all equivalent.
Notes about the Expression
for statements..
Both expression 1 and expression 3 are comma-separated lists:
for ( x = 0 , y = 0 ;   x <= 10 ; x++ , y += 2 )
The commas as used here are comma operators ..
         for statement … An Example
Expressions in a for statement’s header are optional:
You can omit:
      Expression 1: if the control variable is initialized before using
                   the for statement.
     Expression 2: C++ assumes that the condition is true >> infinite loop
Expression 3 : If the increment is in the for’s body, or if its not needed
              for statement …
Examples of varying the control variable in a for statement:
    Vary the control variable from 1 to 100 in increments of
        1. for ( i = 1; i <= 100; i++ )
    Vary the control variable from 7 to 77 in steps of 7.
        for ( i = 7; i <= 77; i += 7 )
    Vary the control variable from 20 to 2 in steps
        of -2. for ( i = 20; i >= 2; i -= 2 )
    Vary the control variable over the following sequence of values:
    2, 5, 8, 11, 14, 17.
        for ( j = 2; j <= 17; j += 3 )
    Trace this code segment
Int count ;
count = 2;
while (count > 0)
cout<< "\n" << count       ;
count-- ;
cout<<"\n" <<“Done”    ;
                               Output:
    Trace this code segment
                                         2
Int count ;
count = 2;
while (count > 0)
cout<< "\n" << count       ;
count-- ;
cout<<"\n" <<“Done”    ;
                               Output:
    Trace this code segment
                                         2
Int count ;
count = 2;
while (count > 0)      True!
cout<< "\n" << count       ;
count-- ;
cout<<"\n" <<“Done”    ;
                               Output:
    Trace this code segment
                                         2
Int count ;
count = 2;
while (count > 0)
cout<< "\n" << count       ;
count-- ;
}
                                         2
cout<<"\n" <<“Done”    ;
                               Output:
    Trace this code segment
                                         2
Int count ;
                                         1
count = 2;
while (count > 0)
cout<< "\n" << count       ;
count-- ;
}
                                         2
cout<<"\n" <<“Done”    ;
                               Output:
    Trace this code segment
                                                 2
Int count ;
                                                 1
count = 2;
while (count > 0)              True!
{
cout<< "\n" << count       ;
count-- ;
}
                                                 2
cout<<"\n" <<“Done”    ;
                                       Output:
    Trace this code segment
                                         2
Int count ;
                                         1
count = 2;
while (count > 0)
cout<< "\n" << count       ;
count-- ;
}
                                         2
cout<<"\n" <<“Done”    ;                 1
                               Output:
    Trace this code segment
                                         2
Int count ;
                                         1
count = 2;
                                         0
while (count > 0)
cout<< "\n" << count       ;
count-- ;
}
                                         2
cout<<"\n" <<“Done”    ;                 1
                               Output:
    Trace this code segment
                                          2
Int count ;
                                          1
count = 2;
                                          0
while (count > 0)      False!
{
cout<< "\n" << count       ;
count-- ;
}
                                          2
cout<<"\n" <<“Done”    ;                  1
                                Output:
    Trace this code segment
                                          2
Int count ;
                                          1
count = 2;
                                          0
while (count > 0)
cout<< "\n" << count       ;
count-- ;
}
                                           2
cout<<"\n" <<“Done”    ;                   1
                                         Done
                               Output:
Calculate the sum of numbers from 1 to 2:
   {
   int sum = 0;
   int i;
   for ( i = 1; i <= 2 ; i++)
   {
   sum = sum + i; // or sum+=i;
   }
   cout<<“The sum is :”<<sum;
   return 0;
   }                              Note: i++ is the same as
                                  i = i + 1, and as i += 1.
                                        sum   i
                                         0
{
int sum = 0;
int i;
for ( i = 1; i <= 2 ; i++)
{
sum = sum + i; // or sum+=i;
}
                               Output
cout<<“The sum is :”<<sum;
return 0;
}
                                        sum   i
                                         0    1
{
int sum = 0;
int i;
for ( i = 1; i <= 2 ; i++)
{
sum = sum + i; // or sum+=i;
}
                               Output
cout<<“The sum is :”<<sum;
return 0;
}
                                        sum   i
                                         0    1
{
int sum = 0;
int i;         True
for ( i = 1; i <= 2 ; i++)
{
sum = sum + i; // or sum+=i;
}
                               Output
cout<<“The sum is :”<<sum;
return 0;
}
                                        sum   i
                                         0    1
{
int sum = 0;                             1
int i;         True!
for ( i = 1; i <= 2 ; i++)
{
sum = sum + i; // or sum+=i;
}
                               Output
cout<<“The sum is :”<<sum;
return 0;
}
                                        sum   i
                                         0    1
{
int sum = 0;                             1    2
int i;
for ( i = 1; i <= 2 ; i++)
{
sum = sum + i; // or sum+=i;
}
                               Output
cout<<“The sum is :”<<sum;
return 0;
}
                                        sum   i
                                         0    1
{
int sum = 0;                             1    2
int i;         True!
for ( i = 1; i <= 2 ; i++)
{
sum = sum + i; // or sum+=i;
}
                               Output
cout<<“The sum is :”<<sum;
return 0;
}
                                        sum   i
                                         0    1
{
int sum = 0;                             1    2
int i;         True!
                                         3
for ( i = 1; i <= 2 ; i++)
{
sum = sum + i; // or sum+=i;
}
                               Output
cout<<“The sum is :”<<sum;
return 0;
}
                                        sum   i
                                         0    1
{
int sum = 0;                             1    2
int i;                                   3    3
for ( i = 1; i <= 2 ; i++)
{
sum = sum + i; // or sum+=i;
}
                               Output
cout<<“The sum is :”<<sum;
return 0;
}
                                        sum   i
                                         0    1
{
int sum = 0;                             1    2
int i;         False!
                                         3    3
for ( i = 1; i <= 2 ; i++)
{
sum = sum + i; // or sum+=i;
}
                               Output
cout<<“The sum is :”<<sum;
return 0;
}
                                                sum   i
                                                 0    1
{
int sum = 0;                                     1    2
int i;                                           3    3
for ( i = 1; i <= 2 ; i++)
{
sum = sum + i; // or sum+=i;
}
                                Output
cout<<“The sum is :”<<sum;
return 0;
}                              The sum is : 3
The Sum of Even Values using a for Statement
  ..Sum the Even values from 2 to 100 ..
  int sum = 0; int number;
  for ( number = 2; number <= 100; number += 2 )
  {
  sum += number;
  }
  cout<<"Sum is :" <<sum ;
         Print the iterations using
                 the 3 ways
#include <iostream>
using namespace std;                       // Do-While Loop
int main() {                               cout << "\nDo-While Loop:" <<
    // For Loop                            endl;
    cout << "For Loop:" << endl;
                                           int k = 0;
    for (int i = 0; i < 5; i++) {
                                           do {
       cout << "Iteration " << i + 1
    << endl;
                                               cout << "Iteration " << k +
    }                                      1 << endl;
                                               k++;
   // While Loop                           } while (k < 5);
   cout << "\nWhile Loop:" << endl;
   int j = 0;                              return 0;
   while (j < 5) {                     }
       cout << "Iteration " << j + 1
   << endl;
       j++;
   }
Write a C++ program to PRINT the ASCII TABLE/CHART
{ ..
       “The more you practice
         the more you get”
                       Taghreed Salem
                             } ..
Thanks!
Do you have any questions?
    You can contact me on:
Taghreed.salem@fa-hists.edu.eg