0% found this document useful (0 votes)
10 views21 pages

Chapter 3 Control Statment

The document covers fundamental programming concepts, focusing on control statements such as conditional statements (if, if-else, nested if, and switch) and repetition statements (while, do-while, and for loops). It provides syntax examples and practical exercises to illustrate the use of these statements in C++. Additionally, it discusses the use of break and continue statements within loops.

Uploaded by

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

Chapter 3 Control Statment

The document covers fundamental programming concepts, focusing on control statements such as conditional statements (if, if-else, nested if, and switch) and repetition statements (while, do-while, and for loops). It provides syntax examples and practical exercises to illustrate the use of these statements in C++. Additionally, it discusses the use of break and continue statements within loops.

Uploaded by

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

Fundamentals of

Programming

1
Chapter 3
Control Statement
2 Conditional Statements
The if Statement
 It is sometimes desirable to make the execution of a statement
dependent upon a condition being satisfied.
 The different forms of the ‘If” statement will be used to decide
whether to execute part of the program based on a condition
which will be tested either for TRUE or FALSE result.
 The different forms of the “If” statement are:
The simple if statement
The If--- else statement
The if---else if---else statement
Nested if---else statement
3 Simple if Statements
General form or syntax: #include <iostream.h>

if (expression) using namespace std;


int main ()
statement;
{
First expression is evaluated. If
int a,b;
the outcome is true then
statement is executed. cout<<"Enter the two number ";
Otherwise, nothing happens. cin>>a>>b;

For example Simple if if(a>b)


statement ,check the number is cout<<a<<" is the largest"<<endl;
the largest return 0;
}
4 if-else statement
General form or Syntax: Example:
{
if (expression)
int age;
statement1; cout<<"enter age \n";
else cin>>age;
if(age>20)
statement2;
cout<<" You Are Adult";
First expression is evaluated. If the else
outcome is true then statement1 is cout<<"Cool down you are kid";
executed. Otherwise, statement2 is }
executed.
Exercise
1. Write a program that compare two numbers and display the
minimum ones using if else statement?
5 If..else if..else statements
 The “if else if” statement allows us to specify more than two alternative statements
each will be executed based on testing one or more conditions.
 General Syntax:
if (expression1)
statements1;
else if(expression2)
statements2;
else if(expressionN)
statementsN;
else
statements
6 Example1. To calculate student’s grade based on
score
#include<iostream>
using namespace std; cout<<”Good”;
int main() { }
if(score>=85) else if(score>=50)
{ {
cout<<” Excellent ”;
} cout<<”Satisfactory”;
else if(score>=75) } else {
{ cout<<”F Grade”;
cout<<”Very Good”; }
} return 0;
else if(score>=65){
}
7 Nesting If statements
 One or more if statements can be nested with in another if statement.
 The nesting will be used to test multiple conditions to perform a task.
 It is always recommended to indent nested if statements to enhance readability
of a program.
General Syntax:

if (expression1) else
{ {
if (expression2) if (expression3)
statementsN; statementsR;
else else
statementsM; statementsT;
} }
8 Cont’d…

#include<iostream.h> cout<<“\n Excellet”;


int main() else
{ cout<<“\n very good”;
int testScore, age; }
else
cout<<”\n Enter your test score :”;
{
cin>>testScore; If(sex==’F’)
cout<<“\n enter your age:”; cout<<“\n You can pass”;
cin>>age; else
if(testScore >= 70) cout<<“\n You are warring”;
{ return 0;
if(age < 10) }
9
The switch Statement
 Another C++ statement that implements a selection control flow is the
switch statement (multiple-choice statement).
 The switch statement provides a way of choosing between a set of
alternatives based on the value of an expression.
 The switch statement has four components:
Switch The General Syntax might be:
Case switch(constant_expression)
Default {
case constant1:
Break
statements;
case constant n:
statements;
default:
statements;
}
10 Cont’d….
Example:/* Program that
demonstrate use of switch statement
*/ case 4: cout<<"Wedsenday.\n";
int day; break;
cout<<"Please enter the week day in case 5: cout<<"Thrusday.\n";
integer:"; break;
cin>> day; case 6: cout<<"Friday.\n";
switch (day) break;
case 7: cout<<"Saturday!.\n";
{
break;
case 1: cout<<"Sunday.\n"; default: cout<<"pls enter the
break; correct input\n";
case 2: cout<<"Monday\n"; return 0;
break; }
case 3: cout<<"Tuesday.\n"; }
break;
11
Cont’d….
#include <iostream>
using namespace std;
int main()
{
int i; case 2: cout << "less than 3 \n";
for(i=0; i<5; i++) case 3: cout << "less than 4 \n";
{ case 4: cout << "less than 5 \n";
switch(i) }
{ cout << '\n';
case 0: cout << "less than 1 \n"; }
case 1: cout << "less than 2 \n"; return 0;
}
12
Repetitions Statements
1. while Loops
while (continuation-condition)
false
{ Continuation
// loop-body; condition?
}
Example for while loop: true
int i = 0;
Statement(s)
while (i < 100) {
Cout<<"Welcome !!!!";
i++;
} Next
Statement
13 2. do-while Loop
Syntax and Example
do {
// Loop body; Statement(s)

} while (continue-condition);
Example for do While
{ true
int age; Continue
cout<<"enter age"; condition?
do {
cin >> age;
cout << ++age << '\n'; false
}
while (age<=10); Next
return 0; Statement
}
14 3. For Loops
for(initialization, condition, increment/decrement)
{
statement;
}
 initialization sets a loop control variable to an initial value.
 condition is an expression that is tested each time the loop repeats.
 The increment/decrement is an expression that determines how the loop control
variable is incremented each time the loop repeats.
Initial-Action

false
Action-After- Continuation
Each-Iteration condition?

true

Statement(s)
(loop-body)

Next
Statement
15 Example 1
int i;
for (i = 0; i < 10; i++) {
cout<<"Welcome to Programming! \n”;
} Output
Welcome to Programming!
Welcome to Programming!
Welcome to Programming!
Welcome to Programming!
Welcome to Programming!
Welcome to Programming!
Welcome to Programming!
Welcome to Programming!
Welcome to Programming!
Welcome to Programming!
16 Example 2
#include<iostream>
using namespace std;
int main()
{
for(int i=10;i>0;i--)
{ Output
cout<<i<<",";
10,9,8,7,6,5,4,3,2,1,FIRE!
}
cout<<"FIRE!";
return 0;
}
17 Example 3
#include<iostream>
using namespace std;
int main()
{
int n, sum = 0;
cout << "Enter a positive integer: ";
cin >> n;
for (int i = 1; i <= n; i++)
sum += i*i;
cout<< "The sum of the first " << n << " squares is "<< sum <<
endl;
return 0;
}
18 continue
 The continue statement can be used in loops and has the opposite effect to break, that is,
the next loop is begun immediately.
 In the case of a while or do-while loop the program jumps to the test expression, whereas
a for loop is reinitialized
 Example:
#include<iostream>
using namespace std;
int main()
{
for( int i = 0; i < 50; i++ )
{
if( i == 30)
continue;
cout<<" The Value of i is:"<<i<<endl;
}
return 0;
}
19 Break Statement
 The break statement exits from a switch or loop immediately.
 You can use the break keyword to jump to the first statement that follows
the switch or loop.
Example: #include<iostream>
using namespace std;
int main()
{
for( int i = 0; i < 50; i++ )
{
if( i == 10)
break;
cout<<" The Value of i is:"<<i<<endl;
}
return 0;
}
20 Exercise
1. Write the program that executes the following program using for loop.
*********
*******
*****
***
*
2. Write the program that executes the following output on the screen
The square of 1 is = 1
The square of 2 is = 4
The square of 3 is = 9
The square of 4 is = 16
The square of 5 is = 25
The square of 6 is = 36
21 Thank You ...

?
System Analysis and Design

You might also like