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

Conditional Statement - If & For Loop

Conditional statements like if, else if, else and switch case execute blocks of code conditionally based on boolean expressions, and loops like for loop and nested loops allow code to be repeated by iterating over a variable within one or more loops. Common conditional statements and loops in C++ are demonstrated through examples of taking user input to check even/odd numbers, largest of 3 numbers, grades, and summing a series using for loop.

Uploaded by

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

Conditional Statement - If & For Loop

Conditional statements like if, else if, else and switch case execute blocks of code conditionally based on boolean expressions, and loops like for loop and nested loops allow code to be repeated by iterating over a variable within one or more loops. Common conditional statements and loops in C++ are demonstrated through examples of taking user input to check even/odd numbers, largest of 3 numbers, grades, and summing a series using for loop.

Uploaded by

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

Conditional Statements

If & for loop


Conditional statements execute lines of
code only if the condition results to true.
Conditional statements return "true" or
"false,"
If Statement
The if keyword is used to execute a statement or block only if a
condition is fulfilled. The if conditional has the form:
if (Condition/Boolean expression) //no semicolon
{
A block is a group of statements which are
Statement1 ; separated by semicolons (;) like all C++
Statement2 ; Block statements, but grouped together in a block
enclosed in braces: { }:

}

Where condition is the expression that is being evaluated. If this


condition is true,statement is executed.If it is false, statement is
ignored (not executed)
#include <iostream.h>
int main()
{ int x ,y;
cout << "enter value for x & y\n";
cin>> x>>y;
if (x > y)
cout << “x is greater than y\n”;

}
if else
We can additionally specify what we want to happen if the
condition is not fulfilled by using the keyword else. Its form used
in conjunction with if is: #include <iostream.h>
if (condition) int main()
{
statementA1;
{ int x ;
statementA2; cout << "enter value for x \n";
… cin>> x ;
} if (x > -1)
else cout << "x is positive\n";
{
statementB1;
else
statementB2; cout<< "x is negative";
… return 0;
} }
Because the condition is either satisfied or not, one of the blocks
in an if-else must execute.
#include <iostream.h> #include <iostream.h>
int main() int main()
{ int x,y ; { int x,y ;
cout << "enter value for x & y\n"; cout << "enter value for x & y\n";
cin>> x >>y; cin>> x >>y;
if (x > y) if (x > y)
{ cout<<"result of x-y is\t"; { cout<<"result of x-y is\t";
cout << (x-y) <<endl; cout << (x-y) <<endl;
} }
if(x<y) else
{ cout<<"result of y-x is\t"; { cout<<"result of y-x is\t";
cout<<(y-x) ; cout<<(y-x) ;
} }
return 0; return 0;
} }
else if
The else if is used to decide between two or more blocks based on
multiple conditions:
#include <iostream.h> #include <iostream.h>
int main()
int main()
{ {
int x,y; int x,y;
cin>>x>>y; cin>>x>>y;
if (x > y) if (x > y)
cout << " x is greater than y\n"; cout << " x is greater than y\n";
else if (y > x) if (y > x)
cout << "y is greater than x\n“; cout << "y is greater than x\n";
else
cout << "x and y are equal\n"; else
return 0; cout << "x and y are equal\n";
return 0;
} }
#include <iostream.h>

int main()
{
int x,y;
cin>>x>>y;
if (x > y)
cout << " x is greater than y\n";
else if (y > x)
cout << "y is greater than x\n";
else if (y == x)
cout << “x and y are equal\n";
return 0;
}
Nested if else
#include <iostream.h>
if (expression) int main()
{
{ if(expression) int x,y,z;
statement; cin>>x>>y>>z;
else If (x >y)
{ if (x>z)
statement; {cout << " x is the largest\t"<<x;}
} else
else {cout << "z is the largest\t"<<z; }
}
{ if(expression)
else
statement; { if (y>z)
else { cout << "y is the largest\t"<<y; }
else
statement
{ cout << "z is the largest\t"<<z; }
} }
return 0;
}
Class work

Write a c++ program that will


ask a number from user and
respond back by printing
whether it’s a odd number or
even number.
Here expression is a variable rather than a
switch-case statement conditional expression

switch (expression)
{
case constant1:
statementA1
statementA2 curly braces are not necessary for cases
... where there is more than one statement
break;
case constant2:
statementB1 The switch evaluates expression and, if expression is equal
statementB2
to constant1, then the statements beneath case constant 1:
are executed until a break is encountered. If expression is not
...
equal to constant1, then it is compared to constant2. If these
break;
are equal, then the statements beneath case constant 2: are
...
executed until a break is encountered. If not, then the same
default: process repeats for each of the constants, in turn. If none of
statementZ1 the constants match, then the statements beneath default: are
statementZ2 executed.
...
}
#include <iostream.h>

int main()
{
int x,y;
cin>>x;
y=x%2;
switch(y)
{
case 0:
cout<<"x is even number";
break;
default :
cout <<"x is odd";
}
return 0;
}
Several case can use same statement
#include <iostream.h>
int main()
{ int score,grade;
cin>>score;
grade=score/10;
switch(grade)
{
case 10:
case 9:
case 8:
cout<<"you got A+";
break;
default :
cout <<"you got less than 80% mark”;
}
return 0;
}
Char type data can be used as case constant
#include <iostream.h>

int main()
{
char x;
cin>>x;
switch(x)
{ case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
cout<<"your choice is a vowel";
break;
default :
cout <<"your choice is a consonent“;
}
return 0;
}
LOOP
Loops allow you to perform statements/block repeatedly

for loop
Its syntax is:
for(initialization; condition; increase/decrease) // no semicolon
{
statement1;
statement2;

}
Initializations: declare and/or initialize control variable. it is evaluated first,
before any iteration occurs.

Condition: this expression is used to determine whether the loop should


continue iterating. It is evaluated immediately after the initialization; if it is true
,the statement is executed .

increase/decrease : to update the control variable. It is evaluated after the


statement is executed.

#include <iostream.h> #include <iostream.h>

int main() int main()


{ {
for(int x = 0; x < 10; x ++) for(int x = 10; x > 0; x --)
cout << x << “\n”; cout << x << “\n”;
return 0; return 0;
} }
The initialization and increase fields are optional. They can remain
empty, but in all cases the semicolon signs between them must be
written.
#include <iostream.h>

int main()
{
int x = 0;
for(; x < 10;)
{cout << x << “\n”;
x ++;
}
return 0;
}
Any type variable can be used as control variable in for
loop

#include <iostream.h>

int main()
{
for(char x = ‘A’; x < =‘Z’; x ++)
cout << x << endl;
return 0;
}
#include <iostream.h>

int main()
{
cout<<"first loop\n";
for(int x = 1; x < 10; x ++)
{cout << x << "\n";}

cout<<"second loop\t";
for(int y = 5; y >0; y --)
{cout << y << "\t"; }

cout<<"\n\nthird loop\a\n";
for(int z = 15; z < 10; z ++)
{cout << z << "\n"; }

return 0;
}
For loop to sum a series
s=1+2+3+4+5+………………………….n
#include<iostream.h>
void main()
{
int i ,n,sum=0;
cout <<"what is your n value ";
cin>>n ;
for(i=1;i<=n;i++)
{sum =sum+i;}
cout<<"summation is "<<sum;
}
Nested for loop
When you place another loop within a loop, the loops are
nested loops.
#include<iostream.h>
void main ()
{
int i, j;
for(i=1;i<=3;i++) //outer loop start
{ cout<<"\ni is now\t"<<i;
for(j=1;j<=4;++j) //inner loop start
{
cout<<" j= "<<j;
i is now 1 j=1 j=2 j=3 j=4
i is now 2 j=1 j=2 j=3 j=4
} // inner loop end i is now 3 j=1 j=2 j=3 j=4
} // outer loop end
}
#include<iostream.h> #include<iostream.h>
void main () void main ()
{ {
int i, j; int i, j;
for(i=5;i>=1;i--)
for(i=1;i<=5;i++)
{
{ for(j=1;j<=i;j++)
for(j=1;j<=i;j++) {cout<< j<< "\t";
{cout<< j<< "\t"; }
} cout<<"\n";
cout<<"\n"; }
}
} }
HW
//pyramid of star
#include<iostream.h>
void main()
{
int b,c,n,m;
cout<<"Enter your line no\t";
cin>>n;
for(c=0;c<=n;c++) // for row
{ for(b=1;b<=n-c;b++)
*
{cout<<" "; //printing spaces
* * * }
* * * * * for(m=1;m<=2*c-1;m++)
{cout<<"*";} //printing stars
* * * * * * * cout<<"\n ";
* * * * * * * * * }
}
while loop
The while loop has a form similar to the if conditional:
while(condition) // no semicolon
{ #include <iostream.h>

Statement1; int main ()


{
Statement2; int n=10;
while (n>0)
… { cout << n << ", ";
--n;
} }
cout << "Finish!\n";
return 0;
}
#include <iostream.h>
int main ()
{int x,y;
x=1;
y=0;
while(x!=0)
{y=y+x;
cin>>x;
}
cout<<y;
return 0;
}
#include <iostream.h>
int main ()
{
char x;
x=‘A’;
while((x==‘N’)||(x==‘n’))
{cout<< “to exit press n /N”
cin>>x;
}
cout<<“you are out of the program”;
return 0
}
do while loop
Its functionality is exactly the same as the while loop, except that condition in
the do-while loop is evaluated after the execution of statement instead of
before, granting at least one execution of statement even if condition is never
fulfilled.

Its syntax is:


do
{
Statement1;
Statement2;

}
while(condition); //semicolon must
#include <iostream.h>

int main ()
{
int n;
do {
cout << "Enter number (0 to end): ";
cin >> n;
cout << "You entered: " << n << "\n";
}
while (n != 0);

cout<< “terminated”;
return 0;
}

You might also like