0% found this document useful (0 votes)
0 views

04 Making decision GUI

The document outlines a course on Programming for Engineers using the C language, covering topics such as decision-making structures (if, if-else, switch), looping constructs (for, while, do-while), and common programming errors. It provides syntax examples and flowcharts for various control structures and includes sample C code for practical understanding. The course is taught by Dr.-Ing. Nguyen Van Binh at HCMC, with content structured to facilitate learning programming concepts effectively.

Uploaded by

ngocthanh2821
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

04 Making decision GUI

The document outlines a course on Programming for Engineers using the C language, covering topics such as decision-making structures (if, if-else, switch), looping constructs (for, while, do-while), and common programming errors. It provides syntax examples and flowcharts for various control structures and includes sample C code for practical understanding. The course is taught by Dr.-Ing. Nguyen Van Binh at HCMC, with content structured to facilitate learning programming concepts effectively.

Uploaded by

ngocthanh2821
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

Course: Programming for Engineers (C language)

Dr.-Ing. Nguyen Van Binh


Email: [email protected]
HCMC, Feb. 2025
Content

1. Introduction to Computer and C language


2. Making Decisions
3. Looping
4. Functions
5. Arrays
6. Pointers
7. Structures
8. Characters and Strings
9. File Processing
10.Dynamic Memory Allocation
10 October 2023 Programming for Engineers (C language) NV Binh 2
2 Making decisions
2.1 Introduction

• This lecture introduces …


– Making decision/selection structures
• if
• if…else
• if… elseif …else
– switch multiple selection statement
– break statement
• Used for exiting immediately and rapidly from certain
control structures

10 October 2023 Programming for Engineers (C language) NV Binh 3


2 Making decisions
2.1 Introduction
– All programs written in terms of 3 control structures
• Sequence structures: Built into C. Programs executed sequentially by
default
• Selection structures: C has three types: if, if…else, and switch
• Repetition structures: C has three types: while, do…while and for

Flowcharting C’s sequence structure.

10 October 2023 Programming for Engineers (C language) NV Binh 4


2 Making decisions
2.2 The if statement • Selection structure
if
Only performs an action if the condition is true
• Syntax
If (test_expression)
{
statement 1;
statement 2;
...
}

Flowchart of if Statement
10 October 2023 Programming for Engineers (C language) NV Binh 5
2 Making decisions
2.2 The if statement

Flowcharting the single-selection if statement.

10 October 2023 Programming for Engineers (C language) NV Binh 6


2 Making decisions
2.2 The if statement

C code:
#include<stdio.h>
main(){
int grade;
printf( "Enter your grade :");
scanf("%d", &grade);
if ( grade >= 60 ) {
printf( "Passed\n");
}
}

10 October 2023 Programming for Engineers (C language) NV Binh 7


2 Making decisions
2.2 The if statement
• if…else
– Specifies an action to be performed both when the
condition is true and when it is false
• Syntax
if (test_expression)
{
//execute your code
}
else
{
//execute your code
}

Flowchart of if-else Statement


10 October 2023 Programming for Engineers (C language) NV Binh 8
2 Making decisions
2.2 The if statement

Flowcharting the double-selection if...else statement.

10 October 2023 Programming for Engineers (C language) NV Binh 9


2 Making decisions
2.2 The if statement
C code:
#include<stdio.h>
main(){
int grade;
printf( "Enter your grade :");
scanf("%d", &grade);
if ( grade >= 60 ){
printf( "Passed\n");
else
printf( "Failed\n");
}
}
10 October 2023 Programming for Engineers (C language) NV Binh 10
2 Making decisions
2.2 The if statement
• Syntax nested if…else statement

if(test_expression one)
{
if(test_expression two) {
/*Statement block Executes when the boolean test expression two is true.
*/
}
}
else
{
//else statement block
}

10 October 2023 Programming for Engineers (C language) NV Binh 11


2 Making decisions
C code:
2.2 The if statement
#include<stdio.h>
nested if…else statement
main(){
int grade;
printf( "Enter your grade :");
scanf("%d", &grade);
if ( grade >= 60 ){
if ( grade >= 90 )
printf( "excellent \n");
else
printf( “Good\n");
}
else
printf( "Failed\n");
}
10 October 2023 Programming for Engineers (C language) NV Binh 12
2 Making decisions
2.2 The if statement
The else…if statement
• Syntax
if(test_expression)
{
//execute your code
}
else if(test_expression n)
{
//execute your code
}
else
{
//execute your code
}

10 October 2023 Programming for Engineers (C language) NV Binh 13


2 Making decisions
C code:
2.2 The if statement
#include<stdio.h>
The else…if statement
main(){
int grade;
printf( "Enter your grade :");
scanf("%d", &grade);
if ( grade >= 90 )
printf( "excellent \n");

else if ( grade >= 60 ){


printf( "Good\n");
}
else
printf( "Failed\n");
}
10 October 2023 Programming for Engineers (C language) NV Binh 14
2 Making decisions
2.3 The switch statement
– Useful when a variable or expression is tested for all the values it can
assume and different actions are taken

switch multiple-selection statement with breaks


10 October 2023 Programming for Engineers (C language) NV Binh 15
2 Making decisions
2.3 The switch statement

• Syntax
– Series of case labels and an optional default case
switch ( value ){
case '1’:
//actions in case 1
break; //exits from statement
case ‘2’:
//actions in case 1
break; //exits from statement
default:
//actions in default
}

10 October 2023 Programming for Engineers (C language) NV Binh 16


2 Making decisions
2.3 The switch statement
#include<stdio.h> printf("You chose Four");
main() break;
{ case 5:
int a; printf("You chose Five.");
printf("Please enter a no between 1 and 5: break;
"); default :
scanf("%d",&a); printf("Invalid Choice. Enter a no between
switch(a) 1 and 5");
{ break;
case 1: }
printf("You chose One"); }
break;
case 2:
printf("You chose Two");
break;
case 3:
printf("You chose Three");
break;
case 4:
10 October 2023 Programming for Engineers (C language) NV Binh 17
2 Making decisions
2.4 Loop
– Repetition control structures
• for
• while
• do…while

Flowchart of Looping

10 October 2023 Programming for Engineers (C language) NV Binh 18


2 Making decisions
2.4 Loop
for Loop
Syntax:
for ( init; condition; increment )
{
statement(s);
}

Flowchart of Looping

10 October 2023 Programming for Engineers (C language) NV Binh 19


2 Making decisions
2.4 Loop
for Loop
#include<stdio.h>
int main ()
{
/* local variable Initialization */
int n,times=5;
/* for loops execution */
for( n = 1; n <= times; n = n + 1 )
{
printf("C for loops: %d\n", n);
}
return 0;
}

10 October 2023 Programming for Engineers (C language) NV Binh 20


2 Making decisions
2.4 Loop
while Loop
Syntax:
While (condition)
{
statement(s);
Incrementation;
}

Flowchart of while loop


10 October 2023 Programming for Engineers (C language) NV Binh 21
2 Making decisions
2.4 Loop
#include<stdio.h> while Loop
int main ()
{
/* local variable Initialization */
int n = 1,times=5;

/* while loops execution */


while( n <= times )
{
printf("C while loops: %d\n", n);
n++;
}
return 0;
}
10 October 2023 Programming for Engineers (C language) NV Binh 22
2 Making decisions
2.4 Loop do while Loop
Syntax:
do
{
statement(s);

}while( condition );

Flowchart of do while loop

10 October 2023 Programming for Engineers (C language) NV Binh 23


2 Making decisions
2.4 Loop do while Loop
#include<stdio.h>

int main ()
{
/* local variable Initialization */ int n
= 1,times=5;

/* do loops execution */ do
{
printf("C do while loops: %d\n", n);
n = n + 1;
}while( n <= times );

return 0;
}

10 October 2023 Programming for Engineers (C language) NV Binh 24


2 Making decisions
Common Programming Error

• Placing a semicolon after the condition

if ( grade >= 60 );

• Missing the beginning and ending braces of compound

statements

• Forgetting a break statement in a switch statement.

• Forgetting an increment in while loop.


10 October 2023 Programming for Engineers (C language) NV Binh 25

You might also like