Programming In C Spring Semester 2013 Lecture 3

  • Slides: 32
Download presentation
Programming In C++ Spring Semester 2013 Lecture 3 Programming In C++, Lecture 3 By

Programming In C++ Spring Semester 2013 Lecture 3 Programming In C++, Lecture 3 By Umer Rana

 • What is counter? • What is repeat? • What is decisions? Programming

• What is counter? • What is repeat? • What is decisions? Programming In C++, Lecture 3 By Umer Rana

Assignment Operators Assignment operators abbreviate assignment expressions • += • -= • *= •

Assignment Operators Assignment operators abbreviate assignment expressions • += • -= • *= • /= • %= Examples : If a=10 a a a += -= *= /= %= Programming In C++, Lecture 3 By Umer Rana 1 1 1 or or or (a (a (a = = = a a a + * / % 1) 1) 1) output output is is is 11 9 10 10 0

Increment / Decrement Operators Increment operator (++) Can be used instead of a+=1 or

Increment / Decrement Operators Increment operator (++) Can be used instead of a+=1 or a=a+1 Decrement operator (--) Can be used instead of a-=1 or a=a-1 Example: If a equals 10, then printf( "%d", ++c ); Output 11 printf( "%d", c++ ); Output 10 In either case, c now has the value of 10 Programming In C++, Lecture 3 By Umer Rana

Decision Making Equality operators == != Relational operators < > <= >= Programming In

Decision Making Equality operators == != Relational operators < > <= >= Programming In C++, Lecture 3 By Umer Rana

Data Flow Diagram (DFD) Expression Show Flow Decision Process Programming In C++, Lecture 3

Data Flow Diagram (DFD) Expression Show Flow Decision Process Programming In C++, Lecture 3 By Umer Rana

Loops To execute a set of instructions repeatedly until a particular condition is being

Loops To execute a set of instructions repeatedly until a particular condition is being satisfied. Three types of looping statements are there 1) For Loop 2) While Loop 3) Do while Loop Programming In C++, Lecture 3 By Umer Rana

Loops Requires 1. The name of a counter variable 2. The initial value of

Loops Requires 1. The name of a counter variable 2. The initial value of the counter variable 3. A condition that tests for the final value of the counter variable (i. e. , whether looping should continue) 4. An increment or decrement by which the counter variable is modified each time through the loop run Programming In C++, Lecture 3 By Umer Rana

Loops For Loop For loop in C is the most general looping construct. In

Loops For Loop For loop in C is the most general looping construct. In for looping statement allows a number of lines represent until the condition is satisfied. Syntax of for loop expression: For Loop expression is divided by semicolons into three separate expressions: the “Initialize expression”, the “test expression”, and the “Increment/Decrement expresssion” for(Initialize counter variable ; Condition ; Increment/Decrement the counter variable) { } Programming In C++, Lecture 3 By Umer Rana Body of the for loop

For Loop DFD Initialization expression Test Expression /Condition True Body of loop Increment /

For Loop DFD Initialization expression Test Expression /Condition True Body of loop Increment / Decrement Expression Programming In C++, Lecture 3 By Umer Rana Exit False

Loops Example: Print number 1 to 10 this way: 1 2 3 4 5

Loops Example: Print number 1 to 10 this way: 1 2 3 4 5 6 7 8 9 10 Programming In C++, Lecture 3 By Umer Rana void main() { printf (“ 1n”); printf (“ 2n”); printf (“ 3n”); printf (“ 4n”); printf (“ 5n”); printf (“ 6n”); printf (“ 7n”); printf (“ 8n”); printf (“ 9n”); printf (“ 10n”); }

Loops Example: #include <stdio. h> void main() { int counter; for(counter = 1; counter

Loops Example: #include <stdio. h> void main() { int counter; for(counter = 1; counter <= 10; counter++ ) printf( "%dn", counter ); } Programming In C++, Lecture 3 By Umer Rana Out. Put 1 2 3 4 5 6 7 8 9 10

Loops Out. Put Counter = 1 Total = 1 Counter = 2 Total =

Loops Out. Put Counter = 1 Total = 1 Counter = 2 Total = 2 Counter = 3 Total = 3 Counter = 4 Total = 4 #include <stdio. h> Counter = 5 Total = 5 void main() Counter = 6 Total = 6 { Counter = 7 Total = 7 int counter, total=0; for(counter = 1; counter <= 10; counter++ )Counter = 8 Total = 8 Counter = 9 Total = 9 { Counter = 10 Total = 10 total=total + 1; printf( “counter=%d, total=%dn", counter, total ); } } Example of Multiple Statement: Programming In C++, Lecture 3 By Umer Rana

Loops While Loop While loop in C is the other most general looping construct.

Loops While Loop While loop in C is the other most general looping construct. The while loop statement executes as long as a specified condition is true. Syntax of while loop expression: while ( condition ) { Code to execute, while the condition is true. } while(condition) { statement(s); } Programming In C++, Lecture 3 By Umer Rana

Loops While Loop DFD Programming In C++, Lecture 3 By Umer Rana

Loops While Loop DFD Programming In C++, Lecture 3 By Umer Rana

Loops Example #include <stdio. h> void main () { // Local variable declaration: int

Loops Example #include <stdio. h> void main () { // Local variable declaration: int a = 10; // while loop execution while( a < 20 ) { printf(“value of a is %d”, a); a++; } } Programming In C++, Lecture 3 By Umer Rana

Loops Example #include <stdio. h> void main () { // Local variable declaration: int

Loops Example #include <stdio. h> void main () { // Local variable declaration: int a = 0; char ch=‘a’; // while loop execution while (ch != ‘r’) { printf(“Enter a character: n”); ch=getche(); printf(“Entered Character is %cn”, ch); } } Programming In C++, Lecture 3 By Umer Rana

Loops do while Loop do… While loop is checks its condition at the bottom

Loops do while Loop do… While loop is checks its condition at the bottom of the loop. n A do. . . while loop is similar to a while loop, except that a do. . . while loop is execute at least one time Syntax of do. . while loop expression: n do { statement(s); } while( condition ); Programming In C++, Lecture 3 By Umer Rana

Loops do while Loop Programming In C++, Lecture 3 By Umer Rana

Loops do while Loop Programming In C++, Lecture 3 By Umer Rana

Loops Example #include <stdio. h> void main () { // Local variable declaration: int

Loops Example #include <stdio. h> void main () { // Local variable declaration: int a = 10; // while loop execution do { printf(“value of a is %d”, a); a++; } while( a < 20 ) ; } Programming In C++, Lecture 3 By Umer Rana

Loops Nested Loop n n One loop inside another loop. In nested loop the

Loops Nested Loop n n One loop inside another loop. In nested loop the inner loop is executed first and then outer. Syntax of for Nested Loop: for ( init; condition; increment ) { statement(s); } Programming In C++, Lecture 3 By Umer Rana

Loops Syntax of while Nested Loop: while(condition) { statement(s); } Programming In C++, Lecture

Loops Syntax of while Nested Loop: while(condition) { statement(s); } Programming In C++, Lecture 3 By Umer Rana

Loops Syntax of do. . while Nested Loop: do { statement(s); } while( condition

Loops Syntax of do. . while Nested Loop: do { statement(s); } while( condition ); }while( condition ); Programming In C++, Lecture 3 By Umer Rana

Loops Loop Control Statements: Loop control statements change execution from its normal sequence. When

Loops Loop Control Statements: Loop control statements change execution from its normal sequence. When execution leaves a scope, all automatic objects that were created in that scope are destroyed break statement Terminates the loop or switch statement and transfers execution to the statement immediately following the loop or switch. continue statement Causes the loop to skip the remainder of its body and immediately retest its condition prior to reiterating. goto statement Transfers control to the labelled statement. Programming In C++, Lecture 3 By Umer Rana

Loops Break Statements: – – When the break statement is encountered inside a loop,

Loops Break Statements: – – When the break statement is encountered inside a loop, the loop is immediately terminated and program control resumes at the next statement following the loop. If you are using nested loops ( ie. one loop inside another loop), the break statement will stop the execution of the innermost loop and start executing the next line of code after the block Programming In C++, Lecture 3 By Umer Rana true false

Loops Break Statements: #include <stdio. h> Void main () { /* local variable definition

Loops Break Statements: #include <stdio. h> Void main () { /* local variable definition */ int a = 10; /* while loop execution */ while( a < 20 ) { printf("value of a: %dn", a); a++; if( a > 15) { /* terminate the loop using break statement */ break; } } } Programming In C++, Lecture 3 By Umer Rana

Loops Continue Statements: n continue forces the next repetition of the loop to take

Loops Continue Statements: n continue forces the next repetition of the loop to take place, skipping any code in between. true false Programming In C++, Lecture 3 By Umer Rana

Loops Continue Statements: #include <stdio. h> Void main () { /* local variable definition

Loops Continue Statements: #include <stdio. h> Void main () { /* local variable definition */ int a = 10; /* while loop execution */ while( a < 20 ) { printf("value of a: %dn", a); a++; if( a == 15) { a++; continue; } } } Programming In C++, Lecture 3 By Umer Rana

Loops Goto Statements: A goto statement provides an unconditional jump from the goto to

Loops Goto Statements: A goto statement provides an unconditional jump from the goto to a labeled statement in the same function. Syntex true goto label. . . label: statement; Programming In C++, Lecture 3 By Umer Rana false

Loops Goto Statements: #include <stdio. h> void main () { /* local variable definition

Loops Goto Statements: #include <stdio. h> void main () { /* local variable definition */ int a = 10; /* do loop execution */ LOOP: do { if( a == 15) { /* skip the iteration */ a ++; goto LOOP; } printf("value of a: %dn", a); a++; }while( a < 20 ); } Programming In C++, Lecture 3 By Umer Rana

Quiz • • • How many type of loops we have? What is the

Quiz • • • How many type of loops we have? What is the expression of the for loop? What is the expression of the while loop? What is the expression of the do. . while loop? What is nested loop? What break statement does? What continue statement does? What goto statement does? What you have to do? Programming In C++, Lecture 3 By Umer Rana

Assignment # 1 Q 1. Differentiate between IDE and Command-Line Development System. Explain the

Assignment # 1 Q 1. Differentiate between IDE and Command-Line Development System. Explain the program execution process and environment with the help of diagram? Q 2. Explain the following terms with appropriate examples: • Getche() • Printf() • Scanf() Programming In C++, Lecture 3 By Umer Rana