Waqas Qadeer waqas.qadeer@gmail.
com
Topics
y if else statements y switch y Loops: y for y while y do while y goto y break y continue y Operators
2
Repetition Statements
y For y while y do while y goto
Example of For Loop
int num; for (num = 1; num <= 3; num++) { println( %d Hello ,num); }
num
Example of Repetition
int num; for (num = 1; num <= 3; num++) cout << num << Potato << endl; // this is similar as printf( %d\n ,num);
OUTPUT
num
Example of Repetition
int num; for (num = 1; num <= 3; num++) cout << num << Potato << endl;
OUTPUT
num
Example of Repetition
true
int num;
for(num = 1; num <= 3; num++) cout << num << Potato << endl;
OUTPUT
num
Example of Repetition
int num; for (num = 1; num <= 3; num++) cout << num << Potato << endl;
OUTPUT
1Potato
num
Example of Repetition
int num; for (num = 1; num <= 3; num++) cout << num << Potato << endl;
OUTPUT
1Potato
num
Example of Repetition
true
int num;
for(num = 1; num <= 3; num++) cout << num << Potato << endl;
OUTPUT
1Potato
10
num
Example of Repetition
int num; for (num = 1; num <= 3; num++) cout << num << Potato << endl;
OUTPUT
1Potato 2Potato
11
num
Example of Repetition
int num; for (num = 1; num <= 3; num++) cout << num << Potato << endl;
OUTPUT
1Potato 2Potato
12
num
Example of Repetition
true
int num;
for(num = 1; num <= 3; num++) cout << num << Potato << endl;
OUTPUT
1Potato 2Potato
13
num
Example of Repetition
int num; for(num = 1; num <= 3; num++) cout << num << Potato << endl;
OUTPUT
1Potato 2Potato 3Potato
14
num
Example of Repetition
int num; for (num = 1; num <= 3; num++) cout << num << Potato << endl;
OUTPUT
1Potato 2Potato 3Potato
15
num
Example of Repetition
false
int num;
for(num = 1; num <= 3; num++) cout << num << Potato << endl;
OUTPUT
1Potato 2Potato 3Potato
16
num
Example of Repetition
false
int num;
for(num = 1; num <= 3; num++) cout << num << Potato << endl;
When the loop control condition is evaluated and has value false, the loop is said to be satisfied and control passes to the statement following the For statement
17
Output
The output was 1Potato 2Potato 3Potato
18
What is output?
int count; for (count = 0; count < 10; count++) { cout << *; }
19
Answer
**********
The 10 asterisks are all on one line. Why?
20
Displaying first 20 natural numbers
int counter; cout<< first 20 natural numbers ; for( counter=1; counter<=20;counter=counter+1) { cout<<counter<<endl; }
21
Count-controlled loop
int count; count = 1; while ( count <= 4 ) { count
cout<< count is << count ; count++; } cout<< Done ;
OUTPUT
22
Table for 2
2x1=2 2x2=4 2x3=6 : : 2 x 10 = 20
Example - Calculate Table for 2
#include <iostream.h> main ( ) { int counter ; for ( counter = 1 ; counter <= 10 ; counter = counter + 1 ) { cout << "2 x " << counter << " = " << 2* counter << "\n ; } }
Finding a Leap Year!! How will do it for 100 Years?
y y y y y y y y y y
int main() { int year; scanf("Enter any year %d ,&year) = "; if(year%4==0) printf("YES! It is a Leap Year\n ); else printf( No! it is not a Leap Year\n ); return 0; }
25
Repetition Structures
while repetition structure
Continues indefinitely as long as the condition is true
product = 2; while (product <= 1000) product *= 2;
product *= 2
true product <= 1000
false
26
int count; count = 1; while ( count <= 4 ) {
count
cout<< count is << count ; count++; } cout<< Done ;
OUTPUT
27
int count; count = 1; while ( count <= 4 ) { TRUE
count
cout<< count is <<count ; count++; } cout<< Done ;
OUTPUT
28
int count; count = 1; while ( count <= 4 ) {
count
cout<< count is <<count ; count++; } cout<< Done ;
OUTPUT count is 1
29
int count; count = 1; while ( count <= 4 ) {
count
cout<< count is << count ; count++; } cout<< Done ;
OUTPUT count is 1
30
int count; count = 1; while ( count <= 4 ) { TRUE
count
cout<< count is << count ; count++; } cout<< Done ;
OUTPUT count is 1
31
int count; count = 1; while ( count <= 4 ) {
count
cout<< count is << count ; count++; } cout<< Done ; OUTPUT count is 1 count is 2
32
int count; count = 1; while ( count <= 4 ) {
count
cout<< count is << count ; count++; } cout<< Done ;
OUTPUT count is 1 count is 2
33
int count; count = 1; while ( count <= 4 ) { TRUE
count
cout<< count is <<count ; count++; } cout<< Done ;
OUTPUT count is 1 count is 2
34
int count; count = 1; while ( count <= 4 ) {
count
cout<< count is + count ; count++; } cout<< Done ;
OUTPUT count is 1 count is 2 count is 3
35
int count; count = 1; while ( count <= 4 ) {
count
cout<< count is <<count ; count++; } cout<< Done ;
OUTPUT count is 1 count is 2 count is 3
36
int count; count = 1; while ( count <= 4 ) { TRUE
count
cout<< count is << count ; count++; } cout<< Done ; OUTPUT count is 1 count is 2 count is 3
37
int count; count = 1; while ( count <= 4 ) {
count
cout<< count is <<count ; count++; } cout<< Done ;
OUTPUT count is 1 count is 2 count is 3 count is 4
38
int count; count = 1; while ( count <= 4 ) {
count
cout<< count is << count ; count++; } cout<< Done ;
OUTPUT count is 1 count is 2 count is 3 count is 4
39
int count; count = 1; while ( count <= 4 ) { FALSE
count
cout<< count is << count ; scount++; } cout<< Done ;
OUTPUT count is 1 count is 2 count is 3 count is 4
40
int count; count = 1; while ( count <= 4 ) {
count
cout<< count is << count ; scount++; } cout<< Done ;
OUTPUT count is 1 count is 2 count is 3 count is 4 Done
41
While Loop
while ( c != y ) { } test expression
false test expression true body of loop exit
42
Repetition Structures
do/while repetition structure
Loop body is executed at least once do { statement while (condition);
43
do/while Repetition Structure
Print counter
do { cout<< counter; }while (++counter <= 10);
true
++counter <= 10
false
44
Example-Guessing game (do-while example)
char c ; int tryNum = 1 ; do { cout << "Please enter your guess by pressing a character key from a to z ; cin >> c ; if ( c == 'z ) { cout << "Congratulations! you guessed the right answer ; tryNum = 6 ; } else tryNum = tryNum + 1 ; } while ( tryNum <= 5 ) ;
break Statement Example
// using break statement with for loop void main() { int x; for (x = 1; x<= 10; x++) { if (x = = 5) break; cout<<x; } cout<<\nBroke out of loop at x<< x; }
46
what is Continue ?
y What is Continue Statement ? When and How do
we use it????
47
continue Statement
y The continue statement causes the program to skip the
rest of the loop in the current iteration as if the end of the statement block had been reached, causing it to jump to the start of the following iteration.
goto statement
y goto allows to make an absolute jump to another
point in the program. You should use this feature with caution since its execution causes an unconditional jump ignoring any type of nesting limitations. y The destination point is identified by a label, which is then used as an argument for the goto statement. y A label is made of a valid identifier followed by a colon (:).
Operators
y ++ y -y += y -= y *= y %= y &&
+ -
* % ||
< > <= >= == ! !=
52
Escape Characters
Escape Sequence \n \t \\ \ \ Working New Line Tab Backslash Double quotation mark Single Code