Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
17 views

Program Flow Control Final

The document discusses program flow control in C++ using conditional statements and looping. It explains how if-else statements allow making decisions based on conditions, while loops like while, do-while, and for loops allow repetitive execution of code. Nested loops are also covered, where one loop is placed within another. Examples are provided for each type of loop and conditional statement.

Uploaded by

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

Program Flow Control Final

The document discusses program flow control in C++ using conditional statements and looping. It explains how if-else statements allow making decisions based on conditions, while loops like while, do-while, and for loops allow repetitive execution of code. Nested loops are also covered, where one loop is placed within another. Examples are provided for each type of loop and conditional statement.

Uploaded by

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

Program Flow Control

Making Decisions
Looping
Making Decisions
• C++ conditional statements allow you to make a decision,
based upon the result of a condition. These statements are
called as Decision Making Statements or Conditional
Statements.
• So far, we have seen that all set of statements in a C++
program gets executed sequentially in the order in which they
are written and appear.
• It is to be noted that C++ assumes any non-zero or non-null
value as true and if zero or null, treated as false.
• This type of structure requires that the programmers indicate
several conditions for evaluation within a program. The
statement(s) will get executed only if the condition becomes
true and optionally, alternative statement or set of
statements will get executed if the condition becomes false.
An ‘if’ statement consists of a Boolean
expression followed by one or more
statements.
An ‘if’ statement can be followed by
an optional ‘else’ statement, which
executes when the Boolean
expression is false.

A ‘switch’ statement allows a


variable to be tested for equality
against a list of values.
• If statements in C++ is used to control the program
flow based on some condition, it's used to execute
some statement code block if the expression is
evaluated to true, otherwise, it will get skipped.
• The if statement in C++ can be used in various
forms depending on the situation and complexity.
'Statement n' can be a statement or a set of
statements and if the test expression is evaluated
to true, the statement block will get executed or it
will get skipped.
#include <iostream> #include <iostream>
using namespace std; using namespace std;

int main () int main ()

{ {

int a = 15; int a = 50;


int b = 3;
if (a > 20){
if (b > a){ cout << " is greater" << endl;
cout << "b is greater" << endl; }
} cout <<"the value of a is: "<< a << endl;
return 0;
return 0; }
}
#include <iostream>
using namespace std; Exercise
Code which displays absolute
int main () value by taking negative number

int number;
cout <<" input negative number : ";
cin >> number;

if (number < 0){


number = -number;
cout << " the absolute value is" << number <<endl;

return 0;
}

}
#include <iostream>
using namespace std;

int main ()

int a = 50;

if (a > 20){
cout << " is greater" << endl;
}
cout <<"the value of a is: "<< a << endl;
return 0;
}
#include <iostream> Exercise code which compare
using namespace std; Two numbers
int main ()

int a = 15;
int b = 20;

if (b > a){
cout << " b is greater " << endl;

}else{

cout <<" a is greater " << endl;


}

return 0;
}
Exercise
Log in password
 else if statements in C++ is like another if condition, it's used in a program when if
statement having multiple decisions.
#include <iostream>
using namespace std;
int main ()
{
int age;
cout <<"Enter your age" <<endl;
cin >> age;

if (age <=10){
cout << " your energy level is 100%" <<endl;
}
else if (age >= 20)
{
cout <<" your energy level is 80% " <<endl;
}else if(age <=50)
{
cout << "your energy level is 75%" <<endl;
}
else{
cout <<"your energy level is 50%" <<endl;
}

return 0;
}
#include <iostream>
using namespace std;
int main ()
{

int a = 15;
int b = 20;

if (b > a){
cout << " b is greater " << endl;
}
else if (a > b)

{
cout <<" a is greater " << endl; Exercise :
}else{ code to check two numbers to and check
the other condition.
cout << " \n both are equal" << endl;
}
return 0;
}
The C++ switch statement is used when you have
multiple possibilities for the if statement
After the end of each block it is necessary to
insert a break statement because if the
programmers do not use the break statement, all
consecutive blocks of codes will get executed
from each and every case on wards after matching
the case block.
#include <iostream>
using namespace std;

int main()
{
char grade ='A';

switch (grade){
case 'A':
cout<<"excellent!"<<endl;
break;
case'B':
case'C':
cout<<"well done "<<endl;
break;
case'D':
cout<<"you pass"<<endl;
break;
case'F':
cout <<"fail please try again"<<endl;
break;
default:
cout<<"invalid grade" <<endl;
cout<<"your grade is" <<grade<<endl;
}
return 0;
#include <iostream>
using namespace std;
int main()
{
char choice;
cout << "Enter A, B, or C: ";
cin >> choice;
switch (choice)
{
case 'A':cout << "You entered A.\n";
break;
case 'B':cout << "You entered B.\n";
break;
case 'C':cout << "You entered C.\n";
break;
default: cout << "You did not enter A, B, or C!\n";
}
return 0;
}
Write a c++ code for Calculator
by choosing
Operator and do operation.
Sometimes it is necessary for the program to execute the
statement several times, and C++ loops execute a block of
commands a specified number of times until a condition
is met.
There may arise some situations during programming
where programmers need to execute a block of code
several times (with slight variations sometimes).
In general, statements get executed sequentially with a
C++ program, one statement followed by another. C++
provides statements for several control structures along
with iteration/repetition capability that allows
programmers to execute a statement or group of
statements multiple times.
• C++ while loops statement allows to repeatedly
run the same block of code until a condition is
met.
• while loop is a most basic loop in C++. while
loop has one control condition, and executes as
long the condition is true. The condition of the
loop is tested before the body of the loop is
executed, hence it is called an entry-controlled
loop.
#include <iostream>
using namespace std;
int main ()
{
// Local variable declaration:
int a = 10;
// while loop execution
while( a < 20 )
{
cout << "value of a: " << a << endl;
a++;
}
return 0;
}
C++ do while loops are very similar to the while
loops, but it always executes the code block at
least once and furthermore as long as the
condition remains true.
This is an exit-controlled loop.
#include <iostream>
using namespace std;
int main()
{
int n = 1;
do
{

cout << n << endl;


n++;
}
while( n <= 10 );
return 0;
}
C++ for loops is very similar to a while loops in
that it continues to process a block of code until a
statement becomes false, and everything is
defined in a single line.
for loop is an entry-controlled loop.
#include <iostream>
using namespace std;
int main()
{
int sum = 0, i, n;

for(i = 0; i < 10; i++)


{
cout << "Enter number" << endl;
cin >> n;

sum = sum + n;
}
cout << "Sum is " << sum << endl;

return 0;

}
Nesting of loops One loop inside other loop
#include <iostream>
using namespace std;

int main () {
int i, j;

for(i = 2; i<100; i++)


{
for(j = 2; j <= (i/j); j++)
if(!(i%j)) break; // if factor found, not prime
if(j > (i/j)) cout << i << " is prime\n";
}

return 0;
}
#include <iostream>
using namespace std;
int main()
{
int i;
int j;
for(i = 12; i <= 14; i++)
{
/*outer loop*/
cout << "Table of " << i << endl;
for(j = 1; j <= 10; j++)
{
/*inner loop*/
cout << i << "*" << j << "=" << (i*j) << endl;
}
}
return 0;
}
Quiz (ctr+c)
#include <iostream>
using namespace std;
int main()
{
for( ; ; )
{
cout << "This loop will never end" << endl;
}
return 0;
}
#include <iostream>
using namespace std;
int main()
{
int i,j;
for(i=1;i<=5;i++)
{
for(j=1; j<=i; j++)
{
cout << i;

}
cout << "\n";
}
return 0;
}
#include <iostream>
using namespace std;
int main()
{
int i,j;
for(i=1;i<=5;i++)
{
for(j=1; j<=i; j++)
{
cout << "*";

}
cout << "\n";
}
return 0;
#include <iostream>
using namespace std;
int main()
{
int i,j;
for(i=1;i<=5;i++)
{
for(j=i; j<=5; j++)
{
cout << "*";

}
cout << "\n";
}
return 0;

You might also like