REVIEW Surie SMKDPM2009. Q1 : Write a while loop that output the numbers 2, 4, 6, 8,..., 20. Answer : i=2; While(i<=20) { printf(“\n%d “,i); i=i+2;

Slides:



Advertisements
Similar presentations
Java Control Statements
Advertisements

CS110 Programming Language I
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
MATLAB Examples. CS 1112 MATLAB Examples Find the number of positive numbers in a vector x = input( 'Enter a vector: ' ); count = 0; for ii = 1:length(x),
Overview Reference parameters Documenting functions A game of craps. Design, code, test and document.
Talking Letters Consonants Lessons 1 - 5
5/17/ Programming Constructs... There are several types of programming constructs in JAVA. - If-else construct or ternary operator - while - do-while.
1 Engineering Problem Solving With C++ An Object Based Approach Chapter 3 Control Structures.
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
Introduction to Computers and Programming Class 15 Introduction to C Professor Avi Rosenfeld.
Switch Statement switch (month) { case 9: case 4: case 6: case 11: days = 30; break; case 2: days = 28; if (year % 4 == 0) days = 29; break; default: days.
1 10/25/06CS150 Introduction to Computer Science 1 Reading from and Writing to Files.
Repetition (Loops) Want to do some repetitive sequence of actions: print vertical line of *s * Corresponding program: printf(“*\n”);
If () else statement, switch statement, while () loop, do…while() loop and for( ; ; ) loop 1.
CHAPTER 5 CONTROL STRUCTURES II (Repetition). In this chapter, you will:  Learn about repetition (looping) control structures  Explore how to construct.
 Decision making statements Decision making statements if statement if...else statement Nested if...else statement (if...elseif....else Statement) 
While Loops Indefinite Iteration. Last lesson we looked at definite loops using the ‘For’ statement. The while loop keeps going while some condition is.
Selection Statements in C++ If Statement in C++ Semantics: These statements have the same meaning as in the algorithmic language. 2- Two way selection:
Practice 1 Seoul National University Graphics & Media Lab.
CSCI 3328 Object Oriented Programming in C# Chapter 5: C# Control Statement – Part II UTPA – Fall
Array Cs212: DataStructures Lab 2. Array Group of contiguous memory locations Each memory location has same name Each memory location has same type a.
More While Loop Examples CS303E: Elements of Computers and Programming.
Introduction to C Programming CE Lecture 4 Further Control Structures in C.
Additional Control Structures. Chapter 9 Topics Switch Statement for Multi-way Branching Do-While Statement for Looping For Statement for Looping Using.
1 Chapter 9 Additional Control Structures Dale/Weems.
1 Additional Control Structures. 2 Chapter 9 Topics  Switch Statement for Multi-way Branching  Do-While Statement for Looping  For Statement for Looping.
Chapter 7 Additional Control Structures. 2 2 void GetYesOrNo (/* out */ char& response) // Inputs a character from the user // Postcondition: response.
1 09/20/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
ㅎㅎ logical operator if if else switch while do while for Third step for Learning C++ Programming Repetition Control Structures.
Program Flow Control - Looping Addis Ababa Institute of Technology Yared Semu April 2012.
Assignment Operators = +=-= *= /=%= Statement Equivalent Statement a = a + 2 ;a += 2 ; a = a - 3 ;a -= 3 ; a = a * 2 ;a *= 2 ; a = a / 4 ; a /= 4 ; a =
Chapter 5: Control Structures II J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
1 ELEC 206 Chapter 3 Control Structures 5-Step Problem Solving Methodology 1. State the problem clearly. 2. Describe the input and output. 3. Work a.
CS161 Topic #16 1 Today in CS161 Lecture #16 Prepare for the Final Reviewing all Topics this term Variables If Statements Loops (do while, while, for)
Iterations Very Useful: Ability to repeat a block of code Example:
read and learn from example loop programs develop modular program
Exercise 1 Introduction to C# CIS Code the C# program to prompt the user to key in 12 integer values from the keyboard. If a value containing.
Lecture 4 Function example. Example1 int max (int a, int b) { int c; if (a > b) c = a; else c = b; return (c); } void main ( ) {int x, y; cin>>x>>y; cout.
1 Programming in C++ Dale/Weems/Headington Chapter 9 Additional Control Structures (Switch, Do..While, For statements)
 CSC111 Quick Revision. Problem Write a java code that read a string, then show a list of options to the user to select from them, where:  L to print.
Flowchart. a diagram of the sequence of movements or actions of people or things involved in a complex system or activity. a graphical representation.
Int fact (int n) { If (n == 0) return 1; else return n * fact (n – 1); } 5 void main () { Int Sum; : Sum = fact (5); : } Factorial Program Using Recursion.
1 BUILDING JAVA PROGRAMS CHAPTER 5 PROGRAM LOGIC AND INDEFINITE LOOPS.
February 7, You have seen how If statement works from last week’s activities. You have learned that If statement is a conditional statement. Today,
Algorithm & Flow Charts Decision Making and Looping
The Repetition control structure using while loop.
CSE 110 Midterm Review Hans and Carmine. If Statements If statements use decision logic In order to properly use if statements relational operators must.
生查子 ~ 歐陽修 去年元夜時,花市燈如晝, 月上柳梢頭,人約黃昏後; 今年元夜時,月與燈依舊, 不見去年人,淚濕春衫袖。
Compound Condition Break , Continue Switch Statements in Java
Unit-1 Introduction to Java
Decisions Chapter 4.
Engineering Problem Solving with C++, Etter/Ingber
Condition Statements.
Chapter 2.2 Control Structures (Iteration)
Intro to Programming Week # 5 Repetition Structure Lecture # 9
TK1114 Computer Programming
TOPIC 4: REPETITION CONTROL STRUCTURE
Example TDs : id and delim
More Loops.
Loops in C.
Program Control Topics While loop For loop Switch statement
Chapter 2.2 Control Structures (Iteration)
Perfect squares class identifySquareButLessClever {
Program Flow.
Fundamental Programming
Clear and Unclear Windows
CSCE 206 Lab Structured Programming in C
More on iterations using
Presentation transcript:

REVIEW Surie SMKDPM2009

Q1 : Write a while loop that output the numbers 2, 4, 6, 8,..., 20. Answer : i=2; While(i<=20) { printf(“\n%d “,i); i=i+2; } Surie SMKDPM2009

Q2 : Write a while loop that calculates the sum Answer : i=2; While(i<=20) { total=total+i; i=i+2; } printf(“\nSum=%d”,total); Surie SMKDPM2009

Q3 : Write a for loop that calculates the sum Answer : for( i = 2 ; i < =20 ; i = i + 2 ) { total = total + ( i * i ) ; } printf(“\nSum=%d “,total); Surie SMKDPM2009

Q4 : Write a for loop that calculates the sum of the numbers entered by the user. The loop will read numbers entered by the user until the user enters a negative number.. Answer : num=1; while (num>0) { printf(“\nPlease Enter a number “); scanf(“%d”,num); total=total+num; } printf(“Sum=%d”,total); Surie SMKDPM2009

Q5 : Write a switch statement that outputs "vowel" if a character is a vowel ('a', 'e', 'i', 'o', 'u'), "consonant" if a character is a consanant (a letter that is not a vowel), "digit" if the character is a digit ('0', '1', '2',..., '9'), and "other" if the character is something else. Answer : scanf(“%c”,inputdata); switch (inputdata) { case ‘ a’, ‘e’, ‘I’, ‘o’, ‘u’: printf(“\nVowel”);break; case ‘b’,’c’,’d’…………:printf(“\nConsonant”);break; case ‘0’,’1’,’2’,’3’,’4’,’5’,’6’,’7’,’8’,’9’:printf(“\nDigit”);break; default : printf”\nOthers”); break; } Surie SMKDPM2009

Q6: Write a loop that calculates Answer : for( i = 2 ; i < =10 ; i = i + 2 ) { total = total + (i*i); } printf(“\nSum=%d “,total); Surie SMKDPM2009

Q7: Write a function that calculates n 2 for a given value n. Answer : Void Calculation() { printf(“\nPlease enter a number”); scanf(“%d”,n); for( i = 1 ; i < =n ; i++) { total = total + (i*i); } printf(“\nSum=%d “,total); } Surie SMKDPM2009

Q8: Write a function that reads numbers entered by the user (until the user enters a negative number) and returns the largest number that was entered by the user. Answer : Void Calculation() { printf(“\nPlease enter a number”); scanf(“%d”,num); large=0; While (num>0) { If (num>large)large=num; printf(“\nPlease enter a number”); scanf(“%d”,num); } printf(“\nLargest Number = %d”,large); } Surie SMKDPM2009