Download presentation
Presentation is loading. Please wait.
1
Programming Fundamental
Instructor Name: Muhammad Safyan Lecture-9
2
Lecture outline For Loop Increment/ Decrement Operator
Compound assignment statement Break statement Continue statement Nested Loop
3
FOR LOOP
4
FOR LOOP In C++, for is a reserved word.
The for loop executes as follows: The initial statement executes. The loop condition is evaluated. If the loop condition evaluates to true i. Execute the for loop statement. ii. Execute the update statement (the third expression in the parentheses). Repeat Step 2 until the loop condition evaluates to false. The initial statement usually initializes a variable (called the for loop control, or for indexed, variable).
6
You can count backward using a for loop if the for loop control expressions are set correctly.
For example, consider the following for loop:
7
You can increment (or decrement) the loop control variable by any fixed number. In the following for loop, the variable is initialized to 1; at the end of the for loop, i is incremented by 2. This for loop outputs the first 10 positive odd integers.
8
2 x 1 = 2 2 x 2 = 4 2 x 3 = 6 : 2 x 10 = 20 Sample Program
Write a program to print the table of 2 2 x 1 = 2 2 x 2 = 4 2 x 3 = 6 : 2 x 10 = 20
10
Increment Operator ++ counter ++ ; same as counter = counter + 1;
11
Decrement operator -- counter -- ; same as counter = counter - 1
12
Increment/Decrement Operator
j++ ++j
13
Increment Operator ++ counter ++ ; same as counter = counter + 1;
14
Decrement operator -- counter -- ; same as counter = counter - 1
15
Increment/Decrement Operator
Increment Operator can be written with following two ways: Pre increment ++j Post increment j++ Decrement Operator can be written with following two ways: Pre decrement - -j Post decrement j- -
16
Increment/Decrement Operator
If using one variable with Increment/decrement Operator , does not matter. It does matter when the variable used in an expression where it evaluated to assign the value to other variable. If used pre increment (++j), the value of j is increased by 1 , new value is used in an expression e.g. If j==5 X=++j;
17
Increment/Decrement Operator
If used post increment(j++) the vlaue of j is used first in an expression than is increased by by 1. If j = 5, and we write the expression x = j++ ;
18
Increment/Decrement Operator
x=5; x++; ++x Cout<<“x=“<<x j=7; Cout<<“j=“<<j++; Cout<<“j=“<<j; Cout<<“j=“<<++j; Increment and decrement operator can be used in repetitive structure e.g. FOR LOOP
19
Compound Assignment Operators
20
Compound Assignment Opreator
+= counter += 3 ; same as counter = counter + 3 ;
21
-= counter -= 5 ; same as counter = counter – 5 ;
22
*= x*=2 x = x * 2
23
%= x %= 2 ; same as x = x % 2 ;
24
Sample Program (Assignment Operator)
find the sum of the squares of the integers from 1 to n. Where n is a positive value entered by the user (i.e. Sum = n2
25
break; continue;
26
Break statement Break statement interrupt the flow of control.
In some case, we want only true case to be executed and remain to be skipped For this purpose we write the break statement right after the true case. Break statement interrupt the flow of control and control jumps out of the repitative structure
27
Break statement #include<iostream.h> #include<conio.h>
main() { int num, j; cin>>num; j=2; while(j<num) if(num%j==0) cout<<"number is not prime"; break; } j++; if(num==j) cout<< "number is prime"; getch();
28
Continue statement There is another statement relating to loops that is continue statement. We may have lot of code in the body of loop Early part of code is executed every time Remaining portion is executed in certain cases and may not executed in other cases. But loop should be continue. Continue forces the immediate next iteration of loop. Statement of the loop body will not executed after continue statement
29
Continue statement-example
int j; J=2; For (int i=0; i<=3; i++) { if If(i==j) Continue; Else Cout<<i; }
30
Nested LOOP A loop within a loop is called Nested loop
for (initial value , terminating value, increment expression) { statement or compound statement }
31
Nested LOOP A loop with a loop is called Nested loop
Inner loop will be executed completely for each iteration of outer loop Inner loop will executed multiple times of outer loop
32
Nested LOOP Exercise Program For(int i=1; i<=3; i++)
For (int j=1; j<=3; j++) cout<< j; Exercise Program
33
Nested LOOP main() { int i,j; i=1; j=1; while(i++<=100)
while(j++<=200) if(j==150) break; else cout<<i<<"----"<<j<<"\n"; } getch();
34
Nested LOOP Write a program that print different combination of digits between (excluding equal combination)
35
Nested LOOP #include<iostream.h> #include<conio.h> main()
{ for(int i=1; i<=3; i++) for (int j=1; j<=3; j++) if (i==j) continue; cout<<i<<"------"<<j<<"\n"; } getch();
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.