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

Decision Control and Loop Control in C++

Uploaded by

satishkhot810
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)
23 views

Decision Control and Loop Control in C++

Uploaded by

satishkhot810
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/ 11

Decision control and loop control

_______________________________________________________________________________________________

1.1 Decision Making statements .


o If
o If – else
o If -else ladder
o Nested If- else
1.2 Selection Statements
o Switch
1.3 Looping statements
o While
o Do-while
o For
1.4 Jump statements
o Break
o Continue
o Goto
o Return
Decision Making statements
___________________________________________________________________________
• Decision making statements are used to decide flow of program on the basis of
condition.
• If the test expression (Condition) is true, statements inside the body of if are executed.
• If the test expression (Condition) is false, statements inside the body of else are
executed.
1) If :
Syntax :
if(Condition)
{
// Execute the Statements if condition is TRUE;
}
Flowchart :

Example :
#include<iostream>
using namespace std;
int main()
{
int num ;
cout<<"Enter a number less than 10= " ;
cin>>num ;
if ( num <= 10 )
{
cout<<"Number is less than 10";
}
}
Output :

2) If-else :
Syntax :
if(Condition)
{
// Execute the Statements if condition is TRUE;
}
else
{
// Execute the Statements if condition is FALSE;
}
Flowchart :

Example :
#include<iostream>
using namespace std;
int main()
{
int num ;
cout<<"Enter a number less than 10= " ;
cin>>num ;
if ( num <= 10 )
{
cout<<"Number is less than 10";
}
else
{
cout<<"Wrong Number !!! please enter number less than 10 ";
}
}
Output:

3) if-else ladder :
Syntax :
if(Condition 1)
{
// Execute the Statement 1 if condition 1 is TRUE;
}
else if(Condition 2)
{
// Execute the Statement 2 if condition 2 is TRUE;
}
else if(Condition 3)
{
// Execute the Statement 3 if condition 3 is TRUE;
}
else
{
// Execute the Statement if every condition is FALSE;
}
Flowhart :

Example :
#include <iostream>
using namespace std;
int main()
{

int age;
cout<<"Enter your age\n";
cin>>age;
if (age>=18)
{
cout<<"You can vote!";
}
else if(age>=10)
{
cout<<"You are between 10 to 18 and you can vote for classroom election";
}
else if(age>=3)
{
cout<<"You are between 3 to 10 and you can vote for babies election";
}
else{
cout<<"You cannot vote because your not born!";
}
}
Output :

4) nested if-else:
Syntax:
if(Condition 1)
{
// Execute the Statements if condition 1 is TRUE;
if(Condition 2)
{
// Execute the Statements if condition 2 is TRUE;
if(Condition 3)
{
// Execute the Statements if condition 3 is TRUE;
}
else
{
// Execute the Statement if condition 3 is FALSE;
}
}
else
{
// Execute the Statement if condition 2 is FALSE;
}
}
else
{
// Execute the Statement if condition 1 is FALSE;
}
Flowchart :

Example :
#include<iostream>
using namespace std;
int main()
{
int num;
cout<<"enter the number greater than 0 and less than 100 = \t";
cin>>num;
if(num>0)
{

if(num<100)
{
cout<<"\tnumber is in between 1 to 100 \n";
}
else
{
cout<<"\tnumber must be greater than 100 \n";
}
}
else
{
cout<<"\t\t\tnumber is zero or smaller than zero \n";
}
return 0;
}
Output :

__________________________________________________________
Selection Statements
__________________________________________________________
• Every time we execute the switch the value inside choice variable is matched with case
name/ case value.
• Switch executes that block of code, which matches the case value/ case_name.
• If the value does not match with any of the cases, then thedefault block is executed.

Switch Case :

Syntax : switch(Choice variable)


{
case casename1: statement_1;
break;
case casename 2: statement_2;
break;
.....
......
......
case casename n: statement_n;
break;
default: default statement;
}

NOTE: Even if you don’t write break it will not give you an error . But that makes your
switch partially perfect .

Example :
#include<iostream>
using namespace std;
int main()
{
int order;
cout<<"\t\t\t\t\t\t\t\tWELCOME TO ROYAL HOTEL\n\n";
cout<<"\t\t\t\t\t\t************************ MENU
************************\t\t\t\t\t\t\n";
cout<<"\t\t\t\t\t\t\t\t\t 1. TEA\n";
cout<<"\t\t\t\t\t\t\t\t\t 2. COFFEE\n";
cout<<"\t\t\t\t\t\t\t\t\t 3. WATER\n";
cout<<"\n\n\t\t\t\t enter your order = ";
cin>>order;
switch(order)
{
case 1: cout<<"\t\t\t\t\t\t\t serve Tea\n";
break;
case 2: cout<<"\t\t\t\t\t\t\t serve Coffee\n";
break;

case 3: cout<<"\t\t\t\t\t\t\t serve Water\n";


break;
default: cout<<"Invalid order number";
}
return 0;
}
Output :
Looping statement
_________________________________________________________________
• In a programming sometimes we need to perform some particular task repetitively.
Thus to perform some part of code again and again we use loops in C++
• There are two types of loops in C:
o Entry controlled loops : Test condition is checked before entering into
loop body. Example : while and for.
o Exit controlled loops : Test condition is checked after evaluating loop
body. Example : do-while
1) While loop:
Syntax :
while(Condition)
{
// Statement to execute if condition is TRUE;
}
Example :
// program to print 1 to 50 number
#include<iostream>
using namespace std;
int main()
{
int i = 1;
while (i<51)
{
cout<<I;
i++;
}
}
Output:
2) Do-while loop:
Syntax :
do
{
// Statement to execute if condition is TRUE;
}
while(Condition);
Example :
// program to print 1 to 50 number
#include<iostream>
Using namespace std;
int main()
{
int i = 1;
do
{
cout<<i;
i++;
}while (i<51);
}
Output:
3) For loop :
Syntax :
for(initialization ; Condition ; increment/ decrement)
{
// Statement to execute if condition is TRUE;
}
Example :
// program to print 1 to 50 number
#include<iostream>
Using namespace std;
int main()
{
for(int i=1;i<51;i++)
{
cout<<i;
}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy