slide07-Repetitions - ICS 103: Computer Programming in C...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
ICS 103: Computer Programming in C Handout-7 Topic: Control Structures ( Repetition & Loop Statements ) Objective: To know use of increment operator ++, and decrement operator -- To know syntax of while , do-while , for statements (loops). To know working of above loops with examples. Use of increment operator ++, and decrement operator - - : i++ means i=i+1; i -- means i=i-1; (For above statements in first step initial value of i will be assigned and after that value of i will be incremented or decremented). ++i means i=i+1 ; -- i means i=i-1 ; (But in these, in first step initial value of i will be incremented or decremented and then value of i will be assigned ). Examples: #include<stdio.h> main() { int i=2, r1 ; r1=i++; printf("Value of r1 is : %d", r1); } Sample Output: Page 1 of 11
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
#include<stdio.h> void main() { int i=2, r1 ; r1 = ++i; printf("Value of r1 is : %d", r1); } Looping Structures : While statement : Syntax : while (expression) statement A conditional loop is one whose continued execution depends on the value of a logical  expression. In the above syntax as long as the expression is  true the statement (or statements if enclosed within braces) is executed. When the expression evaluates to false the execution of the statement(s) stops and program continues with the other following statements. Example: Let number be an integer variable. while (number > 100) { printf (“Enter a number\n”) ; scanf (“%d”, &number) ; } Page 2 of 11
Background image of page 2
In  the  ab o ve  e xa m ple , as  lo ng  a s  the  e nte re d  value  o f  number is greater than 100 the loop continues to ask to enter a value for number . Once a number less than or equal to 100 is entered it stops asking. If in the beginning itself the value of number is less than or equal to 100, then the loop is not at all executed. Note
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 11

slide07-Repetitions - ICS 103: Computer Programming in C...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online