0% found this document useful (0 votes)
7 views7 pages

C++ Operators and Control Statements Guide

The document provides an overview of assignment operators, control statements, and iteration statements in programming. It includes examples of conditional statements like if, if-else, and switch-case, as well as looping constructs such as for, while, and do-while. The document illustrates how to read user input and perform operations based on conditions.

Uploaded by

amgadatef888
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)
7 views7 pages

C++ Operators and Control Statements Guide

The document provides an overview of assignment operators, control statements, and iteration statements in programming. It includes examples of conditional statements like if, if-else, and switch-case, as well as looping constructs such as for, while, and do-while. The document illustrates how to read user input and perform operations based on conditions.

Uploaded by

amgadatef888
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

Operator (cont)

Assignment operator:
=, +=, -=, *=, /=, %=
shorthand x=3+4*5
Ex: 23 temporary area
x += 3; x = x + 3;
y %= 7; y = y % 7;

x = 5;

Multiple Assignment

x = y = z = 11 ;

11

11

11
Control statements:
Conditional (Branching - Decision) statement int main() int main() int main()
if statement: { { {
int num; int num; int num;
if(condition) int rem;
{ cout << "Enter Number: "; cout << "Enter Number: "; cout << "Enter Number: ";
statement to be executed cin >> num; cin >> num; cin >> num;
when condition is N.Z. rem = num % 2;
} if( rem != 0) if( (num % 2) != 0) if( num % 2)
{ { {
num = num * 2; num = num * 2; num = num * 2;
} } }
condition N.Z cout << num; cout << num; cout << num;
return 0; return 0; return 0;
} } }
Zero
num num % 2 (num % 2) != 0
7 1 non zero
Example: 8 0 zero
Write a program that read a number from user.
if number is odd, multiply it by 2 then print it,
other print it
if .. else statement int main()
{
if(condition) int Grade;
{ N.Z. cout << "Enter Grade: ";
statement to be executed cin >> Grade;
when condition is N.Z if(Grade >= 60)
} {
else cout << "Pass" << endl;
{ }
statement to be executed else
when condition is Zero {
} cout << "Fail" <<endl;
}
return 0;
} Grade >= 90 A
90 > Grade >= 80 B
80 > Grade >= 60 C
otherwise Fail
Zero condition N.Z
int main() int main() int main() int main()
{ { { {
int Grade; int Grade; int Grade; int Grade;
cout << "Enter Grade: "; cout << "Enter Grade: "; cout << "Enter Grade: "; cout << "Enter Grade: ";
cin >> Grade; cin >> Grade; cin >> Grade; cin >> Grade;
if(Grade >= 90) if(Grade >= 90) if(Grade >= 90) if(Grade >= 90)
{ { { {
cout << "A" << endl; cout << "A" << endl; cout << "A" << endl; cout << "A" << endl;
} } } }
if(Grade >= 80) if(Grade >= 80 && Grade < 90) if(Grade >= 80 && Grade < 90) else
{ { { {
cout << "B" << endl; cout << "B" << endl; cout << "B" << endl; if(Grade >= 80)
} } } {
if(Grade >= 60) if(Grade >= 60 && Grade < 80) if(Grade >= 60 && Grade < 80) cout << "B" << endl;
{ { { }
cout << "C" <<endl; cout << "C" <<endl; cout << "C" <<endl; else
} } } {
else else if(Grade < 60) if(Grade >= 60)
{ { { {
cout << "Fail" <<endl; cout << "Fail" <<endl; cout << "Fail" <<endl; cout << "C" << endl;
} } } }
return 0; return 0; return 0; else
} } } {
cout << "Fail" << endl;
O/P: O/P: O/P: }
}
Grade Grade Grade }
55 Fail 55 Fail 55 Fail return 0;
65 C 65 C 65 C }
85 BC ??? 85 BFail?? 85 B
95 ABC ?? 95 AFail?? 95 A
int main() char ch;
{ int x;
int Grade;
cout << "Enter Grade: ";
cin >> Grade;
if(Grade >= 90)
cout << "A" << endl;
else if(Grade >= 80)
{
cout << "B" << endl;
}
else
{
if(Grade >= 60)
{
cout << "C" << endl;
}
else
{
cout << "Fail" << endl;
}
}
return 0;
}
int main() switch .. case int main()
{ {
int num; switch(var) int num;
cout << "Enter Number: "; { cout << "Enter Number: ";
cin >> num case const1: cin >> num
if(num == 1) ..... switch(num)
{ case const2: {
cout << "One" <<endl; ..... case 1:
} cout << "one" << endl;
else default: break;
{ ...... case 2:
if(num == 2) } cout << "Two" << endl;
{ break;
cout << "Two" <<endl; case 3:
} cout << "Three" << endl;
else break;
{ default:
if(num == 3) cout << "Out of Range" <<endl;
{ break;
cout << "Three" << endl; }
} return 0;
else }
{
cout <<"Out of Range"<< endl;
}
}
}
return 0;
}
* Check for the same variable
* Check for equality Fall Through
* Check against constant value
* variable to check is from integer family or char
Iteration (looping) statement

while loop do .. while


for loop

while(condition) do
{ {
for(initial statement ; conditional statement ; update statement)
statement to be repeated when statement to be repeated when
{ non zero
conditional statement is N.Z. conditional statement is N.Z.
statement to be repeated when
} }while(condition);
conditional statement is N.Z.
}

int main() int main()


{ {
int Sum; int Sum;
int Count; int Count;
Sum = 0; Sum = 0;
Count = 1; Count = 1;
for( ; Count <= 100 ; ) while(Count <= 100)
{ {
Sum = Sum + Count; Sum = Sum + Count;
Count++; Count++;
} }
cout << "Total = " << Sum <<endl; cout << "Total = " << Sum <<endl;
return 0; return 0;
} }

You might also like