Control Structures in C Risanuri Hidayat, Ir., M.Sc.

Slides:



Advertisements
Similar presentations
INTEC CS160 - Jeanine Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 3 Control Structures and Data Files.
Advertisements

Java Control Statements
Selection Feature of C: Decision making statements: It allow us to take decisions as to which code is to be executed next. Since these statements control.
Control Structures.
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Introduction to Control Statements Presented by: Parminder Singh BCA 5 th Sem. [ Batch] PCTE, Ludhiana 5/12/ Control Statements.
5/17/ Programming Constructs... There are several types of programming constructs in JAVA. - If-else construct or ternary operator - while - do-while.
Topic 03 Control Statements Programming II/A CMC2522 / CIM2561 Bavy Li.
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
1 Engineering Problem Solving With C++ An Object Based Approach Chapter 3 Control Structures.
Control Flow C and Data Structures Baojian Hua
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.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Logical and Relational Expressions Nested if statements.
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
UNIT II Decision Making And Branching Decision Making And Looping
Control Structures Session 03 Mata kuliah: M0874 – Programming II Tahun: 2010.
Lecture 10: Reviews. Control Structures All C programs written in term of 3 control structures Sequence structures Programs executed sequentially by default.
Control Structures A control structure is simply a pattern for controlling the flow of a program module. The three fundamental control structures of a.
CPS120: Introduction to Computer Science Decision Making in Programs.
Chapter 3 Control Flow Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University.
DiagrammaticRepresentation Iteration Construct False True Condition Exit from Statement (s) loop Sequence construct Selection construct Statement 1 Statement.
Switch Statement Is a selection control structure for multi-way branching. SYNTAX switch ( IntegralExpression ) { case Constant1 : Statement(s); // optional.
1 Do-While Statement Is a looping control structure in which the loop condition is tested after each iteration of the loop. SYNTAX do { Statement } while.
1 Week 6 Branching. 2 What is “Flow of Control”? l Flow of Control is the execution order of instructions in a program l All programs can be written with.
Review the following: if-else One branch if Conditional operators Logical operators Switch statement Conditional expression operator Nested ifs if –else.
Switch Statements Comparing Exact Values. The Switch Statement: Syntax The switch statement provides another way to decide which statement to execute.
C Programming Lecture 7 : Control Structures. Control Structures Conditional statement : if, switch Determine a block of statements to execute depending.
Control Statements in C 1.Decision making statements 2.Looping statements 3.Branching statements
Control Structures - Selections - Repetitions/iterations (part 2) 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Program Looping Why we need loops in our code –Make code concise for repetitive processes When to use loops –Run a block of code repetitively –Process.
1 Chapter 3: Loops and Logic. 2 Control Statements If statement Example NumberCheck.java Relational operators (, >=, ==, !=) Using code blocks with If.
Statements
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
CPS120: Introduction to Computer Science Decision Making in Programs.
Control Flow Statements
CSI 3125, Preliminaries, page 1 Control Statements.
CPS120: Introduction to Computer Science Decision Making in Programs.
Chapter 2: Fundamental Programming Structures in Java Adapted from MIT AITI Slides Control Structures.
Control structures in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
5 Copyright © 2004, Oracle. All rights reserved. Controlling Program Flow.
Loops and Logic. Making Decisions Conditional operator Switch Statement Variable scope Loops Assertions.
ECE122 Feb 10, Unary Operator An operator that takes only a single operand Plus: + Minus: – Cast: (type). E.g. (double)
C syntax (simplified) BNF. Program ::= [ ] Directives ::= [ ] ::= | |… ::=#include > ::=#define.
Chapter 7 Control Structures. Java has very flexible three looping mechanisms. You can use one of the following three loops:  while Loop  do...while.
Contoh Presentasi PENILAIAN MINAT
Pernyataan Kawalan Java
BAB INHERITANCE (Pewarisan)
Sequence, Selection, Iteration The IF Statement
Unit-1 Introduction to Java
Expressions and Control Flow in JavaScript
Control Structures.
LutfianoAzis.  Pengertian Kerajinan Bahan Lunak  Kerajinan Bahan Lunak adalah suatu produk kerajinan yang mengunakan bahan bersifat lunak sebagai dasar.
KEYSHA SALSABILA ABADI VIII-I PRAKARYA. PENGERTIAN BAHAN LUNAK Bahan lunak, yaitu bahan yang memiliki sifat fisik empuk/lunak sehingga sangat mudah dibentuk.
LutfianoAzis  Pengertian Kerajinan Bahan Lunak  Kerajinan Bahan Lunak adalah suatu produk kerajinan yang mengunakan bahan bersifat lunak sebagai dasar.
KELAS:8-4. Pengertian bahan lunak Kerajinan bahan lunak merupakan produk kerajinan yang menggunakkan bahan dasar yang bersifat lunak yaitu lentur, lembut,
TUGAS PRAKARYA (KERAJINAN BAHAN LUNAK) NAMA : ABDUL RAFI KELAS : VIII - 1 NO. ABSEN : 1.
Nama : Rahmat Hidayat Kelas :VIII. 2 No.Absen : 26 SMP NEGERI 48 JAKARTA TUGAS PRAKARYA.
Prakarya Bab 1 (Kerajinan Bahan Lunak) Nama : Jenny Dias Syafira Kelas : 8-3 Pelajaran : Prakarya Sekolah : SMPN 48 Jakarta.
ppt kerajinan bahan lunak 84
Tugas PPT Prakarya tentang Kerajinan Bahan Lunak
PRAKARYA TUGAS 2 (KERAJINAN BAHAN LUNAK) NAMA:KHALIDA TANTRI KELAS:8-3 MAPEL:PRAKARYA.
2.0 FUNDAMENTALS OF JAVA PROGRAMMING LANGUAGE
CS149D Elements of Computer Science
الكلية الجامعية للعلوم التطبيقية
IF if (condition) { Process… }
for, do-while and switch statments
Conditionals.
Program Flow.
Control Statements Paritosh Srivastava.
Presentation transcript:

Control Structures in C Risanuri Hidayat, Ir., M.Sc.

Condition OperatorMeaningExample ==Equal tocount == 10 !=Not equal toflag != DONE <Less thana < b <=Less than or equal to<= LIMIT >Greater thanpointer > end_of_list >=Greater than or equal tolap >= start

Control Structures in C These include –ifelse, –while, –do-while, –for, and a selection statement called –switch.

if-else The if-else statement can exist in two forms: with or without the else. The two forms are: if(expression) statement or if(expression) statement1 else statement2

if-else If if (condition) statement1; else statement2; int a, b; //... if(a < b) a = 0; else b = 0;

Nested if nested if adalah statement if yang targetnya adalahjuga if atau else. if (i == 10) { if (j < 20) a = b; if (k > 100) c = d; // this if is else a = c; // associated with this else } else a = d; // this else refers to if(i == 10)

if-else-if Ladder Bentuknya: if(condition) statement; else if(condition) statement; else if(condition) statement;. else statement;

if-else-if Ladder // Demonstrate if-else-if statements (IfElse.c). #include main () { int bulan = 4; // April char season[10]; if(bulan == 12 || bulan == 1 || bulan == 2) strcpy(season,"Salak"); else if(bulan == 3 || bulan == 4 || bulan == 5) strcpy(season,"Durian"); else if(bulan == 6 || bulan == 7 || bulan == 8) strcpy(season,"Mangga"); else if(bulan == 9 || bulan == 10 || bulan == 11) strcpy(season,"Jeruk"); else strcpy(season,"Mbuh"); printf("April adalah musim %s\n ",season); }

switch switch merupakan statement percabangan dengan banyak cabang. Bentuknya seperti berikut: switch (expression) { case value1: // statement sequence break; case value2: // statement sequence break;. case valueN: // statement sequence break; default: // default statement sequence }

switch expression harus bertype byte, short, int, or char; // A simple example of the switch(switch.c) #include main() { int i; for(i=0; i<6; i++) switch(i) { case 0: printf("i is zero.\n"); break; case 1: printf("i is one.\n"); break; case 2: printf("i is two.\n"); break; case 3: printf("i is three.\n"); break; default: printf("i is greater than 3.\n"); } // switch } // main

Nested switch Kita dapat juga membuat statement switch di dalam switch yang lain switch(count) { case 1: switch(target) { // nested switch case 0: printf("target is zero"); break; case 1: // no conflicts with outer switch printf("target is one"); break; } // switch(target) break; case 2: //...

Iteration while while loop merupakan dasar looping di C. While akan mengulang statement jika kondisi yang disyaratkan benar. Bentuk statement while adalah: while(condition) { // body of loop }

while // Demonstrate the while loop (while.c). #include main() { int n = 10; while(n > 0) { printf("tick %d \n",n); n--; } // while } // main

do-while Sering kali dalam program kita membuat instruksi terlebih dahulu baru kemudian di-test hasilnya. Hal ini juga sering terjadi dalam looping. C mm-fasilitasi hal ini dengan do-while. Bentuknya sebagai berikut: do { // body of loop } while (condition);

do-while // Demonstrate the do-while loop (dowhile.c). #include main() { int n = 10; do { printf("tick %d \n",n); n--; } while(n > 0); } // main

for For merupakan statement loop yang paling sering digunakan dalam berbagai bahasa, termasuk C. Berikut ini bentuk umumnya: for(initialization; condition; iteration) { // body }

for // Demonstrate the for loop (loop.c). #include main() { int n; for(n=10; n>0; n--) printf("tick %d \n",n); }

for // Using the comma (comma.c) #include main() { int a, b; for(a=1, b=4; a<b; a++, b--) { printf("a = %d \n", a); printf("b = %d \n", b); }

Nested Loops Like all other programming languages, C allows loops to be nested. That is, one loop may be inside another. For example, here is a program that nests for loops: // Loops may be nested (nestedfor.c). #include main() { int i, j; for(i=0; i<10; i++) { for(j=i; j<10; j++) printf("."); printf("\n"); }

Jump C supports four jump statements: break, continue, return goto. These statements transfer control to another part of your program.

break In C, the break statement has two uses. –First, as you have seen, it terminates a statement sequence in a switch statement. –Second, it can be used to exit a loop.

break // Using break to exit a loop (break.c). #include main() { int i; for(i=0; i<100; i++) { if(i == 10) break; // terminate loop if i is 10 printf("i: %d \n", i); } printf("Loop complete."); }

break // Using break to exit a while loop (break2.c). #include main() { int i = 0; while(i < 100) { if(i == 10) break; // terminate loop if i is 10 printf("i: %d \n", i); i++; } printf("Loop complete."); }

continue continue go immediately to next iteration of loop In while and do-while loops, a continue statement causes control to be transferred directly to the conditional expression that controls the loop. In a for loop, control goes first to the iteration portion of the for statement and then to the conditional expression.

continue // Demonstrate continue (continue.c). #include main() { int i; for(i=0; i<10; i++) { printf("%d ", i); if (i%2 == 0) continue; printf("\n"); }

return The return statement is used to explicitly return from a method. That is, it causes program control to transfer back to the caller of the method. The following example illustrates this point. Here, return causes execution to return to the C, since it is the run-time system that calls main( ).

return // Demonstrate return (return.c). #include main() { int t = 1; printf("Before the return."); if(t==1) return; // return to caller printf("This won't execute."); }

goto It is possible to jump to any statement within the same function using goto. A label is used to mark the destination of the jump. goto label1; : label1:

goto // Using continue with a label (goto.c). #include main() { int i,j; for (i=0; i<10; i++) { for(j=0; j<10; j++) { if(j > i) { printf("\n"); goto outer; } printf(" %d", (i * j)); } outer: printf(".. outer..\n"); }