0% found this document useful (0 votes)
38 views26 pages

Week 4

The document discusses various control structures in programming including relational operators, logical operators, boolean expressions, and selection and repetition structures like if, if-else, switch, while, do-while, for statements. It provides examples of each control structure and their usage along with brief explanations. Self-tests are included to help understand the different structures.

Uploaded by

José Manuel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views26 pages

Week 4

The document discusses various control structures in programming including relational operators, logical operators, boolean expressions, and selection and repetition structures like if, if-else, switch, while, do-while, for statements. It provides examples of each control structure and their usage along with brief explanations. Self-tests are included to help understand the different structures.

Uploaded by

José Manuel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 26

Control Structures

Jeanine Ingber
Relational Operators

 == equality
 != non equality
 < less than
 > greater than
 <= less than equal to
 >= greater than equal to

Jeanine Ingber
Logical Operators

 ! not
 && and
 || or

Jeanine Ingber
Boolean Expressions

 X>0
 y==0
 x>0 && y==0

Jeanine Ingber
Control Structures

 Selection
 if
 if else
 switch
 repetition
 while
 do while
 for

Jeanine Ingber
If statement

 if(Boolean expression)
statement; //single statement
 if(Boolean expression)
{ //more than one statement
statement1;
.
statement n;
}

Jeanine Ingber
If statement - examples
if (x>0)
cout << sqrt(x);

if(x>0)
{
x=sqrt(x);
cout << x;
{

Jeanine Ingber
if - else statement

 if(Boolean expression)
statement;
else
statement;
 if(Boolean expression)
{
statement block
}
else
{
statement block
}

Jeanine Ingber
if-else examples
1) if(x>=0)
cout << sqrt(x);
else
cout << “x is negative”;

2) if(grade >=90)
cout << “grade is A”;
else if(grade >= 80)
cout << “grade is B”;
else if(grade >= 70)
cout << “grade is C”;
else
cout << “grade is F”;

Jeanine Ingber
Self - tests

Jeanine Ingber
Switch Statement
•Multiway selection statement
•example
switch(expression)
{
case constant:
statement(s);
break;
case constant:
statement(s);
break;
default: //optional
statement(s);
}
Jeanine Ingber
Switch Statement

 Expression must be of type integer or


character
 The keyword case must be followed by a
constant
 break statement is required unless you
want all subsequent statements to be
executed.

Jeanine Ingber
Self-test

Jeanine Ingber
repetition

 while statement
 do while statement
 for statement

Jeanine Ingber
while statement
 while(expression)
statement;
 while(expression)
{ //braces needed for multiple statements
statement;
statement;
.
}
 Statements are repeated while expression is true. If expression is false
at the first test, statement(s) are not executed

Jeanine Ingber
example

#include <iostream>
using namespace std;
int main()
{
int count=0, base=3;
while(base > 0 && count < 5)
{
count++;
base--;
cout << base << endl;
}
cout << “count is “ << count;
return 0;
}//end main
output?

Jeanine Ingber
Self-test while loops

Jeanine Ingber
do while

 do
statement;
while(expression);
 do
{
statement1;
statement2;
.
} while(expression);
 note - the expression is tested after the statement(s)
are executed, so statements are executed at least once.

Jeanine Ingber
do while example
n=5;
fact =1;
do
{
fact = fact*n;
n--;
} while(n>1);
cout << fact;

output————?

Jeanine Ingber
for statement
 for(initialization; test; increment/decrement)
statement;

 for(initialization; test; increment/decrement)


{
statement;
statement;
.
}

Jeanine Ingber
for statement

initalize
test
increment
decrement

true
statement(s)
statement(s)

Jeanine Ingber
for statement - example

//sum odd integers between 1 and 10


int sum =0;
for(int I=1;I<10;I+=2)
sum = sum + I;
cout << sum;

output—————?

Jeanine Ingber
for statement - example
//n factorial again
int fact =1;
for(int n=5;n>1;n- -)
fact = fact * n;
cout << fact << n;

output—————?

Jeanine Ingber
nesting control stuctures

 when the body of one statement contains


another statement, the statements are
said to be nested.

nested for example


for(int I=0; I<10; I++)
for(int j=0; j<5; j++)
x++;//x incremented 50 times
Jeanine Ingber
break statement

 break;
 terminates loop
 execution continues with the first statement
following the loop
cin >> x;
for(int I=0; I<100; I++)
{
x- -;
if(x<0) break;
}
cout << x << “ “ << I;

Jeanine Ingber
continue statement

 continue;
 forces next iteration of the loop, skipping any
remaining statements in the loop
for(int I=1; I<=100; I++)
{
if(I%2) continue;
cout << I;
}

output?

Jeanine Ingber

You might also like